Printing question only once in loop. Then printing answer once, then continue

Hello,

Im looking at a problem here.

I want to make a couple of questions, which you can anspwer with 2 pushbuttons.

Now i want to print the question only once, then wait for one pushbutton to be pressed, after the pushbutton a response have to appear, with 1) CORRECT! and 2)Sorry, wrong answer. In both cases they have to continue to question#2, so no second try.

Im having a hard time finding this because it is either the question which print continiously or the response printing continiously.

void loop()
{
      switchState1 = digitalRead(7);
      switchState2 = digitalRead(8);

      if(i == 1){
      Serial.print("Vraag #");  Serial.print(0);Serial.print(": ");
      serialLine_Print(questions[0].q);
      Serial.println("Possible answers:");
      Serial.print("1: ");serialLine_Print(questions[0].answer1);
      Serial.print("2: ");serialLine_Print(questions[0].answer2); 
      Serial.print(i); 
      i=2;
      }
      
        if(switchState1 != previousswitchState1){
          Serial.println("CORRECT!");
          Serial.println("Thijs wandeld naar school");
          i=2;
          previousswitchState1 = switchState1;
        }
        else if(switchState2 != previousswitchState2){
          Serial.println("Sorry, wrong answer");
          i=2
          previousswitchState2 = switchState2;
        }
    
}

I only posted the loop. I am working in structs. When i == 3 the program will go to the next question.

Add a boolean variable to your program and set it to true and print only when the boolean is true

if (questionPrinted == true)
  {
      Serial.print("Vraag #");  Serial.print(0);Serial.print(": ");
      serialLine_Print(questions[0].q);
      Serial.println("Possible answers:");
      Serial.print("1: ");serialLine_Print(questions[0].answer1);
      Serial.print("2: ");serialLine_Print(questions[0].answer2); 
      Serial.print(i); 
      questionPrinted = false;
  }

Thanks. It works, but to a certain level.
i named the boolean value, and i set it true.
i got:

      if(question==1 && value == true){
      Serial.print("Vraag #");  Serial.print(1);Serial.print(": ");
      serialLine_Print(questions[0].q);
      Serial.println("mogelijke antwoorden:");
      Serial.print("1: ");serialLine_Print(questions[0].antwoord1);
      Serial.print("2: ");serialLine_Print(questions[0].antwoord2);  
      value = false;
      }
      
          if(switchState1 != previousswitchState1)               //THIS LINE
          if(switchState1 == HIGH){
          Serial.println("GOOD! Robin is walking to school.");
          Serial.println();
          question = 2;
          value = true;
          previousswitchState1 = switchState1;
          delay(1000);
          }
        }
        
        else if(switchState2 != previousswitchState2){
          if(switchState2 == HIGH){
          Serial.println("Wrong. Next question");
          Serial.println();
          question = 2;
          value = true;
          previousswitchState2 = switchState2;
          delay(1000);
          }
        }
      
    
      if(question==2 && value == true){
      Serial.print("Vraag #");  Serial.print(2);Serial.print(": ");
      serialLine_Print(questions[1].q);
      Serial.println("possible answers");
      Serial.print("1: ");serialLine_Print(questions[1].antwoord1);
      Serial.print("2: ");serialLine_Print(questions[1].antwoord2);  
      value = false;
      previousswitchState1 = 0;
      previousswitchState2 = 0;
      }
      
          if(switchState1 != previousswitchState1){      //AND THIS LINE
          if(switchState1 == HIGH){
          Serial.println("Well done, lisa likes icecream very much");
          question=3;
          value = true;
          previousswitchState1 = switchState1;
          }
        }
        else if(switchState2 != previousswitchState2){
          if(switchState2 == HIGH){
          Serial.println("Wrong. Next question");
          question=3;
          value = true;
          previousswitchState2 = switchState2;
          }
        }

The problem is, when answering the first question, i have to press button 1 or button 2. Then the program will continue to question to, but also directly print "Well done, lisa likes icecream very much". a different placing of the curly brackets doesnt matter. It is because the 2 conditions are in the same loop. (See //THIS LINE & // AND THIS LINE, in the code)
If you would like the full code please let me know.

You don't seem to understand that you are trying to force an asynchronous process into a synchronous mold.

You print a question. You check for an answer. But, you don't check that the answer is in response to a particular question.

If you REALLY want to wait for an answer, do that.

Write two functions, askQuestion() and waitForAnswer(). The askQuestion() function should take an argument - which question to ask. The waitForAnswer() function should return the answer to that question. THEN you can sync the answers to the questions.

There is a simple user input example in Planning and Implementing a Program

And, if that does not help you will need to post your complete program - preferably the shortest possible complete program that illustrates the problem.

...R

I can't immediately see the problem but I suspect that it has to do with the switchState and previousswitchState variables. Print their values before any tests involving them. Are they what you expect ?

The problem is that "THIS LINE, AND THIS LINE" are in the same loop. They are both directly under void loop(). So when i press the button during question #1, the "AND THIS LINE" is also true so the compiler executes both lines.

Killerpirate:
The problem is that "THIS LINE, AND THIS LINE" are in the same loop. They are both directly under void loop(). So when i press the button during question #1, the "AND THIS LINE" is also true so the compiler executes both lines.

That is true. So, don't print the "that answer is right" message for question 2 when you asked question 1.

PaulS:
You don't seem to understand that you are trying to force an asynchronous process into a synchronous mold.

You print a question. You check for an answer. But, you don't check that the answer is in response to a particular question.

Thanks for your request.I understand what you are saying and i agree. But im not sure how you want me to implement this into an function. Can you give me a small start?

switch/case state machine?

  1. print question #1, advance to state 2
  2. wait for input. Once input given;
    print 'correct'
    else
    print 'wrong'
    advance to state 3
  3. print question #2, advance to state 4
  4. wait for input
    etc.

If I ask you a question, you expect that I'll wait for a response, don't you?

I really don't understand what you are trying to do. Asking questions and waiting for answers is a task for more suited for a PC application.

byte questionNumber = 0;

void loop()
{
   askQuestion(questionNumber++);
   byte answer = waitForAnswer();
   bool gotItRight = checkAnswer(questionNumber, answer);
   if(!gotItRignt)
   {
      // What do you want to do if the answer was wrong?
      // Ask the question again? Count the number of wrong answers?
   }
}

You can, I hope, see how asking a question is an action that depends only on picking the correct string of text to show.

Waiting for an answer is rather trivial - a while statement that is exited when one of two switches is pressed. Returning the switch number (the answer) is easy.

Checking that the answer is correct only requires looking an the nth element of an array. The answer supplied is either correct, or it isn't, so returning true or false is trivial. The question number defines the array element to look at.

What you do with a correct answer, or a wrong answer is up to you.

Try writing the 3 functions yourself. That are not hard.