How to make a trivia code with four push buttons and switch..case

I have create a trivia where the user must answer 5 questions by either pressing button A, B, or C. I can't figure out how to save the questions in my program. I tried using switch case, but it didnt work. For the sake of this project, the program is supposed to be simple. for now I only have two buttons (A and B).
What am I missing?
here is the code:

#include <stdio.h>
int startbuttonState = 0;
int buttonAstate = 0;
int buttonBstate=0;

int lastButtonState = 0;

int buttonPushCounter = 0;
void setup()
{
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);

Serial.begin(9600);

pinMode(13, OUTPUT);
Serial.println("Press start button to begin");
}

void loop() {
startbuttonState = digitalRead(2);
buttonAstate = digitalRead(3);
buttonBstate = digitalRead(4);
int questioncount=0;
if (startbuttonState==HIGH){
switch(questioncount) {
case 0:
Serial.println("What is a baby dog called?");
Serial.println("A)Puppy B)Kitten C)Cub D)Fawn");
delay (400);
if (buttonAstate=HIGH){
Serial.println("CORRECT");
questioncount=1;}
else if (buttonBstate==HIGH){
Serial.println("WRONG WRONG WRONG");
questioncount=0;}
delay(400);
break;
case 1:
Serial.println("What is a baby cat called?");
Serial.println("A)Puppy B)Kitten C)Cub D)Fawn");
delay (400);
if (buttonBstate=HIGH){
Serial.println("CORRECT");
questioncount=2;}
else if (buttonAstate==HIGH){
Serial.println("WRONG WRONG WRONG");
questioncount=0;}
delay(400);
break;
}
}
}

Hello
Are you familar to use the array() and struct() instructions?
You can organize the questions and answers in such data structures.
Try it!

You need to understand that after asking the question, you must wait until a button is pressed.

Waiting is not "delay()"! You do not know how long it will take for a button to be pressed, and you must be waiting for any button, which is to say you must check every button until one is pressed, and only then decide if it is the correct button or the wrong one and act accordingly.

You may of course, arrange a "timeout" at which no button pressed will be considered as wrong, but you cannot just determine a time and see if the button was pressed at the end of that time as it may have been (correctly or incorrectly) pressed within that time but released again before you check.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
https://forum.arduino.cc/index.php?topic=712198.0
Then look down to "code problems" about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What model Arduino are you using?

Can you post a circuit diagram please, showing how you have wired your switches?

Thanks.. Tom... :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.