const int greenLED = 2;
const int yellowLED = 3;
const int redLED = 4;
char lastCommand = ' ';
void setup() {
Serial.begin(9600);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
// This is the menu of choices
Serial.println("Choose one of these letters to turn on the light:");
Serial.println("R - RED");
Serial.println("Y - YELLOW");
Serial.println("G - GREEN");
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
if (command == 'g' || command == 'G') {
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
Serial.println("- You have turned on GREEN");
lastCommand = 'g';
} else if (command == 'y' || command == 'Y') {
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
Serial.println("- You have turned on YELLOW");
lastCommand = 'y';
} else if (command == 'r' || command == 'R') {
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
Serial.println("- You have turned on RED");
lastCommand = 'r';
} else if (command != ' ' && command != '\n') {
// This is the error message
Serial.println("Sorry, you have written a wrong letter. Choose one of these (R - RED, Y - YELLOW, G - GREEN)");
}
}
}