How to display on serial monitor? (Questions and Answer Input)

void setup()                
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  Serial.println("Welcome to the F1 driving simulator");
  delay(1000);
  Serial.println("You will need to answer 5 question in order to proceed");
  delay(1000);
  Serial.println("Answer 3/5 questions correctly to play the game");
  delay(1000);
  Serial.println("Press 1 or 2 to choose the set of questions");
  delay(1000);
  Serial.println("------------------------------------------------------");
  delay(1000);
  Serial.println("");
}

void loop()                      
{
 if (Serial.available()) 
 {
  char set = Serial.read();
  Serial.println("First Set");
  Serial.println("");
  delay(2000);
  
  if(set == '1')
    {
      Serial.println("The charge of an electron is");
      Serial.println("A) 9.21 x 10^-37 kg");
      Serial.println("B) 1.759 x 10^11 C/kg");
      Serial.println("C) 1.6 x 10^-19 C");
      Serial.println("D) 1.6 x 10^-19 J");
      delay(2000); 
     
      char var = Serial.read();
      
      if(var == 'c')
      {
        Serial.println("Right answer!");
      }
      else
      {
        Serial.println("Wrong answer!");
      }
    }   
  
  else if(set == '2')
    {
    //do that 
    }
  }
}

This code works halfway, can't input the answers.

How to proceed from here? To input either "a", "b", "c" or "d" as answers for the next step

This code works halfway, can't input the answers.

What does that mean?

You're checking to see if there's at least one character to read, but reading two.

Don't do that.

If you go beyond a couple of questions the way you are organizing your program will lead to code that is extremely difficult to follow and to debug.

As I see it there are two separate processes

getTheQuestionNumber() and askTheQuestion()

I suggest you organize the code like this

void loop() {
  getTheQuestionNumber();
  if (qNumber > 0) {
      askTheQuestion(qNumber);
  }
}

void getTheQuestionNumber() {
   // code to get a question number and save it in qNumber
   // put 0 in qNumber if there is no response or an invalid number
}

void askTheQuestion( byte quesNum ) {
   switch (quesNum) {
      case 1:
           questionA();
           break;
       case 2:
           questionB();
           break;
  }
}

void questionA() {
  // display question and options
  correctAnswer = 'x';
  getResponse(correctAnswer);

}

void getResponse( char okAns ) {
   // wait for answer and save it in qResp
    if (qResp == okAns) {
         // hooray
     else {
        // boo
     }
}

...R

Robin2: If you go beyond a couple of questions the way you are organizing your program will lead to code that is extremely difficult to follow and to debug.

As I see it there are two separate processes

getTheQuestionNumber() and askTheQuestion()

I suggest you organize the code like this

void loop() {
  getTheQuestionNumber();
  if (qNumber > 0) {
      askTheQuestion(qNumber);
  }
}

void getTheQuestionNumber() {   // code to get a question number and save it in qNumber   // put 0 in qNumber if there is no response or an invalid number }

void askTheQuestion( byte quesNum ) {   switch (quesNum) {       case 1:           questionA();           break;       case 2:           questionB();           break;   } }

void questionA() {   // display question and options   correctAnswer = 'x';   getResponse(correctAnswer);

}

void getResponse( char okAns ) {   // wait for answer and save it in qResp     if (qResp == okAns) {         // hooray     else {         // boo     } }




...R

Hi there,

I did try to work out on the program code you shown me, but there is still error to verify and upload. I'm not sure where is my mistake. I'll be honest as i'm still new to arduino programs.

Could you help me check the program code?

int qNumber;
int quesNum;
int qResp;

void setup()                
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  Serial.println("Welcome to the F1 driving simulator");
  delay(1000);
  Serial.println("You will need to answer 5 question in order to proceed");
  delay(1000);
  Serial.println("Answer 3/5 questions correctly to play the game");
  delay(1000);
  Serial.println("Press 1 or 2 to choose the set of questions");
  delay(1000);
  Serial.println("------------------------------------------------------");
  delay(1000);
  Serial.println("");
}


void loop() 
{
  getTheQuestionNumber();
  if (qNumber > 0) 
  {
      askTheQuestion(qNumber);
  }
}


void getTheQuestionNumber() 
{
   int qNumber = Serial.read()// code to get a question number and save it in qNumber
   if (qNumber == '0')// put 0 in qNumber if there is no response or an invalid number

}


void askTheQuestion( byte quesNum ) 
{
   switch (quesNum) 
   {
      case 1:
           questionA();
           break;
      case 2:
           questionB();
           break;
   }
}

void questionA() 
{
  Serial.println("The charge of an electron is");
  Serial.println("A) 9.21 x 10^-37 kg");
  Serial.println("B) 1.759 x 10^11 C/kg");
  Serial.println("C) 1.6 x 10^-19 C");
  Serial.println("D) 1.6 x 10^-19 J");
  delay(2000); // display question and options
  correctAnswer = '';
  getResponse(correctAnswer);
 }


void getResponse( char okAns ) 
{
   // wait for answer and save it in qResp
    if (qResp == okAns) 
    {
     Serial.println("Right answer!");// hooray
    }     
    else 
    {
     Serial.println("Wrong answer!");// boo
    }
}

Thank you, your help will be appreciated.

When you have a compiling problem you should also post the first few lines of the error message.

I tried your code and it said

expected ‘,’ or ‘;’ before ‘}’ token

which is exactly what the problem is. You forgot the semi-colon at the end of the line before the number in the error message int qNumber = Serial.read()

However that isn't the only problem. The next line is incomplete and I don't know what you want to do.

If you comment out the incomplete line you will see other errors but I think it would be better for you to explore those yourself. Learning to figure out errors is an essential part of programming. If you do get stuck I will try to help.

One line will give an obscure error so I will give you the answer now correctAnswer = ''; is not valid. You must specify some character - a space might be suitable.

...R

   int qNumber = Serial.read()// code to get a question number and save it in qNumber
   if (qNumber == '0')// put 0 in qNumber if there is no response or an invalid number

Both lines are missing the terminating semi-colon

      case 2:
           questionB();
           break;

There is no questionB() function in the program

  correctAnswer = '';

The correctAnswer variable is not declared in the program.

int qNumber;
int quesNum;
int qResp;
int correctAnswer;

void setup()                
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  Serial.println("Welcome to the F1 driving simulator");
  delay(1000);
  Serial.println("You will need to answer 5 question in order to proceed");
  delay(1000);
  Serial.println("Answer 3/5 questions correctly to play the game");
  delay(1000);
  Serial.println("Press 1 or 2 to choose the set of questions");
  delay(1000);
  Serial.println("------------------------------------------------------");
  delay(1000);
  Serial.println("");
}


void loop() 
{
  getTheQuestionNumber();
  if (qNumber > 0) 
  {
      askTheQuestion(qNumber);
  }
}


void getTheQuestionNumber() 
{
   int qNumber = Serial.read();// code to get a question number and save it in qNumber
   if (qNumber == '0');// put 0 in qNumber if there is no response or an invalid number

}


void askTheQuestion( byte quesNum ) 
{
   switch (quesNum) 
   {
      case 1:
           questionA();
           break;
      case 2:
           questionB();
           break;
   }
}

void questionA() 
{
  Serial.println("The charge of an electron is");
  Serial.println("A) 9.21 x 10^-37 kg");
  Serial.println("B) 1.759 x 10^11 C/kg");
  Serial.println("C) 1.6 x 10^-19 C");
  Serial.println("D) 1.6 x 10^-19 J");
  delay(2000); // display question and options
  correctAnswer = ' ';
  getResponse(correctAnswer);
 }
 

void questionB() 
{
  Serial.println("Resistivity of wire depends on");
  Serial.println("A) Material");
  Serial.println("B) Length");
  Serial.println("C) Cross section area");
  Serial.println("D) None of the above5");
  delay(2000); // display question and options
  correctAnswer = ' ';
  getResponse(correctAnswer);
 }

void getResponse( char okAns ) 
{
   // wait for answer and save it in qResp
    if (qResp == okAns) 
    {
     Serial.println("Right answer!");// hooray
    }     
    else 
    {
     Serial.println("Wrong answer!");// boo
    }
}

This is the code i done improvise from your help.

How to move on from here. I can’t enter any key to proceed. To show the questions.

void loop() 
{
  getTheQuestionNumber();
  if (qNumber > 0) 
  {
      askTheQuestion(qNumber);
  }
}


void getTheQuestionNumber() 
{
   int qNumber = Serial.read();// code to get a question number and save it in qNumber
   if (qNumber == '0');// put 0 in qNumber if there is no response or an invalid number

You've got a scope problem with "qNumber" . You've also got a semicolon problem with that last "if".

I just posted a demo that has a question and answer part. See Chapters 7 and 8 here.

...R

So I have decided to use 4 push button switch for my questionnaire.

However, i still can't move on to the next question after answering the question before.

const int buttonA = 38;
const int buttonB = 40;
const int buttonC = 42;
const int buttonD = 44;

int buttonStateA = 0;
int buttonStateB = 0;
int buttonStateC = 0;
int buttonStateD = 0;

int lastButtonStateA = LOW;
int lastButtonStateB = LOW;
int lastButtonStateC = LOW;
int lastButtonStateD = LOW;

int count;
int countA;
int countB;
int countC;
int countD;

long lastDebounceTime = 0;
long debounceDelay = 50;


void setup()
{
 pinMode(buttonA, INPUT);
 pinMode(buttonB, INPUT);
 pinMode(buttonC, INPUT);
 pinMode(buttonD, INPUT);

 Serial.begin(9600);           // set up Serial library at 9600 bps
  
 Serial.println("Welcome to the F1 driving simulator");
 delay(100);
 Serial.println("You will need to answer 5 question in order to proceed");
 delay(100);
 Serial.println("Answer 3/5 questions correctly to play the game");
 delay(100);
 Serial.println("Press A or B to choose the set of questions");
 delay(100);
 Serial.println("------------------------------------------------------");
 delay(100);
 Serial.println("");
}

void loop()
{
 buttonStateA = digitalRead(buttonA);
 buttonStateB = digitalRead(buttonB);
 buttonStateC = digitalRead(buttonC);
 buttonStateD = digitalRead(buttonD);
  
  if(buttonStateA != lastButtonStateA)
  {
    if(buttonStateA == LOW)
   
    {
      count++;
      askTheQuestion();
      
    }
    lastButtonStateA = buttonStateA;
  }
}


void askTheQuestion() 
{
    if(count == 1)
    {
    Serial.println("First Set");
    delay(1000);
    Serial.println("Press A again");
    delay(1000);
    Serial.println("");
    delay(1000);
    }
    
   if(count == 2)
    {
      delay(100);
      questionA();
    }
    
    if(count == 3)
    {
      RightAnswer();
    }
      
   /* if(buttonStateA != lastButtonStateA)
  {
    if(buttonStateA == LOW)
    {
      questionA();
    }
    lastButtonStateA = buttonStateA;
  }*/
}


void questionA() 
{
  Serial.println("The charge of an electron is");
  Serial.println("A) 9.21 x 10^-37 kg");
  Serial.println("B) 1.759 x 10^11 C/kg");
  Serial.println("C) 1.6 x 10^-19 C");
  Serial.println("D) 1.6 x 10^-19 J");
 }


void questionB() 
{
  Serial.println("Resistivity of wire depends on");
  Serial.println("A) Material");
  Serial.println("B) Length");
  Serial.println("C) Cross section area");
  Serial.println("D) None of the above");
 }

void questionC() 
{
  Serial.println("The value of the following is given by (100 x 10^3) A x (1 x 10^-6) A");
  Serial.println("A) 0.001A");
  Serial.println("B) 0.1A");
  Serial.println("C) 1A");
  Serial.println("D) 10A");
 }

void questionD() 
{
  Serial.println("A circuit contains two un-equal resistances in parallel");
  Serial.println("A) Current is same in both");
  Serial.println("B) Large current flows in larger resistor");
  Serial.println("C) Potential difference across each is same");
  Serial.println("D) Smaller resistance has smaller conductance");
 }

void questionE() 
{
  Serial.println("Kirchhoff's second law is based on law of conservation of");
  Serial.println("A) Charge");
  Serial.println("B) Energy");
  Serial.println("C) Momentum");
  Serial.println("D) Mass");
 }

 void RightAnswer()
 { 
  Serial.println("Right answer!");// hooray
 }

 void WrongAnswer()
 {
  Serial.println("Wrong answer!");// boo
 }



/* void Break()
 {
   while(Serial.available() == 0)
   {
     break;
   }
 }*/

It would help me, and I suspect it would also help you, if you were to write down in English the steps your code is supposed to take to achieve your objective.

...R

I’m thinking that your problem might be related to sloppy thinking, and that starts with simple things like variable names. count? What the hell is that supposed to mean? You seem to think you can use it to determine which question to ask next (although you don’t have any code that actually causes the next question to be asked) and as some sort of indicator of where you are in the process of asking a question and getting an answer.

It can’t. Use it for ONE purpose, and give it a name that matches its purpose. That way, you won’t be tempted to use it for something else.

If the variable was named questionToAskNext, it would be quite obvious that it is NOT intended to be used to determine where you are in the ask a question, get an answer, check the answer process.

Robin2: It would help me, and I suspect it would also help you, if you were to write down in English the steps your code is supposed to take to achieve your objective.

...R

These are the steps: 1 ) Serial Monitor will display the introduction of the gaming simulator

2 ) User will then need to choose the sets of question, either (set A = Button A) or (set B = Button B)

3 ) The user will be prompted to press button A or button B again after selecting set A or set B

4 ) Example (Set A chosen - Button A pressed again), the first multiple choice question will display on the Serial Monitor.

5 ) User will need to answer the first multiple choice question, one of the four buttons (Button A, Button B, Button C, Button D) when pressed will display "Right Answer!" whereas for the other three buttons when pressed will display "Wrong Answer!"

6 ) Right after step 5, the next multiple choice question will then display with a delay of 2-3 seconds. Then it will repeat the same as step 5 again.

7 ) Step 5 and step 6 will keep on repeating till the last multiple choice question. (Question1 - Question5)

8 ) After the last multiple choice question is done, then it will display their overall marks, example (2/5 or 4/5)

9 ) If they score 2/5 or less, they will need to answer the questions again/loop (choose set A or set B again). if they score 3/5 or more, they can proceed to play the game.

Hope this will help Robin2, thanks!!!

void loop()
{
 buttonStateA = digitalRead(buttonA); 
 buttonStateB = digitalRead(buttonB);
 buttonStateC = digitalRead(buttonC);
 buttonStateD = digitalRead(buttonD);
  
  if(buttonStateA != lastButtonStateA)
  {
    if(buttonStateA == LOW)
   
    {
      countA++;
      askTheQuestion();
      
    }
    lastButtonStateA = buttonStateA;
  }
}

Is the void loop in the correct flow?

void askTheQuestion()
{
    if(countA == 1) 
    {
    Serial.println("Set A");
    delay(100);
    Serial.println("Press A again");
    delay(100);
    Serial.println("");
    delay(100);
    }
    
   if(countA == 2)
    {
      delay(100);
      question1();
    }
    
    if(countA == 3)
    {
      RightAnswer();
    }
    
}

Should i let the program flow like this?

Is there other ways, instead of using count sequence?

Thank you again!!!

void questionA() 
{
  Serial.println("The charge of an electron is");
  Serial.println("A) 9.21 x 10^-37 kg");
  Serial.println("B) 1.759 x 10^11 C/kg");
  Serial.println("C) 1.6 x 10^-19 C");
  Serial.println("D) 1.6 x 10^-19 J");
 }

I suggest you use the F macro or you will run out of RAM.

http://www.gammon.com.au/forum/?id=12153#tip2

 buttonStateA = digitalRead(buttonA); 
 buttonStateB = digitalRead(buttonB);
 buttonStateC = digitalRead(buttonC);
 buttonStateD = digitalRead(buttonD);
  
  if(buttonStateA != lastButtonStateA)

What if they press all the buttons? Is that an automatic pass?

Try a state machine:

http://www.gammon.com.au/statemachine

You have various states: Selecting a group of questions, asking one question, waiting for the answer, and so on

mikeydude30: These are the steps:

This Thread Planning and Implementing a Program might help you to turn those steps into a series of functions thaat build into a complete project.

I will try to have another look at your list later today.

...R

Robin2: This Thread Planning and Implementing a Program might help you ...

Now stickied. :)

[quote author=Nick Gammon link=topic=260531.msg1903376#msg1903376 date=1412154520] Now stickied. :) [/quote]

Thank you very much

...R