Trying to read from serial and print "$101=152" if incoming serial string is = "red. Not working why?"

String red;         // incoming serial byte

void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {

  while (Serial.available() > 0){
    
    red=Serial.readString(); //store string
       if(red == "red"){
          Serial.println("$101=152");
    }
   }
  }



red=Serial.readString(); //store string

What have you got the Serial monitor Line ending set to ? If you want a clue as to the probable cause of your problem try printing the length of the String returned by Serial.readString()

when type red, but does not return "$101=125"

Change the Line ending setting to "No line ending" otherwise extra characters will be appended to the end of what you type. To see the effect try printing the length of the String as I suggested

Sorry, don't really know how to change to "No line ending", could you explain how please?
thanks.

try this maybe, it checks if "red" is anywhere in received string.

String red;         // incoming serial byte

void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("READY!");
}

void loop() {

  while (Serial.available() > 0) {

    red = Serial.readString(); //store string
    if (strstr(red.c_str(), "red") != NULL) {
      Serial.println("$101=152");
    }
    else {
      Serial.println("Not RED!");
    }
  }
}

hope that helps...

1 Like

Look at the bottom of your Serial monitor screen shot and you will see
image

Change that value

Works great!! thanks.
How do i do same but only if it finds exactly red?

does give 5 as length instead of 3 length (red).

My advice would be to simply change the Line ending and to use your original code

In my opinion

if (strstr(red.c_str(), "red") != NULL)

is a bit too advanced for you at the moment

@robogio

1. If you choose Newline option (for example) for the Line ending tab of the Serial Monitor (Fig-1) and then enter red in the InputBox and then click on the Send button, then the following events occur: (When click is done on the Line ending tab, a drop-down menu appears with options for: No line ending, Newline, Carriage return, Both LN & CR)

SerialMonitor
Figure-1:

(1) Serial Monitor sends these ASCII codes (for the characters r e d Newline): 0x72 x65 0x64 0x0A one-after-another to the UNO.

(2) UNO receives these codes and saves in the variable red using readString() method.
(3) Because you have used readString() method, a null-character (ASCII code is 0x00) is automatically added at the end of the red variable. Now, the red variable contains five characters (r e d Newline null-char).

(4) When the equality comparison is made by this code if(red == "red"), you are comparing five characters against four characters ("red" string holds an unseen null-character at the end); so, the comparison fails and there appears nothing on the OutputBox of the Serial Monitor.

(5) The solution is (as suggested by @UKHeliBob in post #4) to choose "No line ending" for the Line ending tab of the Serial Monitor; as a result, the Serial Monitor will send the ASCII codes only for three characters (r e d). Now, the comparison matches and the message $101=152 appears on the Serial Monitor.

1 Like

So your 3 character fixed string is never going to match the 5 character input. Change the Line ending

You could insert a trim function to get rid of the invisible characters (line feed and carriage return) if present. Then the line ending setting won't matter.

red = Serial.readString();  //store string
red.trim();      
if(red == "red")
{

That said, using the String class can cause mysterious memory problems and should be avoided if you don't know what you are doing. See the Evils of Arduino Strings.

11 posts were split to a new topic: Difference between String and string

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.