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...
thanks in advance!