How to code a yes/no quiz

Hello! I am trying to code a quiz with 5 questions that will be answered using a yes button or no button. If a question is answered correctly, it will proceed to the next question; if it’s answered incorrectly, the quiz ends. I have no experience whatsoever with this so I can’t get it to work. As of now, only the first question works.

The screen is an LCD1602
Here are the 5 questions with the “correct” answers to successfully complete the quiz. If the question is answered alternatively, the quiz should end. :

  1. Do you want a snack right now? YES
  2. Has it been >2 hrs since your last meal? YES
  3. Is your next meal soon? NO
  4. Is it after 9 pm? NO
  5. Have u already had >1 snack today? NO

PLEASE HELP!!!

quiz.ino (969 Bytes)

If you post your code as described in the how to use this forum sticky more members will be able to see it.

Perhaps you code that quiz on your PC first. Read the questions and answers from a file, one by one, wait for keyboard input and proceed as appropriate. If that works, go to an Arduino and do the same with less support by an OS.

In general you want to implement a state machine that advances on a correct answer. The reaction on a wrong answer is up to you, maybe counting errors or abort and restart.

DrDiettrich:
In general you want to implement a state machine that advances on a correct answer

... and you can read about them here, here, or here.

Here is the Arduino reference on switch...case, which is a useful coding technique when "if" gets a bit cumbersome. This is how to poll for a button to change state.

I see btw you are using state change detect, so disregard the last part of my previous (I finally got round to looking at your code.)

The way I did it, was to have 7 states numbered 0 to 6; 0 to 4 to house the questions, and 5 & 6 to say yes have a snack or no soup for you. Each state is a case in a switch..case.

To save having do the button state vs previous state comparison thing over and over, I put those two tests in two functions at the top of loop(), setting a boolean yes as true if yes pressed (else false) and a boolean no as true if no pressed (else false) .

Then in each of the states 0-4 ask the question, and go to whatever next state is appropriate, depending on which of yes or no was true and what the right answer was for that question. (While neither yes nor no is true, it will just lurk in the current state with the question on the lcd.)

In each of states 5 & 6 I just showed an appropriate message for a second and then reverted to state 0 for a new round of questions.

Here, should you want it OP, is my code. It’s a “working solution” so you may or may not want to read further depending on how much more experimenting you want to do.

But I’m attaching it rather than putting inline in case you don’t want to “peek”. That said, I’m not for a moment suggesting it’s a perfect way to do this. YMMV.

I use a different LCD library, so you’ll need to put yours into my code.

_682274_quiz_v2.ino (6.22 KB)

jubukraa:
Here, should you want it OP, is my code. It's a "working solution" so you may or may not want to read further depending on how much more experimenting you want to do.

But I'm attaching it rather than putting inline in case you don't want to "peek". That said, I'm not for a moment suggesting it's a perfect way to do this. YMMV.

I use a different LCD library, so you'll need to put yours into my code.

I keep trying to open it but Arduino gives me an error message and says "cannot create sketch."

Hello, I’ve tried to get help with this on a previous forum post but still can’t figure it out. I’m very inexperienced so at this point, it would be amazing if someone could just write an example of the correct way to code it (even if it’s not all 5 questions) so that I can work from that with copy/paste. I will attach the code I have so far below.

Here is my original question:

I am trying to code a quiz with 5 questions that will be answered using a yes button or no button. If a question is answered correctly, it will proceed to the next question; if it’s answered incorrectly, the quiz ends. I have no experience whatsoever with this so I can’t get it to work. As of now, only the first question can be answered then it doesn’t advance any further.

I have an Arduino one and the screen is an LCD1602
Here are the 5 questions with the “correct” answers to successfully complete the quiz. If the question is answered alternatively, the quiz should end. :

  1. Do you want a snack right now? YES
  2. Has it been >2 hrs since your last meal? YES
  3. Is your next meal soon? NO
  4. Is it after 9 pm? NO
  5. Have u already had >1 snack today? NO

PLEASE HELP!!! Thank you so much in advance.

#include <LiquidCrystal.h>

int buttonPin8= 8;
int buttonPin9 = 9;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int prevButtonState8;
int prevButtonState9;
void setup()
{
 pinMode(buttonPin8, INPUT);
 pinMode(buttonPin9, INPUT);
 digitalWrite(buttonPin8, LOW);
 digitalWrite(buttonPin9,LOW);
 lcd.begin(16, 2);
 lcd.setCursor(0,0);
 lcd.print("DO YOU WANT A ");
 lcd.setCursor(0,1);
 lcd.print("SNACK RIGHT NOW?");
}


void loop(){
 int buttonState8 = digitalRead(buttonPin8);
 int buttonState9 = digitalRead(buttonPin9);

 if (buttonState8 == LOW && prevButtonState8== HIGH){
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("WAS UR LAST MEAL");
   lcd.setCursor(0,1);
   lcd.print(">2 HOURS AGO?");
   prevButtonState8 = buttonState8;
 }
 else if(buttonState9 == LOW && prevButtonState9== HIGH){
   lcd.clear();
   lcd.print("CONSULT ME LATER");
   prevButtonState9 = buttonState9;
 }
 else{
  prevButtonState8 = buttonState8;
  prevButtonState9 = buttonState9;
 }
}

Please don't double post - it just wastes everyone's time. I have suggested to the Moderator to merge with your other Thread where you already have Replies

...R

Duplicate topics merged

Look into using a ‘state machine’.

Switches should be handled by ‘state change’ not ‘switch level’.

hannah35358:
I keep trying to open it but Arduino gives me an error message and says “cannot create sketch.”

I don’t recall ever seeing that message, and I get it too when I try to download it back again, but it opens in notepad, so the file’s not corrupt. Weird.

Anyway here’s the sketch for you just to copy and paste out of here into the ide. (I only attached it originally rather than do it this way so you wouldn’t be tempted to “peek” if you were still keen to try further yourself.) I have copied the below back out again and recompiled to check there’s no rubbish in the code.

Don’t forget to put your LCD stuff into my code instead of my lines (the library include stuff and the begin), since I use a different library.

And my code uses input pullup: buttons need to be wired from their respective pins to ground, not to 5V, with no external resistors.

Also read the links I gave in one of the early posts of the original thread.

// https://forum.arduino.cc/index.php?topic=682274.0
// 5 may 2020

/*
  1. Do you want a snack right now?  YES
  2. Has it been >2 hrs since your last meal? YES
  3. Is your next meal soon? NO
  4. Is it after 9 pm? NO
  5. Have u already had >1 snack today? NO
*/

//using different lcd library from OP
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

//states
enum {ST_q1, ST_q2, ST_q3, ST_q4, ST_q5, ST_haveASnack, ST_noSoupAForAYou} currentQuestion = ST_q1;
// ala https://www.gammon.com.au/statemachine

bool yes = false;
bool no = false;

const byte yesPin = 8;
bool yesState;
bool lastYesState;
const byte noPin = 9;
bool noState;
bool lastNoState;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("setup() ... ");
  Serial.println("*** snack quiz ***");
  Serial.print("Compiler: ");
  Serial.print(__VERSION__);
  Serial.print(", Arduino IDE: ");
  Serial.println(ARDUINO);
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(yesPin, INPUT_PULLUP);
  pinMode(noPin, INPUT_PULLUP);
  //initialize button states
  yesState = digitalRead(yesPin);
  lastYesState = yesState;
  noState = digitalRead(noPin);
  lastNoState = noState;

  lcd.begin(LCD_COLS, LCD_ROWS);
  lcd.setCursor(0, 0);
  lcd.print("Snack...");
  lcd.setCursor(0, 1);
  lcd.print("        ...quiz");
  delay(1000);
  Serial.println("setup() done");
  Serial.println(" ");
}

void loop()
{
  yes = checkYesButton();
  no = checkNoButton();
  askQuestions();
} //loop

void askQuestions()
{
  switch (currentQuestion) //ST_q1, ST_q2, ST_q3, ST_q4, ST_q5, ST_haveASnack, ST_noSoupAForAYou
  {
    case ST_q1:
      lcd.setCursor(0, 0);
      lcd.print("DO YOU WANT A   ");
      lcd.setCursor(0, 1);
      lcd.print("SNACK RIGHT NOW?");
      //for this question, YES is correct answer to advance
      if (yes) //this would only have been returned true by checkYesButton() if the yes button was pressed
      {
        currentQuestion = ST_q2;
        yes = false;
      }

      if (no) //this would only have been returned true by checkNoButton() if the no button was pressed
      {
        currentQuestion = ST_noSoupAForAYou;
        no = false;
      }

      break;

    case ST_q2:
      lcd.setCursor(0, 0);
      lcd.print("WAS UR LAST MEAL");
      lcd.setCursor(0, 1);
      lcd.print(">2 HOURS AGO?   ");
      //for this question, YES is correct answer to advance
      if (yes) //this would only have been returned true by checkYesButton() if the yes button was pressed
      {
        currentQuestion = ST_q3;
        yes = false;
      }

      if (no) //this would only have been returned true by checkNoButton() if the no button was pressed
      {
        currentQuestion = ST_noSoupAForAYou;
        no = false;
      }
      break;

    case ST_q3:
      lcd.setCursor(0, 0);
      lcd.print("IS YOUR NEXT    ");
      lcd.setCursor(0, 1);
      lcd.print("      MEAL SOON?");
      //for this question, NO is correct answer to advance
      if (no) //this would only have been returned true by checkNoButton() if the no button was pressed
      {
        currentQuestion = ST_q4;
        no = false;
      }

      if (yes) //this would only have been returned true by checkYesButton() if the yes button was pressed
      {
        currentQuestion = ST_noSoupAForAYou;
        yes = false;
      }
      break;

    case ST_q4:
      lcd.setCursor(0, 0);
      lcd.print("IS IT           ");
      lcd.setCursor(0, 1);
      lcd.print("      AFTER 9PM?");
      //for this question, NO is correct answer to advance
      if (no) //this would only have been returned true by checkNoButton() if the no button was pressed
      {
        currentQuestion = ST_q5;
        no = false;
      }

      if (yes) //this would only have been returned true by checkYesButton() if the yes button was pressed
      {
        currentQuestion = ST_noSoupAForAYou;
        yes = false;
      }
      break;

    case ST_q5:
      lcd.setCursor(0, 0);
      lcd.print("HAVE YOU HAD    ");
      lcd.setCursor(0, 1);
      lcd.print(" >1 SNACK TODAY?");
      //for this question, NO is correct answer to advance
      if (no) //this would only have been returned true by checkNoButton() if the no button was pressed
      {
        currentQuestion = ST_haveASnack;
        no = false;
      }

      if (yes) //this would only have been returned true by checkYesButton() if the yes button was pressed
      {
        currentQuestion = ST_noSoupAForAYou;
        yes = false;
      }
      break;

    case ST_haveASnack:
      lcd.setCursor(0, 0);
      lcd.print("CONGRATULATIONS!");
      lcd.setCursor(0, 1);
      lcd.print("  HAVE A SNACK! ");
      delay(1000);
      currentQuestion = ST_q1;

      break;

    case ST_noSoupAForAYou:
      lcd.setCursor(0, 0);
      lcd.print("No soupa...     ");
      lcd.setCursor(0, 1);
      lcd.print("     ...fora you");
      delay(1000);
      currentQuestion = ST_q1;

      break;
  }//switch
}//askQuestions

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