Hi all, I am brand new to Arduino, and have been following Paul McWhorter tutorials. All has been good so far, but I am now experiencing issues. I have checked and checked everything but must be missing something.
I have successfully used strings via serial printer, but when it comes to asking what colour LED, it will not turn on the LED using the IF strings.
Please help, I have been really enjoying the tutorials so far, but this is frustrating.
It asks what colour, but when I type an answer, nothing happens in the circuit. I have proven the circuit so it has to be my code.
My code is as follows:
int redpin=8;
int greenpin=9;
int bluepin=10;
String mycolour;
String msg="What colour do you want?";
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:
digitalWrite(greenpin,HIGH);
delay(100);
digitalWrite(greenpin,LOW);
delay(100);
Serial.println(msg);
while (Serial.available()==0) {
}
mycolour=Serial.readString();
if (mycolour=="red") {
digitalWrite(redpin,HIGH);
digitalWrite(greenpin,LOW);
digitalWrite(bluepin,LOW);
}
if (mycolour=="red") {
digitalWrite(greenpin,HIGH);
digitalWrite(redpin,LOW);
digitalWrite(bluepin,LOW);
}
if (mycolour=="red") {
digitalWrite(bluepin,HIGH);
digitalWrite(greenpin,LOW);
digitalWrite(redpin,LOW);
}
if (mycolour=="off") {
digitalWrite(redpin,LOW);
digitalWrite(greenpin,LOW);
digitalWrite(redpin,LOW);
}
}