Hello guys,
So I decided to learn basics of the arduino. I've encountered a problem while trying to read text from serial monitor.
Idea of my project is to control RGB led using Arduino Uno board. So the problem is that the RGB LED won't turn on while using Serial.readString () function. The code doesn't have any errors. LED works 100% because I can turn it on with command digitalWrite(redpin,HIGH) etc. Also I can turn it on when I try to read numbers from serial monitor with command (Serial.parseInt()) . So the problem is not in the wiring or LED itself.
I have to mention, that one day before the code was working just fine and now it does not.
Here is my code:
int redpin = 5, greenpin = 3, bluepin = 6;
String msg = "What color do you want?";
String color;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println (msg);
while (Serial.available () == 0) { //Waiting for response in serial monitor
}
color = Serial.readString();
if (color == "red") {
digitalWrite(redpin, HIGH);
digitalWrite(greenpin, LOW);
digitalWrite(bluepin, LOW);
}
if (color == "green") {
digitalWrite(redpin, LOW);
digitalWrite(greenpin, HIGH);
digitalWrite(bluepin, LOW);
}
if (color == "blue") {
digitalWrite(redpin, LOW);
digitalWrite(greenpin, LOW);
digitalWrite(bluepin, HIGH);
}
if (color == "off") {
digitalWrite(redpin, LOW);
digitalWrite(greenpin, LOW);
digitalWrite(bluepin, LOW);
}
}
Assuming that you use serial monitor, what is the line ending set to in serial monitor? If it's not set to 'none', you will receive extra characters and hence it will not match your words.
Would you mind learning the basics of this forum? Please read the guide in the sticky post at the top of any forum section and then modify your post above and put in the code tags. Thanks!
Serial.readString()
Description
Serial.readString() reads characters from the serial buffer into a String. The function terminates if it times out (see setTimeout()).
vs.
Serial.readStringUntil()
Description
readStringUntil() reads characters from the serial buffer into a String. The function terminates if it times out (see setTimeout()).
Hmm... the description of readStringUntil() seems to be a cut&paste...
It should read
The function terminates if it times out (see setTimeout()) or the terminator character is read.
The duplicate topic in the Avrdude, stk500, Bootloader issues section has been deleted
No only was it a duplicate but it had no reason to be there
Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting will result in a timeout from the forum.
In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.