I am currently trying to have the LCD print out random statements each statement is a switch case.
Within the switch I have an if statement where if within this case you press the correct button or sense the correct color then it'll print out that you're correct and the green LED will turn on. If you're wrong then the LCD will print out try again and the red LED turns on. I have it so that if the skip button is pressed or the ans is correct then pick a new random case.
Some issues I'm running into:
-
Once it runs through the else statement how do I return it to that same case?
-
Also my skip switch works but, the other switch buttons do not. I've tested these buttons and they're wired correctly. So something is wrong with my logic and I don't really know why because I have it that if it prints this case out and the button pressed is correct make the ans=1 which will then randomly pick out the next case.
I've posted a schematic and the code that I have so far. Sorry if I don't make any sense I'm a beginner at arduino and I'm trying to combine certain projects from the project book it comes with. Any help or advice is highly appreciated.
Here's my code so far:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 13; //skip button
int switchState = 0;
int reply;
int prevreply;
const int sensorPin = A0; //phototransistor
int sensorValue;
int cDelay = 1000; //1 Sec delay
int ans = 0;
const int aSwitchPin = 8; //A
const int bSwitchPin = 7; //B
const int cSwitchPin = 6; //C
int aSwitchState = 0;
int bSwitchState = 0;
int cSwitchState = 0;
const int rLEDPin = 10; //Red LED
const int gLEDPin = 9; //Green LED
void setup() {
lcd.begin(16, 2);
pinMode(switchPin,INPUT);
pinMode(aSwitchPin, INPUT);
pinMode(bSwitchPin, INPUT);
pinMode(cSwitchPin, INPUT);
pinMode(rLEDPin, OUTPUT);
pinMode(gLEDPin, OUTPUT);
lcd.print("Let's Play!");
lcd.setCursor(0, 1);
lcd.print("LETTERS & COLORS");
Serial.begin(9600);
}
void loop() {
delay(500);
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
Serial.println(ans);
switchState = digitalRead(switchPin);
aSwitchState = digitalRead(aSwitchPin);
bSwitchState = digitalRead(bSwitchPin);
cSwitchState = digitalRead(cSwitchPin);
if (switchState == HIGH || ans == 1) {
ans = 0;
reply = random(0,4);
//ans = 0;
while(reply == prevreply) { //if reply is the same as the previous reply then generate a different number
reply = random(0,4);
}
prevreply = reply;
lcd.clear();
switch(reply){
case 0:
lcd.print("Press letter A");
Serial.println(aSwitchState);
if(aSwitchState == 1) {
lcd.setCursor(0, 1);
lcd.print("You're AMAZING!");
delay(cDelay);
ans = 1;
}
break;
case 1:
lcd.clear(); //clear before starting case
digitalWrite(gLEDPin,LOW);//Turn off in the beginning of case
digitalWrite(rLEDPin,LOW);
lcd.print("Press letter B");
if(bSwitchState == 1) {
lcd.setCursor(0, 1);
lcd.print("You're BRILLIANT!");
delay(cDelay);
ans = 1;
}
break;
case 2:
lcd.clear(); //clear before starting case
digitalWrite(gLEDPin,LOW);//Turn off in the beginning of case
digitalWrite(rLEDPin,LOW);
lcd.print("Press letter C");
if(cSwitchState == 1) {
lcd.setCursor(0, 1);
lcd.print("You're CLEVER!");
delay(cDelay);
ans = 1;
}
break;
case 3:
lcd.clear(); //clear before starting case
digitalWrite(gLEDPin,LOW);//Turn off in the beginning of case
digitalWrite(rLEDPin,LOW);
lcd.print("Scan color RED");
if(sensorValue >= 120 && sensorValue < 200) { //calibrated values based on measured values of from transistor
lcd.setCursor(0, 1); //go to next line
lcd.print("Correct Red!");
digitalWrite(gLEDPin,HIGH);
delay(cDelay);
ans = 1;
}
else {
lcd.setCursor(0, 1);
lcd.print("Try again");
digitalWrite(rLEDPin,HIGH);
delay(cDelay);
}
break;
case 4:
lcd.clear();
digitalWrite(gLEDPin,LOW);
digitalWrite(rLEDPin,LOW);
lcd.print("Scan color BLUE");
if(sensorValue > 75 && sensorValue < 120) {
lcd.setCursor(0, 1);
lcd.print("Correct Blue!");
digitalWrite(gLEDPin,HIGH);
//Serial.print("Correct Blue!");
delay(cDelay);
ans = 1;
}
else {
lcd.setCursor(0, 1);
lcd.print("Try again");
digitalWrite(rLEDPin,HIGH);
delay(cDelay);
//lcd.clear();
//return 4; //How do you return to top of the case statement???
}
break;
}
}
}
Schematic: