Quiz with 3 possible answers, buttons don't work

Hey everyone,

I'm an absolute noob to this. I am making a quiz for one player, and the questions have 3 possible answers, meaning I have 3 buttons. I have the code for two buttons and it's working properly, but now I'd like to modify this code for 3 buttons. I think the problem is within the function that checks whether an answer is correct, which you can see below:

 boolean correctAnswer = false;
    
    // this loop will wait for a button to be pressed
    while(!aButton && !bButton &&!cButton){
    
      //read the buttons
      aButton = digitalRead(2);
      bButton = digitalRead(3);
      cButton = digitalRead(4);
    
   
      if(aButton && answers[answerNumber] == 1) {
        correctAnswers++;
        answerCorrect = true;
      } 

    if(bButton && answers[answerNumber] == 2) {
        correctAnswers++;
        answerCorrect = true;
      } 

      
    if(cButton&& answers[answerNumber] == 3) {
        correctAnswers++;
        answerCorrect = true;
      }

The problem is that when running the code, it's completely ignoring the cButton and bButton. The aButton works fine, it responds to the correct answers (so if the given answer is equal to 1, the serial monitor will say it's correct, and if it is not equal to 1, the serial monitor will say it's false). But the bButton and cButton are always false, no matter what. This is one of the questions with the number of the answer (button) that should be correct:

   Serial.println(questions[2]); //this is one of the questions, part of an array


  delay(1000);
  
  //here it's going to wait for an answer
  if (checkAnswer(2)) {
    Serial.println("Correct");
  } else {
    Serial.println("Not correct");
  }

As you can see with the (checkAnswer(2)), I should get a correct answer if I'd press the 2nd button, right? But I'm not getting it. It's false, no matter what (pressing the other buttons will give me a false answer too). If I'd write (checkAnswer(1)), however, the answer would be correct when pressing the first button.

So what should I do? This code used to work like a charm with 2 buttons, but now that I've added a third one, it completely stopped working (except for with the first button). It's probably a stupid question, I'm sorry. Oh, and is there a way to fix this without having to completely change my code? So still by using if-else statements? That's pretty much the only thing I understand about Arduino, so... :slight_smile: thanks in advance!

Please supply the full code rather than snippets.

Weedpharma

Thanks for your quick response! Here's the full code:

//questions
String questions[5] = {"question 1",
                    "question 2",
                    "question 3",
                    "question 4",
                    "question 5"};
                    
// boolean with the correct answers                
boolean  answers[5] = {1,2,3,2,1};
              
//variables
int start = 0;  //this is just a start button, works fine
int aButton = 0; 
int bButton = 0; 
int cButton = 0;
int correctAnswers = 0;






void setup() {
 

pinMode(2,INPUT); //aButton
pinMode(3,INPUT); //bButton
pinMode(4,INPUT); //cButton
pinMode(5,INPUT); //start

  Serial.begin(9600);
  
}

void loop() {
  
  //waits for a button to be pressed
  while(!aButton && !start){
    Serial.println("checking button");
    aButton = digitalRead(2);
    bButton = digitalRead(3);
    cButton = digitalRead(4);
    start = digitalRead(5);


    
  }


  
  //first question

  Serial.println(questions[0]);

  
  delay(1000);
  
  //checking the answer
  if (checkAnswer(1)) {
    Serial.println("correct");
  } else {
    Serial.println("incorrect");
  }
  

  delay(3000);


  Serial.println(questions[1]);

  delay(1000);
  
  //checking answer
  if (checkAnswer(2)) {
    Serial.println("correct");
  } else {
    Serial.println("incorrect");
  }
  

  delay(3000);

   Serial.println(questions[2]);

  delay(1000);
  
  //checking answer
  if (checkAnswer(3)) {
    Serial.println("correct");
  } else {
    Serial.println("incorrect");
  }
  

  delay(3000);



  Serial.println(questions[3]);

  

  delay(1000);
  
  //checking answer
  if (checkAntwoord(2)) {
    Serial.println("correct");
  } else {
    Serial.println("incorrect");
  }
  

  delay(3000);

 
  
  Serial.println(questions[4]);

  delay(1000);
  
  //check answer
  if (checkAnswer(1)) {
    Serial.println("correct");
  } else {
    Serial.println("incorrect");
  }
  
 
  delay(3000);
  
  Serial.println("done");
  Serial.println("you have");
  Serial.println(correctAnswers);
  Serial.println("correct answers");
  if (correctAnswers >2) {
      digitalWrite(9, HIGH);
      delay(4000);
      digitalWrite(9, LOW);
      Serial.println("yaaay you won");
  }
  else {
    Serial.println("noooo you lost");
  }

} 




//function comparing the given answer to the correct answer

 boolean correctAnswer = false;
    
    // this loop will wait for a button to be pressed
    while(!aButton && !bButton &&!cButton){
    
      //read the buttons
      aButton = digitalRead(2);
      bButton = digitalRead(3);
      cButton = digitalRead(4);
    
   
      if(aButton && answers[answerNumber] == 1) {
        correctAnswers++;
        answerCorrect = true;
      } 

    if(bButton && answers[answerNumber] == 2) {
        correctAnswers++;
        answerCorrect = true;
      } 

      
    if(cButton&& answers[answerNumber] == 3) {
        correctAnswers++;
        answerCorrect = true;
      } 

}
    
    //stuur terug naar het basisprogramma of het antwoord goed was of niet 
    return answerCorrect;


}

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got your buttons wired,
If your buttons pull the digital input HIGH, have you got 10K resistors connected from the digital inputs to GND to pull the digital input LOW when the button is open?
You cannot leave an input pin open circuit if you are using its input.

Hope this helps.. Tom... :slight_smile:

I strongly suggest you get rid of ALL the delay()s from your program and use millis() to manage the timing without blocking. This is illustrated in Several Things at a Time

The code you posted will not compile. There is no checkAnswer() function nor is there a checkAntwoord() function. Also the code at the bottom (which may be intended to be in the function) has mixed up variable names.

...R

pinMode(2,INPUT); //aButton
pinMode(3,INPUT); //bButton
pinMode(4,INPUT); //cButton

You are operating the buttons as INPUT and not as INPUT_PULLUP, so may I ask about the buttons circuit: Are you using external pull-up or external pull-down resistors with the buttons?

Besides of that: I think you want to react on a pressed button, so you should not watch out for a special button state but for a "button state change detection".

Perhaps in the Arduino-IDE work through the example (select from the IDE main menu):

File - Examples - 02.Digital - StateChangeDetection