Hi. I am following a youtube tutorial series and when i follow the code shown nothing is happening...
The led works, it will light all 3 colours when I jump it straight from the 5v, but nothing happens when i type in a colour into the serial monitor. can anybody see anything wrong with the code?
//
int red = 2;
int blue = 3;
int green = 4;
String quiestion = "Which colour do you like? Blue, Green or Red...";
String answer;
void setup() {
Serial.begin(9600);
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
}
void loop() {
Serial.println(quiestion);
while (Serial.available() == 0) {
}
answer = Serial.readString();
if (answer == "red") {
digitalWrite(red, HIGH);
digitalWrite(blue, LOW);
digitalWrite(green, LOW);
}
if (answer == "blue") {
digitalWrite(red, LOW);
digitalWrite(blue, HIGH);
digitalWrite(green, LOW);
}
if (answer == "green") {
digitalWrite(red, LOW);
digitalWrite(blue, LOW);
digitalWrite(green, HIGH);
}
}//
i think i am missing something really obvious but just cant see it. using pins 2,3,4 for supply.
thanks in advance whoever finds...
What is the line ending setting in the serial monitor?
Why do you think this is an installation issue?
Please remember to use code tags when posting code.
Hi. i am trying to serial read a string to change colour of an RGB Led. This is driving me crazy.... The led works, it will light all 3 colours when I jump it straight from the 5v, but nothing happens when i type in a colour into the serial monitor. can anybody see anything wrong with the code?
//
int red = 2;
int blue = 3;
int green = 4;
String quiestion = "Which colour do you like? Blue, Green or Red...";
String answer;
void setup() {
Serial.begin(9600);
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
}
void loop() {
Serial.println(quiestion);
while (Serial.available() == 0) {
}
answer = Serial.readString();
if (answer == "red") {
digitalWrite(red, HIGH);
digitalWrite(blue, LOW);
digitalWrite(green, LOW);
}
if (answer == "blue") {
digitalWrite(red, LOW);
digitalWrite(blue, HIGH);
digitalWrite(green, LOW);
}
if (answer == "green") {
digitalWrite(red, LOW);
digitalWrite(blue, LOW);
digitalWrite(green, HIGH);
}
}//
i think i am missing something really obvious but just cant see it. using pins 2,3,4 for supply.
thanks in advance whoever finds...
Never apply 5V directly to an LED. It can easily damage the LED. You need current limiting resistors, even when the LED is driven from a microprocessor output.
The Serial baud rate is set to 9600 in your sketch. Are you also using the same baud rate in the Serial Monitor? Also, try printing the answer and see what you get.
Thread moved to a more appropriate forum section
Duplicate threads merged
Why did you start a second one ?