Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

08.Strings > StringLength has several problems #36

Open
@TheOldBrick

Description

@TheOldBrick

The StringLength example has several problems.


It is the only example so far that shares ahttps://www.arduino.cc/en/Tutorial/BuiltInExamples page with another example, specifically StringLengthTrim. Seehttp://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 enteredfishfood in the Serial Monitor, and here's the output that the codeconsistently 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 textbool stringComplete = false;           // whether the string is ready to displayvoid 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp