arduino / arduino-examples

Arduino IDE bundled examples
Creative Commons Zero v1.0 Universal
90 stars 41 forks source link

08.Strings > StringLength has several problems #36

Open TheOldBrick opened 3 years ago

TheOldBrick commented 3 years ago

The StringLength example has several problems.


It is the only example so far that shares a https://www.arduino.cc/en/Tutorial/BuiltInExamples page with another example, specifically StringLengthTrim. See http://www.arduino.cc/en/Tutorial/StringLengthTrim. When a user goes to this page, its code is completely different than the code in the IDE example for StringLength; this page shows the code for the StringLengthTrim example.

StringLength should have its own /www.arduino.cc/en/Tutorial/BuiltInExamples page like every other example.


The code in the IDE example for StringLength is very confusing for new users.

Example:

I entered fishfood in the Serial Monitor, and here's the output that the code consistently produces:


The code should be similar to Examples > 04.Communication > SerialEvent

Here's code that I used instead for the StringLength example:

String txtMsg = "";                    // variable to hold incoming text
bool stringComplete = false;           // whether the string is ready to display

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // send an intro
  Serial.println("\n\nString  length():\n");

}

void loop() {

  // If the user wants to display the cumulative text message
  if (stringComplete) {

    // get length of the cumulative text message
    unsigned int txtMsgLength = txtMsg.length();

    // print the cumulative text message
    Serial.println(txtMsg);
    // print the length of the cumulative text message
    Serial.println(txtMsgLength);

    // if the cumulative text message is >= 140 characters, complain:
    if (txtMsgLength < 140) {
      Serial.println("That's a perfectly acceptable text message");
    } else {
      Serial.println("That's too long for a text message.");
    }

    // prepare for next chunk of the cumulative text message
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  routine is run between each time loop() runs, so using delay inside loop can
  delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // if the incoming character is a newline
    if (inChar == '\n') {
      // set a flag to indicate that the user wants to display the cumulative text message
      stringComplete = true;
    // otherwise
    } else {
      // add the incoming character to the cumulative text message
      txtMsg += inChar;
    }
  }
}


New users need to be told the purpose of this code. That the user should repeatedly enter text into the Serial Monitor until the cumulative text, which the program saves, reaches 140 characters.