// Arduino Tutorial 19: Reading Strings from the Serial Monitor
// By: Paul McWhorter
// Website:
// https://toptechboy.com/arduino-tutorial-19-reading-strings-from-the-serial-monitor/
//
// Code taken from:
// https://github.com/mazjin/paulmcwhorter_arduino_tutorials/blob/master/lesson_19/lesson_19_session.ino
//
//
// Warning:
// It does not work.
// It is supposed to not work.
// It is not your fault.
// You should skip is lesson.
// Solution:
// Under the Youtube video is a message from Paul:
// "GUYS MAKE SURE YOUR SERIAL MONITOR IS SET TO 'NO LINE ENDING'!!!"
// "Otherwise you will have problems with this code"
// No Paul, this is your fault, you should not bother new users with this problem.
// Solution in Wokwi:
// Wokwi has a option to avoid that a CarriageReturn or LineFeed is added.
// I have turned that on in "diagram.json"
// Using it:
// Start the simulation.
// Type lowercase 'red' or 'r' followed by enter.
// Try the other colors as well.
int endDelay = 1000;
String inputString;
// String prompt = "What is your name? ";
// String response1 = "Hello ";
// String response2 = ", welcome to Arduino!";
String prompt = "Would you like a RED (R), BLUE (B) or GREEN (G) light?";
int redPin = 6;
int grnPin = 5;
int bluPin = 3;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println(prompt);
while(Serial.available() == 0){}
inputString = Serial.readString();
inputString.toLowerCase();
Serial.println();
//VSCode serial monitor input always uses \n line ending - be careful!
if(inputString == "red" || inputString == "r") {
digitalWrite(redPin,1);
digitalWrite(grnPin,0);
digitalWrite(bluPin,0);
} else if(inputString == "green" || inputString == "g") {
digitalWrite(redPin,0);
digitalWrite(grnPin,1);
digitalWrite(bluPin,0);
} else if(inputString == "blue" || inputString == "b") {
digitalWrite(redPin,0);
digitalWrite(grnPin,0);
digitalWrite(bluPin,1);
} else {
Serial.println("No valid input :(");
digitalWrite(redPin,0);
digitalWrite(grnPin,0);
digitalWrite(bluPin,0);
}
delay(endDelay);
}