Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

08.Strings > StringLength has several problems #36

Open
TheOldBrick opened this issue Apr 3, 2021 · 0 comments
Open

08.Strings > StringLength has several problems #36

TheOldBrick opened this issue Apr 3, 2021 · 0 comments

Comments

@TheOldBrick
Copy link

TheOldBrick commented Apr 3, 2021

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.

  • It outputs seemingly extraneous characters

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;
    }
  }
}

  • It contains esoteric logic that is never explained

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant