This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I am going through some YouTube tutorials with Paul McWhorter. I always like to add a little extra work at the end of his videos to stretch what I learned.
In my last few lessons we used the Serial Read functions to pull strings from the Serial Monitor and using those responses to light up LED.
I found that part easy and went beyond and added or functions to my IF statements so I could account for various capitalizations and that worked well.
I wanted to add a "rude" response if a color other that three given selections (red, yellow, green) were given. For some reason when I add that portion it sends the "rude" feedback 100%. I tried tweaking to both "and" and "or" functions to try and fix this but am told one my variable "response" is used but not defined. It flags the error on the very last "IF" statement.
Here is my current code:
int RED=9;
int YELLOW=8;
int GREEN=7;
int wait=3000;
String response;
String msg1="What color would you like?";
String msg2="Red, yellow, or green?";
String msg3="Enjoy the color ";
String msg="green.";
String msr="red.";
String msy="yellow.";
String msa="Enjoy them all at once. You Monster";
String rude="Fuck off and answer the question!";
void setup() {
Serial.begin(9600);
pinMode(GREEN,OUTPUT);
pinMode(RED,OUTPUT);
pinMode(YELLOW,OUTPUT);
}
void loop() {
Serial.println();
Serial.println(msg1);
Serial.println(msg2);
while(Serial.available()==0){}
response=Serial.readString();
if (response=="RED"|| response=="Red" ||response=="red"){
digitalWrite(RED,HIGH);
digitalWrite(YELLOW,LOW);
digitalWrite(GREEN,LOW);
Serial.print(msg3);
Serial.println(msr);
}
if (response=="YELLOW" || response=="Yellow" ||response=="yellow"){
digitalWrite(RED,LOW);
digitalWrite(YELLOW,HIGH);
digitalWrite(GREEN,LOW);
Serial.print(msg3);
Serial.println(msy);
}
if (response=="GREEN" ||response=="Green" ||response=="green"){
digitalWrite(RED,LOW);
digitalWrite(YELLOW,LOW);
digitalWrite(GREEN,HIGH);
Serial.print(msg3);
Serial.println(msg);
}
if (response=="ALL" ||response=="All" ||response=="all"){
digitalWrite(RED,HIGH);
digitalWrite(YELLOW,HIGH);
digitalWrite(GREEN,HIGH);
Serial.println(msa);
}
if (response!="RED"||&& response!="Red" ||&& response!="red" ||&& response!="GREEN" ||&& response!="Green" ||&& response!="green" ||&& response!="YELLOW" ||&& response!="Yellow" ||&& response!="yellow" ||&& response!="ALL" ||&& response!="All" ||&& response!="all" ){
digitalWrite(RED,LOW);
digitalWrite(YELLOW,LOW);
digitalWrite(GREEN,LOW);
Serial.println(rude);
}
delay(wait);
}
Post Details
- Posted
- 10 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/arduino/com...