Hello, good people.
EXPECTED OUTCOME:
I am brand new to learning Arduino. I'm having difficulties with having LED's light up based upon the input the user gives in the Serial Monitor. The outcome that I'm attempting to make happen is the Serial Monitor asks the user to type RED, YELLOW, or GREEN. When the user inputs a color, the corresponding LED lights up.
SETUP:
I have a red, yellow, and green LED attached to a breadboard with 330 ohm resistors. The Arduino I'm using is a Mega 2650. I have the wires hooked up to three PWM slots. I first made sure that the LED setup is working. I set all 3 LEDs to HIGH with no input from the user, and all of the LEDs lit up just fine. The baud rate is set to 9600 on the Serial Monitor as well. I coded the sketch to have one LED on at a time just to make sure that I declared each output slot on the Arduino correctly (meaning I wanted to make sure I didn't have "int redLED = 6;" when it's actually the green LED that's wired to slot 6).
FIRST ATTEMPT AT HOPED-FOR OUTCOME:
After I made sure all of that is working properly, I coded the sketch to where it will ask the user for an input of a String and light up the corresponding LED while having the other 2 turn off. When I type RED, YELLOW, or GREEN, none of the LED's light up. I made sure that I typed the response in all CAPS since that's how I set up the IF statements.
TROUBLESHOOTING TO SEE IF THE COMMAND TO TURN ON LEDs EXECUTES AFTER ANY INPUT:
What I did next was have it ask the user for an input, turn on all of the LEDs regardless of the input just to see if it's executing any code after the input command. All of the LED's turned on when I typed anything.
TROUBLESHOOTING TO SEE IF ANYTHING WITHIN THE "IF" COMMAND IS EXECUTED:
I then added an output of "Hello" after the LED's turned on just to make sure it was outputting something, and then I added the IF statements after the "Hello" output. The Serial Monitor asks the user for the input, I typed a color, all of the LEDs turned on, the word "Hello" displayed, and I expected two of the lights to turn off after the output of "Hello" while keeping on the LED of the color I typed. All of the LEDs just stayed on. I then have it coded to where it asks for a color, turns on the corresponding LED, and then prints the input of the string just to see if it was accepting the input. When I type a color, no LEDs turn on and no text prints in the Serial Monitor to show what color I typed.
CONCLUSION:
I have a circuit set up on the site TinkerCAD of this setup (except it's an Arduino UNO) and it's working just fine. I even copied and pasted exactly what I have on TinkerCAD, and it still won't work. Sorry if this description ends up not being clear to anyone who reads it. I've re-read it several times and tried to make it as clear as I could. I'll provide any clarification if needed. Is anyone able to offer guidance on where I'm going wrong with this? Thanks a bunch.
int delayChoice = 1000;
int redLED = 7;
int greLED = 6;
int yelLED = 5;
String askColor = "Please enter RED, GREEN, or YELLOW: ";
String color;
void setup(){
Serial.begin(9600);
pinMode(redLED, OUTPUT);
pinMode(greLED, OUTPUT);
pinMode(yelLED, OUTPUT);
}
void loop(){
Serial.println(askColor);
while (Serial.available() == 0){
}
color = Serial.readString();
if (color == "RED"){
digitalWrite(yelLED, LOW);
digitalWrite(greLED, LOW);
digitalWrite(redLED, HIGH);
delay(delayChoice);
}
if (color == "YELLOW"){
digitalWrite(greLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(yelLED, HIGH);
delay(delayChoice);
}
if (color == "GREEN"){
digitalWrite(yelLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(greLED, HIGH);
delay(delayChoice);
}
}