I would like to control three LED's with a string variable using arduino mega.
I have connected the three LED's to pins 10,11 and 12 of the arduino.
If my string equals Red, the red LED should turn ON. if my string equals Yellow, the yellow LED should turn ON. if my string equals Green, the green LED should turn ON
I wrote the following program, and I expect the following: When I type Red, I expect the Red LED to turn ON, and so on for the others. but no LED turns ON
You can check the following video: https://www.youtube.com/watch?v=MAnAc_t0OrM&t=1583s
int redPin=10,yellowPin=11,greenPin=12;
String my_Color;
String msg1="Which LED do you want to turn ON?";
String msg2="Red, Yellow or Green?";
String msg3="You choose LED:";
void setup() {
// put your setup code here, to run once:
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(msg1);
Serial.println(msg2);
while(Serial.available()==0){
}
my_Color=Serial.readString();
Serial.print(msg3 );
Serial.println(my_Color);
Serial.println('Red',BIN);
Serial.println('my_Color',BIN);
if(my_Color=="Red")
{
Serial.print("Red True");
digitalWrite(redPin,HIGH);
digitalWrite(yellowPin,LOW);
digitalWrite(greenPin,LOW);
}
if(my_Color=="Yellow")
{
digitalWrite(redPin,LOW);
digitalWrite(yellowPin,HIGH);
digitalWrite(greenPin,LOW);
}
if(my_Color=="Green")
{
digitalWrite(redPin,LOW);
digitalWrite(yellowPin,LOW);
digitalWrite(greenPin,HIGH);
}
}
Could anyone check the sketch and correct me ??