New to Arduino, getting into String variables and reading them from serial monitor. Basically what I'm trying to do is have the user input either red, green, yellow. Whichever String they enter the LED comes on.
What is happening seems to be that the if statements are not true and are never entered thus not LEDs come on. I do have a serial.Println(myColor) at the end which displays myColor as the user has inputted into serial monitor. I just don't understand why the if statements are not firing. its really weird.
String msg="What LED do you want to turn on?";
String myColor;
int redPin = 12;
int greenPin = 13;
int yellowPin = 8;
void setup(){
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
}
void loop(){
//LED ColorDEmo
Serial.println(msg); //print the question to serial monitor
while(Serial.available() == 0){ //while we wait do nothing till data is available
}
myColor = Serial.readString(); // set myColor to serial value
if (myColor == "red"){ // if the value is red
Serial.println("In red");
turnOffLEDS(); // turn off all LEDS
digitalWrite(redPin, HIGH); // turn on red lED
}
if (myColor == "green"){ // if the value is green
Serial.println("In green");
turnOffLEDS(); // turn off all LEDS
digitalWrite(greenPin, HIGH); // turn on green LED
}
if (myColor == "yellow"){ // if the value is yellow
Serial.println("In yellow");
turnOffLEDS(); // turn off all LEDS
digitalWrite(yellowPin, HIGH); // turn on yellow LED
}
Serial.println(myColor); // print myColor
}
// turns all LEDS off
void turnOffLEDS(){
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(redPin, LOW);
}
-
debug your code by printing data to the serial console ... print the received message and print the length of the messagejsotola– jsotola07/20/2022 02:58:48Commented Jul 20, 2022 at 2:58
-
myColor = Serial.readString(); Serial.println(myColor.length()); Serial.println(myColor) results 4 red So it seems it is attaching an additional character tot he end of the String variable. SHould i be using readStringUntil() insteadMoose– Moose07/20/2022 03:17:02Commented Jul 20, 2022 at 3:17
-
Just a little side-point: It is a good to get used to using char arrays rather then strings. I know the current way arduino moves is to keep users on lover level, (buing more shields) but once you get to more advanced tasks and have to deal with memory footprint, then habit of using char array makes your life easier latter.Tomas– Tomas07/20/2022 03:51:00Commented Jul 20, 2022 at 3:51
-
Thanks @Tomas, that's a really good point. String variables are memory hogs from what I've heard from others as well.Moose– Moose07/20/2022 04:08:21Commented Jul 20, 2022 at 4:08
-
Also, what is the program to do if the user enters "RED" instead of "red", "RE" only, "R" only, etc. That is why single character responses are generally better and less error-prone.starship15– starship1507/26/2022 17:53:07Commented Jul 26, 2022 at 17:53
2 Answers 2
Avoiding String objects (back to the basics) often makes life easier. Simply check if the answer contains one of the characters 'd' 'g' 'y'
Or (and) simplify your protocol to only send one character.
BTW:You might notice a delay in readString, waiting for a multi-character input to finish, because Serial.available() is true already after the first character of an input, and readString terminates only if it times out
-
thats a good idea, simplify into single characters.Moose– Moose07/21/2022 17:38:06Commented Jul 21, 2022 at 17:38
thank you @jsotola
throught he debugging you mentioned i was able to learn that the '\n' was added to the end of the string. Instead of using
Serial.readString()
I used Serial.readStringUntil('\n');
this would check for a delimiter character '\n' then stop. Thus, my myColor variable would be red instead of red'\n'