Please help me!!!

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:

  1. Once it runs through the else statement how do I return it to that same case?

  2. 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:

Here, OP, is how to get an image into a post

I haven't looked at your code or schematic yet (I need coffee and it's trash day....) but you may find it useful to look at the code I posted in #12 here.

//return 4; //How do you return to top of the case statement???

If you have not changed the switch variable in the case then next time through loop() the code for the same case will be executed. Note that you cannot return a value from loop(). Where would it return it to ?

UKHeliBob:

//return 4; //How do you return to top of the case statement???

If you have not changed the switch variable in the case then next time through loop() the code for the same case will be executed. Note that you cannot return a value from loop(). Where would it return it to ?

Oh, I see, I wasn’t sure about the return function since I’m not familiar with it. That’s why I just commented it out. Thanks for letting me know. The beginning of each loop is set so that a new random variable is created each time. I don’t really know how to hold that switch variable the same.

Once I run the code and it runs through the the case statement which then runs into the if else but whenever it's the else statement it gets stuck there. I’m not sure how to make it run through the loop again with the same switch variable after going through the else statement.

he beginning of each loop is set so that a new random variable is created each time.

Then each time through loop() it will execute the code for the case returned by the random() function which is probably not what you want.

I think what you want is for the code to stay in the same case until the user provides the correct answer, at which point you move to a random case and so on. Does that sound like what you are after ?

Here is your current code Auto formatted in the IDE and posted here in code tags. Please do the same in future when posting code

#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;
    }
  }
}

UKHeliBob:
Then each time through loop() it will execute the code for the case returned by the random() function which is probably not what you want.

I think what you want is for the code to stay in the same case until the user provides the correct answer, at which point you move to a random case and so on. Does that sound like what you are after ?

Here is your current code Auto formatted in the IDE and posted here in code tags. Please do the same in future when posting code

#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;
    }
  }
}

Oh, thank you I’ll fix that on my post. That kind of sounds like what I want, but it’s more like I want it to stay on the same case unless the correct answer (ans==1) or the skip button is pressed (switchState==HIGH) then move on to a random case.

cyrawr:
I want it to stay on the same case unless the correct answer (ans==1) or the skip button is pressed (switchState==HIGH) then move on to a random case.

In the sketch I linked earlier, you can get some clues to do that. In each case of the switch..case it asks a question, and the pressing of the Yes or No button takes you to some other case. That other case is the next question (if answer is correct) or the "bad luck" message if you got the answer wrong. If you get the answer correct in the last question, you get the "well done" message.

jubukraa:
In the sketch I linked earlier, you can get some clues to do that. In each case of the switch..case it asks a question, and the pressing of the Yes or No button takes you to some other case. That other case is the next question (if answer is correct) or the "bad luck" message if you got the answer wrong. If you get the answer correct in the last question, you get the "well done" message.

Thank you that actually is very similar to what I'm trying to do. I just have a couple of questions about it. I think I understand why you created a new function to be able to go back go to a specific case within the switch. So in my case my code would look more like this :

void LCDreplies () {
  switch(reply)
  {
    case 0:
       lcd.print("Press letter A");
         if(correct) {
           lcd.setCursor(0, 1);
           lcd.print("You're AMAZING!");
           delay(cDelay);
           LCDreplies = random(reply); //new random case
         }
         if(skip) {
          LCDreplies = random(reply);          
         }
         else {
          lcd.setCursor(0, 1);
          lcd.print("Try again");
          delay(cDelay);
          LCDreplies = 0; //go back to the same case
         }
         break; ... .. ..... //repeat for other cases

Basically I have it so that in each case there's a correct answer, the skip button and a wrong answer (else). When its correct or the skip button is pressed then it goes onto the next random case. But if not correct or skip then go back to the same case.

I'm just confused about the booleans. For your code you had checkYesButton() and CheckNoButton() would you be able to explain what's going on here:

bool checkYesButton()
{
  bool temp = false;
  // read the button:
  yesState = digitalRead(yesPin);

  if (yesState == LOW && lastYesState == HIGH)
  {
    Serial.println("New yes ");
    temp = true;
    // Delay a little bit to avoid bouncing
    delay(50);
  }//change
  lastYesState = yesState;
  return temp;
}//yes

bool checkNoButton()
{
  bool temp = false;
  // read the button:
  noState = digitalRead(noPin);

  // compare the buttonState to its previous state
  if (noState == LOW && lastNoState == HIGH)
  {
    Serial.println("New no ");
    temp = true;
    // Delay a little bit to avoid bouncing
    delay(50);
  }//change
  // save the current state as the last state, for next time through the loop
  lastNoState = noState;
  return temp;
}//no

I know that for the skip button it would pretty much be the same as what you have. But what about for my correct button, there's 3 buttons to pick from and only one of them will be correct. Would I just have to create 3 booleans for each button to check the state at each one?

Would I just have to create 3 booleans for each button to check the state at each one?

I'm thinking you mean 1 boolean for each button? Each button is either newly pressed or it's not

But yes, I think that where I have merely yes and no, you would have buttons and bools for answer1, answer2 etc, then in the current case where I look for either yes or no to be true and dive off accordingly, you would look at answer 1, answer2 etc and also the skip button to have been newly pressed.

(The "right" way to do that would be to have the buttons in an array I think, "right" meaning elegant and easily maintainable. But it can of course be done the long way as above. (As soon as you see numbers on vraibale names it's time to think arrays. So answer1, answer2 etc would indicate answer[i] is called for. But that can be another day's discussion.))

Hi

I don't know if your circuit truly reflects how your hardware is wired:-

However I can see a number of issues with it

  1. The standard for circuit diagrams is to have the positive supply rail at the top and the negative/Gnd rail at the bottom. You have reversed this which makes the circuit much more difficult to read.

  2. The photo transistor is reverse biased. The arrow on the emitter shows current flow. The collector has to be at a higher voltage than the emitter for current to flow when light strikes the device. Your circuit would be correct if 1 was corrected.

  3. Pin D13 is wired directly to Gnd. Why? Should it perhaps be wired to the junction between R7 and the Skip switch?

  4. Inputs D8 an D7 are hard-wired to Gnd they will never go high. They should be wired to the junction between R4 and R5 and switches A and B respectively. The other ends of R4 and R5 should be wired to Gnd.

  5. Input D6 is wired to Gnd via R6 and will never go high.

  6. This is the big one, Switch C is wired directly between 5V and Gnd. Closing this will short circuit the Arduino 5V rail possibly damaging the on board regulator (depending on how you are powering up the Arduino).

Ian

jubukraa:
I’m thinking you mean 1 boolean for each button? Each button is either newly pressed or it’s not

But yes, I think that where I have merely yes and no, you would have buttons and bools for answer1, answer2 etc, then in the current case where I look for either yes or no to be true and dive off accordingly, you would look at answer 1, answer2 etc and also the skip button to have been newly pressed.

(The “right” way to do that would be to have the buttons in an array I think, “right” meaning elegant and easily maintainable. But it can of course be done the long way as above. (As soon as you see numbers on vraibale names it’s time to think arrays. So answer1, answer2 etc would indicate answer[i] is called for. But that can be another day’s discussion.))

Here’s ,y mew code:

#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

const int rLEDPin = 10; //Red LED
const int gLEDPin = 9; //Green LED

bool Acorrect = false;
bool Bcorrect = false;
bool Ccorrect = false;
bool skip = false;


bool aSwitchState;
bool prevASwitchState;
bool bSwitchState;
bool prevBSwitchState;
bool cSwitchState;
bool prevCSwitchState;
bool switchState;
bool prevSwitchState;



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);

  //initializing the state of each buttons
  switchState = digitalRead(switchPin);
  aSwitchState = digitalRead(aSwitchPin);
  bSwitchState = digitalRead(bSwitchPin);
  cSwitchState = digitalRead(cSwitchPin);

  int prevASwitchState  = aSwitchState;
  int prevBSwitchState = bSwitchState;
  int prevCSwitchState = bSwitchState;
  int prevSwitchState = switchState;
  
}

void loop() { 
sensorValue = analogRead(sensorPin);
 Serial.println(sensorValue);
 Serial.println(ans);

  Acorrect = checkAButton();
  Bcorrect = checkBButton();
  Ccorrect = checkCButton();
  skip = checkSkipButton();
  LCDreplies();
}

void LCDreplies () {
  switch(reply)
  {
    case 0: //A switch
      lcd.clear(); //clear before starting case
      digitalWrite(gLEDPin,LOW);//Turn off LED in the beginning of case
      digitalWrite(rLEDPin,LOW);
       lcd.print("Press letter A");
         if(Acorrect) {
           lcd.setCursor(0, 1);
           lcd.print("You're AMAZING!");
           delay(cDelay);
           reply = random(reply); //new random case
         }
         if(skip) {
          reply = random(reply);          
         }
         else {
          digitalWrite(rLEDPin,HIGH);
          lcd.setCursor(0, 1); //go to next line
          lcd.print("Try again");
          delay(cDelay);
          reply = 0; //go back to the same case
         }
         break;
    
    case 1: //B switch
        lcd.clear(); 
        digitalWrite(gLEDPin,LOW);
        digitalWrite(rLEDPin,LOW);
        lcd.print("Press letter B");
          if(Bcorrect) {
            lcd.setCursor(0, 1);
            lcd.print("You're BRILLIANT!");
            delay(cDelay);
            reply = random(reply);
          } 
          if(skip) {
           reply = random(reply); 
          }
          else {
          digitalWrite(rLEDPin,HIGH);
          lcd.setCursor(0, 1);
          lcd.print("Try again");
          delay(cDelay);
          reply = 1; 
          }
        break;

    case 2: //C switch
       lcd.clear(); 
       digitalWrite(gLEDPin,LOW);
       digitalWrite(rLEDPin,LOW);
       lcd.print("Press letter C");
         if(Ccorrect) {
           lcd.setCursor(0, 1);
           lcd.print("You're CLEVER!");
           delay(cDelay);
           reply = random(reply); 
         }
         if(skip) {
          reply = random(reply);          
         }
         else {
          digitalWrite(rLEDPin,HIGH);
          lcd.setCursor(0, 1); 
          lcd.print("Try again");
          delay(cDelay);
          reply = 2; 
         }
         break;

    case 3: //  RED 
       lcd.clear(); 
       digitalWrite(gLEDPin,LOW);
       digitalWrite(rLEDPin,LOW);
       lcd.print("Scan color RED");
          if(sensorValue >= 120 && sensorValue < 200) { //roughly calibrated based on the average values sense for each color gel (based on lighting and position)
           lcd.setCursor(0, 1);
           lcd.print("YOU GOT RED!");
           delay(cDelay);
           reply = random(reply); 
         }
         if(skip) {
          reply = random(reply);          
         }
         else {
          lcd.setCursor(0, 1);
          digitalWrite(rLEDPin,HIGH);
          lcd.print("Try again");
          delay(cDelay);
          reply = 3;
         }
         break;
    
    case 4: //  BLUE   
       lcd.clear(); 
       digitalWrite(gLEDPin,LOW);
       digitalWrite(rLEDPin,LOW);
       lcd.print("Scan color BLUE");
          if(sensorValue > 75 && sensorValue < 120) {
           lcd.setCursor(0, 1);
           lcd.print("YOU GOT BLUE!");
           delay(cDelay);
           reply = random(reply); 
         }
         if(skip) {
          reply = random(reply);          
         }
         else {
          digitalWrite(rLEDPin,HIGH);
          lcd.setCursor(0, 1); 
          lcd.print("Try again");
          delay(cDelay);
          reply = 4; 
         }
         break;
  }
}

//CHECKING THE STATES OF EACH BUTTON
bool checkSkipButton()
{
  bool temp = false;
  // read the button:
  switchState = digitalRead(switchPin);

  if (switchState == LOW && prevSwitchState == HIGH)
  {
    Serial.println("New switch ");
    temp = true;
    delay(50);
  }
  prevSwitchState = switchState;
  return temp;
}

bool checkAButton()
{
  bool temp = false;
  // read the button:
  aSwitchState = digitalRead(aSwitchPin);

  if ( aSwitchState == LOW && prevASwitchState == HIGH)
  {
    Serial.println("New A");
    temp = true;
    delay(50);
  }
  prevASwitchState = aSwitchState;
  return temp;
}

bool checkBButton()
{
  bool temp = false;
  // read the button:
  bSwitchState = digitalRead(bSwitchPin);

  if ( bSwitchState == LOW && prevBSwitchState == HIGH)
  {
    Serial.println("New B");
    temp = true;
    delay(50);
  }
  prevBSwitchState = bSwitchState;
  return temp;
}

bool checkCButton()
{
  bool temp = false;
  // read the button:
  cSwitchState = digitalRead(cSwitchPin);

  if ( cSwitchState == LOW && prevCSwitchState == HIGH)
  {
    Serial.println("New C");
    temp = true;
    delay(50);
  }
  prevCSwitchState = cSwitchState;
  return temp;
}

But for some reason before I press any button it moves onto case 0 and goes to the else statement so it prints out try again and the red LED turns on.

How are the buttons wired ?

I have wired each button so that one end is connected to the 5V and the other end is connected to a 10kohm resistor, the ground and a digital pin.

Hi

I don't know if your circuit truly reflects how your hardware is wired:-

However I can see a number of issues with it

  1. The standard for circuit diagrams is to have the positive supply rail at the top and the negative/Gnd rail at the bottom. You have reversed this which makes the circuit much more difficult to read.

  2. The photo transistor is reverse biased. The arrow on the emitter shows current flow. The collector has to be at a higher voltage than the emitter for current to flow when light strikes the device. Your circuit would be correct if 1 was corrected.

  3. Pin D13 is wired directly to Gnd. Why? Should it perhaps be wired to the junction between R7 and the Skip switch?

  4. Inputs D8 an D7 are hard-wired to Gnd they will never go high. They should be wired to the junction between R4 and R5 and switches A and B respectively. The other ends of R4 and R5 should be wired to Gnd.

  5. Input D6 is wired to Gnd via R6 and will never go high.

  6. This is the big one, Switch C is wired directly between 5V and Gnd. Closing this will short circuit the Arduino 5V rail possibly damaging the on board regulator (depending on how you are powering up the Arduino).

Ian

Thank you, I thought the schematic made sense. I don't really know how to show show that its sharing a node. For each switch I have it so that one end is connected to the 5V and the other end is connected to a 10kohm resistor, the ground and its digital pin.

Your switches must be wired properly !

Use one of the three examples below.

No !

cyrawr:
Thank you, I thought the schematic made sense. I don't really know how to show show that its sharing a node. For each switch I have it so that one end is connected to the 5V and the other end is connected to a 10kohm resistor, the ground and its digital pin.

Not quite right I'm afraid. You currently show all four switches wired directly across 5V and Gnd. Not a good idea.

Pin 1 of each switch should be wired to 5V pin 2 should go to pin 1 of the relevant resistor (R2, R3, R4 and R5) and to it's respective Arduino pin (D7, D8, D9 and D13). Pin 2 of each resistor should go to Gnd.

e.g. Pin 1 of switch A goes to 5V, Pin 2 of switch A goes to R2 pin 1 and Arduino D7. Pin 2 of R2 goes to Gnd.

Hope this helps

Ian

OP

Please DO NOT make major edits of previous posts.

jubukraa:
In the sketch I linked earlier, you can get some clues to do that. In each case of the switch…case it asks a question, and the pressing of the Yes or No button takes you to some other case. That other case is the next question (if answer is correct) or the “bad luck” message if you got the answer wrong. If you get the answer correct in the last question, you get the “well done” message.

This works, but then I run into an issue of half of the LCD seems to be flickering. Then as soon as I press a button the screen doesn’t flicker. Would anyone be able to tell me why that happens?

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int switchPin = 13; //skip button
//int switchState = 0;
int reply = random(0,4);
int prevreply;
const int sensorPin = A0; //phototransistor
int sensorValue;
int cDelay = 1000; //1 Sec delay
int ans = 0;
const int aPin = 6; //A
const int bPin = 7; //B
const int cPin = 8; //C

const int rLEDPin = 10; //Red LED
const int gLEDPin = 9; //Green LED

bool Acorrect = false;
bool Bcorrect = false;
bool Ccorrect = false;
bool skip = false;


bool aState;
bool prevaState;
bool bState;
bool prevbState;
bool cState;
bool prevcState;
bool switchState;
bool prevSwitchState;



void setup() {
  lcd.begin(16, 2);
  pinMode(switchPin,INPUT);
  pinMode(aPin, INPUT);
  pinMode(bPin, INPUT);
  pinMode(cPin, INPUT);
  pinMode(rLEDPin, OUTPUT);
  pinMode(gLEDPin, OUTPUT);
  lcd.print("Let's Play!");
  lcd.setCursor(0, 1);
  lcd.print("LETTERS & COLORS");
  delay(4000);
  Serial.begin(9600);

  //initializing the state of each buttons
  switchState = digitalRead(switchPin);
  aState = digitalRead(aPin);
  bState = digitalRead(bPin);
  cState = digitalRead(cPin);

  int prevaState  = aState;
  int prevbState = bState;
  int prevcState = bState;
  int prevSwitchState = switchState;
 
}

void loop() {
 sensorValue = analogRead(sensorPin);
 Serial.println(sensorValue);
 Serial.println(ans);

  Acorrect = checkAButton();
  Bcorrect = checkBButton();
  Ccorrect = checkCButton();
  skip = checkSkipButton();
  LCDreplies();
  
}

void LCDreplies () {
       lcd.clear();
       digitalWrite(gLEDPin,LOW);
       digitalWrite(rLEDPin,LOW);
  switch(reply)
  {
    case 0: //A switch
       lcd.print("Press letter A");
         if(Acorrect) {
           digitalWrite(gLEDPin,HIGH);
           lcd.setCursor(0, 1);
           lcd.print("You're AMAZING!");
           delay(cDelay);
           reply = random(0,4); //new random case
         }
         if(skip) {
          reply = random(0,4);          
         }
         if(Bcorrect || Ccorrect) {
          digitalWrite(rLEDPin,HIGH);
          lcd.setCursor(0, 1); //go to next line
          lcd.print("Try again");
          delay(cDelay);
          reply = 0; //go back to the same case
         }
         break;
   
    case 1: //B switch
        lcd.print("Press letter B");
          if(Bcorrect) {
            digitalWrite(gLEDPin,HIGH);
            lcd.setCursor(0, 1);
            lcd.print("You're BRILLIANT!");
            delay(cDelay);
            reply = random(0,4);
          }
          if(skip) {
           reply = random(0,4);
          }
          if(Acorrect || Ccorrect) {
          digitalWrite(rLEDPin,HIGH);
          lcd.setCursor(0, 1);
          lcd.print("Try again");
          delay(cDelay);
          reply = 1;
          }
        break;

    case 2: //C switch
       lcd.print("Press letter C");
         if(Ccorrect) {
           digitalWrite(gLEDPin,HIGH);
           lcd.setCursor(0, 1);
           lcd.print("You're CLEVER!");
           delay(cDelay);
           reply = random(0,4);
         }
         if(skip) {
          reply = random(0,4);         
         }
         if(Acorrect || Bcorrect) {
          digitalWrite(rLEDPin,HIGH);
          lcd.setCursor(0, 1);
          lcd.print("Try again");
          delay(cDelay);
          reply = 2;
         }
         break;

    case 3: //  RED
       lcd.print("Scan color RED");
          if(sensorValue >= 120 && sensorValue < 250) { //roughly calibrated based on the average values sense for each color gel (based on lighting and position)
           digitalWrite(gLEDPin,HIGH);
           lcd.setCursor(0, 1);
           lcd.print("YOU GOT RED!");
           delay(cDelay);
           reply = random(0,4);
         }
         if(skip) {
          reply = random(0,4);          
         }
         if(sensorValue < 120) {
          lcd.setCursor(0, 1);
          digitalWrite(rLEDPin,HIGH);
          lcd.print("Try again");
          delay(cDelay);
          reply = 3;
         }
         break;
   
    case 4: //  BLUE
       lcd.print("Scan color BLUE");
       //delay(cDelay);
          if(sensorValue < 120) {
           digitalWrite(gLEDPin,HIGH);
           lcd.setCursor(0, 1);
           lcd.print("YOU GOT BLUE!");
           delay(cDelay);
           reply = random(0,4);
         }
         if(skip) {
          reply = random(0,4);         
         }
         if(sensorValue >= 120 && sensorValue < 250) {
          digitalWrite(rLEDPin,HIGH);
          lcd.setCursor(0, 1);
          lcd.print("Try again");
          delay(cDelay);
          reply = 4;
         }
         break;
  }
}

//CHECKING THE STATES OF EACH BUTTON
bool checkSkipButton()
{
  bool temp = false;
  // read the button:
  switchState = digitalRead(switchPin);

  if (switchState == LOW && prevSwitchState == HIGH)
  {
    Serial.println("New switch ");
    temp = true;
    delay(50);
  }
  prevSwitchState = switchState;
  return temp;
}

bool checkAButton()
{
  bool temp = false;
  // read the button:
  aState = digitalRead(aPin);

  if ( aState == LOW && prevaState == HIGH)
  {
    Serial.println("New A");
    temp = true;
    delay(50);
  }
  prevaState = aState;
  return temp;
}

bool checkBButton()
{
  bool temp = false;
  // read the button:
  bState = digitalRead(bPin);

  if ( bState == LOW && prevbState == HIGH)
  {
    Serial.println("New B");
    temp = true;
    delay(50);
  }
  prevbState = bState;
  return temp;
}

bool checkCButton()
{
  bool temp = false;
  // read the button:
  cState = digitalRead(cPin);

  if ( cState == LOW && prevcState == HIGH)
  {
    Serial.println("New C");
    temp = true;
    delay(50);
  }
  prevcState = cState;
  return temp;
}

Wow , that's an odd one. I was going to say it was due to the update rate, until I saw the pic. I thought you meant top half vs bottom half, perhaps one line static one updating, not side to side.

That looks to me more like an electrical connection (or lack of) thing.... That when you press the button the breadboard's flexing or something?

If I have a chance (just back from 5k run (ok, walk....) and I am reliably informed I have garden chores) I'll set an Uno up like that and run the code. Not sure when though...

void LCDreplies ()
{
lcd.clear(); <-----<<<<
. . .

You should not be clearing the LCD all the time.

Only change items on the screen when needed otherwise your screen will flicker.

Maybe once every 500ms, then only the things that change.