Wait for button response

I am working on a project that I am unsure what direction to go in for the code. I am building a game that will play a sound asking the player a question. The player will have a choice of buttons to choose an answer from. If the player chooses a correct answer a winning sound will play. If the player chooses an incorrect answer a loosing sound will play. I have been able to get things to work individually but am having trouble bringing them together. Currently my code does not have any mechanism to wait for a correct/incorrect response from the player before starting the loop over. If I press one of the answer buttons while the question is still being played then the winning/loosing routine will execute. How would I go about getting things to wait for a response before staring the loop over gain? See several posts below for more complete section of code*

  {
  if (pinvalRIGHT == HIGH)
  if (pinvalWRONG == HIGH)
  if (pinvalSTART == LOW)
  
  {
  //ASK QUESTIONS:  
   //QUESTION1
  digitalWrite(STARTLED, LOW);
  digitalWrite(AUDIO1, HIGH);
  delay(20);
  digitalWrite(AUDIO1, LOW);
  digitalWrite(DESTA, HIGH);
  digitalWrite(DESTB, HIGH);
  delay(period2); 
  digitalWrite(DESTA, LOW);
  digitalWrite(DESTB, LOW);
  delay(period2); 
  
  
  if (pinvalRIGHT == LOW)
  if (pinvalWRONG == HIGH)
  if (pinvalSTART == HIGH)
  {
  //ANSWER1 CORRECT
  digitalWrite(WINNER, HIGH);
  digitalWrite(DESTA, HIGH);
  digitalWrite(DESTB, HIGH);
  delay(period2); 
  digitalWrite(DESTA, LOW);
  digitalWrite(DESTB, LOW);
  delay(period2); 
  }
   
  if (pinvalRIGHT == HIGH)
  if (pinvalWRONG == LOW)
  if (pinvalSTART == HIGH)
  {
  //ANSWER1 INCORRECT
  digitalWrite(LOOSER, HIGH);
  digitalWrite(DESTA, HIGH);
  digitalWrite(DESTB, HIGH);
  delay(period2); 
  digitalWrite(DESTA, LOW);
  digitalWrite(DESTB, LOW);
  delay(period2); 
  }
    
  }
  
  digitalWrite(STARTLED, HIGH);
  }

how would I go about getting things to wait for a response before staring the loop over gain?

There is no loop in your code.

AWOL:

how would I go about getting things to wait for a response before staring the loop over gain?

There is no loop in your code.

I only posted what was inside the loop. Should I post more code?

Nor anything that reads the buttons. Seems like a snippet that is missing a lot of pieces.

I only posted a snippet from the loop. I will post more code momentarily...

OK, here is a more complete piece of my code:

int STARTLED = A0; 
int STARTBUTTON = A1;
int BUTTON1 = A2;
int BUTTON2 = A3;
int BUTTON3 = A4;
int BUTTON4 = A5;
int BUTTON5 = A6;
int BUTTON6 = A7;
int BUTTONWRONG = A8;
int UNO1 = A9;
int UNO2 = A10;
int UNO3 = A11;
int UNO4 = A12;
int UNO5 = A13;
int UNO6 = A14;
int UNO7 = A15;
int SSEGA = 0;
int SSEGB = 1;
int SSEGC = 2;
int SSEGD = 3;
int SSEGE = 4;
int SSEGF = 5;
int SSEGG = 6;
int SSEGH = 7;
int SSEGI = 8;
int SSEGJ = 9;
int SSEGK = 10;
int SSEGL = 11;
int SSEGM = 12;
int SSEGN = 13;
int DESTA = 14;
int DESTB = 15;
int DESTC = 16;
int DESTD = 17;
int DESTE = 18;
int DESTF = 19;
int DESTG = 20;
int OPEN1 = 21;
int MAP1 = 22;
int MAP2 = 23;
int MAP3 = 24;
int MAP4 = 25;
int MAP5 = 26;
int MAP6 = 27;
int MAP7 = 28;
int MAP8 = 29;
int MAP9 = 30;
int LOGO = 50;
int OPEN2 = 51;
int OPEN3 = 52;
int OPEN4 = 53;

int period = 500;
int period2 = 1000;
int period3 = 100;

// variables will change:
int pinval1 = 0;          //button value
int pinval2 = 0;          //button value
int pinval3 = 0;          //button value
int pinval4 = 0;          //button value
int pinval5 = 0;          //button value
int pinval6 = 0;          //button value
int pinvalWRONG = 0;          //button value
int pinvalSTART = 0;          //button value



void setup()
{
  pinMode(STARTLED, OUTPUT);  
  pinMode(STARTBUTTON, INPUT); 
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(BUTTON3, INPUT);
  pinMode(BUTTON4, INPUT);
  pinMode(BUTTON5, INPUT);
  pinMode(BUTTON6, INPUT);
  pinMode(BUTTONWRONG, INPUT);
  pinMode(UNO1, OUTPUT);
  pinMode(UNO2, OUTPUT);
  pinMode(UNO3, OUTPUT);
  pinMode(UNO4, OUTPUT);
  pinMode(UNO5, OUTPUT);
  pinMode(UNO6, OUTPUT);
  pinMode(UNO7, OUTPUT);
  pinMode(SSEGA, OUTPUT);
  pinMode(SSEGB, OUTPUT);
  pinMode(SSEGC, OUTPUT);
  pinMode(SSEGD, OUTPUT);
  pinMode(SSEGE, OUTPUT);
  pinMode(SSEGF, OUTPUT);
  pinMode(SSEGG, OUTPUT);
  pinMode(SSEGH, OUTPUT);
  pinMode(SSEGI, OUTPUT);
  pinMode(SSEGJ, OUTPUT);
  pinMode(SSEGK, OUTPUT);
  pinMode(SSEGL, OUTPUT);
  pinMode(SSEGM, OUTPUT);
  pinMode(SSEGN, OUTPUT);
  pinMode(DESTA, OUTPUT);
  pinMode(DESTB, OUTPUT);
  pinMode(DESTC, OUTPUT);
  pinMode(DESTD, OUTPUT);
  pinMode(DESTE, OUTPUT);
  pinMode(DESTF, OUTPUT);
  pinMode(DESTG, OUTPUT);
  pinMode(OPEN1, OUTPUT);
  pinMode(MAP1, OUTPUT);
  pinMode(MAP1, OUTPUT);
  pinMode(MAP2, OUTPUT);
  pinMode(MAP3, OUTPUT);
  pinMode(MAP4, OUTPUT);
  pinMode(MAP5, OUTPUT);
  pinMode(MAP6, OUTPUT);
  pinMode(MAP7, OUTPUT);
  pinMode(MAP8, OUTPUT);
  pinMode(MAP9, OUTPUT);
  pinMode(LOGO, OUTPUT);
  pinMode(OPEN2, OUTPUT);
  pinMode(OPEN3, OUTPUT);
  pinMode(OPEN4, OUTPUT);
  
  digitalWrite(STARTBUTTON, HIGH);
  digitalWrite(BUTTON1, HIGH);
  digitalWrite(BUTTON2, HIGH);
  digitalWrite(BUTTON3, HIGH);
  digitalWrite(BUTTON4, HIGH);
  digitalWrite(BUTTON5, HIGH);
  digitalWrite(BUTTON6, HIGH);
  digitalWrite(BUTTONWRONG, HIGH);
  digitalWrite(UNO1, LOW);
  digitalWrite(UNO2, LOW);
  digitalWrite(UNO3, LOW);
  digitalWrite(UNO4, LOW);
  digitalWrite(UNO5, LOW);
  digitalWrite(UNO6, LOW);
  digitalWrite(UNO7, LOW);
  
  
    
}


void loop()

  {
  
  
  pinval1 = digitalRead(BUTTON1);
  pinval2 = digitalRead(BUTTON2);
  pinval3 = digitalRead(BUTTON3);
  pinval4 = digitalRead(BUTTON4);
  pinval5 = digitalRead(BUTTON5);
  pinval6 = digitalRead(BUTTON6);
  pinvalWRONG = digitalRead(BUTTONWRONG);
  pinvalSTART = digitalRead(STARTBUTTON);
  
  
  if (pinval1 == HIGH)
  if (pinval2 == HIGH)
  if (pinval3 == HIGH)
  if (pinval4 == HIGH)
  if (pinval5 == HIGH)
  if (pinval6 == HIGH)
  if (pinvalWRONG == HIGH)
  if (pinvalSTART == HIGH) 
  {
  //ALL OFF
  digitalWrite(MAP1, LOW);  
  digitalWrite(MAP2, LOW); 
  digitalWrite(MAP3, LOW);
  digitalWrite(MAP4, LOW);
  digitalWrite(MAP5, LOW);
  digitalWrite(MAP6, LOW);
  digitalWrite(MAP7, LOW);
  digitalWrite(MAP8, LOW);
  digitalWrite(MAP9, LOW);
  digitalWrite(DESTA, LOW);
  digitalWrite(DESTB, LOW);
  digitalWrite(DESTC, LOW);
  digitalWrite(DESTD, LOW);
  digitalWrite(DESTE, LOW);
  digitalWrite(DESTF, LOW);
  digitalWrite(DESTG, LOW);
  digitalWrite(LOGO, LOW);
  digitalWrite(SSEGA, LOW);
  digitalWrite(SSEGB, LOW);
  digitalWrite(SSEGC, LOW);
  digitalWrite(SSEGD, LOW);
  digitalWrite(SSEGE, LOW);
  digitalWrite(SSEGF, LOW);
  digitalWrite(SSEGG, LOW);
  digitalWrite(SSEGH, LOW);
  digitalWrite(SSEGI, LOW);
  digitalWrite(SSEGJ, LOW);
  digitalWrite(SSEGK, LOW);
  digitalWrite(SSEGL, LOW);
  digitalWrite(SSEGM, LOW);
  digitalWrite(SSEGN, LOW);
  digitalWrite(STARTLED, HIGH);
  digitalWrite(UNO1, LOW);
  digitalWrite(UNO2, LOW);  
  digitalWrite(UNO3, LOW);
  digitalWrite(UNO4, LOW);
  digitalWrite(UNO5, LOW);
  digitalWrite(UNO6, LOW);
  digitalWrite(UNO7, LOW);
  
  digitalWrite(STARTBUTTON, HIGH);
  digitalWrite(BUTTON1, HIGH);
  digitalWrite(BUTTON2, HIGH);
  digitalWrite(BUTTON3, HIGH);
  digitalWrite(BUTTON4, HIGH);
  digitalWrite(BUTTON5, HIGH);
  digitalWrite(BUTTON6, HIGH);
  digitalWrite(BUTTONWRONG, HIGH); 
  } 
  
  
  if (pinval1 == HIGH)
  if (pinval2 == HIGH)
  if (pinval3 == HIGH)
  if (pinval4 == HIGH)
  if (pinval5 == HIGH)
  if (pinval6 == HIGH)
  if (pinvalWRONG == HIGH)
  if (pinvalSTART == LOW)
  
  
  {
  //ASK QUESTIONS:
  
  
  
   //QUESTION1
  digitalWrite(STARTLED, LOW);
  digitalWrite(UNO1, HIGH);
  digitalWrite(UNO2, HIGH);
  digitalWrite(UNO3, LOW);
  digitalWrite(UNO4, LOW);
  digitalWrite(UNO5, HIGH);
  digitalWrite(UNO6, LOW);
  digitalWrite(UNO7, LOW);  
  delay(20);
  digitalWrite(UNO1, LOW);
  digitalWrite(UNO2, LOW);
  digitalWrite(UNO5, LOW);
  digitalWrite(DESTA, HIGH);
  delay(period2);
  delay(period2);
  digitalWrite(DESTB, HIGH);
  delay(period2); 
  delay(period2);
  delay(period2);
  delay(period2);
  delay(period2);
  delay(period2);
  digitalWrite(DESTA, LOW);
  digitalWrite(DESTB, LOW);
  delay(period2); 
  
  pinval1 = digitalRead(BUTTON1);
  pinval2 = digitalRead(BUTTON2);
  pinval3 = digitalRead(BUTTON3);
  pinval4 = digitalRead(BUTTON4);
  pinval5 = digitalRead(BUTTON5);
  pinval6 = digitalRead(BUTTON6);
  pinvalWRONG = digitalRead(BUTTONWRONG);
  pinvalSTART = digitalRead(STARTBUTTON);
  


  
  
  if (pinval1 == LOW)
  if (pinval2 == HIGH)
  if (pinval3 == HIGH)
  if (pinval4 == HIGH)
  if (pinval5 == HIGH)
  if (pinval6 == HIGH)
  if (pinvalWRONG == HIGH)
  if (pinvalSTART == HIGH)
  {
  //ANSWER1 CORRECT
  digitalWrite(STARTLED, LOW);
  digitalWrite(UNO1, HIGH);
  delay(20);
  digitalWrite(UNO1, LOW);
  digitalWrite(DESTA, HIGH);
  delay(period2);  
  digitalWrite(MAP1, HIGH); 
  delay(period);
  digitalWrite(MAP2, HIGH);
  delay(period);
  digitalWrite(MAP3, HIGH);
  delay(period);
  digitalWrite(MAP4, HIGH);
  delay(period);
  digitalWrite(MAP5, HIGH);
  delay(period);
  digitalWrite(MAP6, HIGH);
  delay(period);
  digitalWrite(DESTB, HIGH);
  digitalWrite(LOGO, HIGH);
  digitalWrite(SSEGB, HIGH);
  digitalWrite(SSEGC, HIGH);
  digitalWrite(SSEGD, HIGH);
  digitalWrite(SSEGE, HIGH);
  digitalWrite(SSEGG, HIGH);
  delay(period2);
  }
  

  if (pinval1 == HIGH)
  if (pinval2 == HIGH)
  if (pinval3 == HIGH)
  if (pinval4 == HIGH)
  if (pinval5 == HIGH)
  if (pinval6 == HIGH)
  if (pinvalWRONG == LOW)
  if (pinvalSTART == HIGH)
  {
  //ANSWER1 INCORRECT
  digitalWrite(STARTLED, LOW);
  digitalWrite(UNO1, HIGH);
  digitalWrite(UNO7, HIGH);
  delay(20);
  digitalWrite(UNO1, LOW);
  digitalWrite(UNO7, LOW);
  digitalWrite(DESTA, HIGH);
  delay(period2);  
  digitalWrite(MAP1, HIGH); 
  delay(period);
  digitalWrite(MAP2, HIGH);
  delay(period);
  digitalWrite(MAP3, HIGH);
  delay(period);
  digitalWrite(MAP4, HIGH);
  delay(period);
  digitalWrite(MAP5, HIGH);
  delay(period);
  digitalWrite(MAP6, HIGH);
  delay(period);
  digitalWrite(DESTB, HIGH);
  digitalWrite(LOGO, HIGH);
  digitalWrite(SSEGB, HIGH);
  digitalWrite(SSEGC, HIGH);
  digitalWrite(SSEGD, HIGH);
  digitalWrite(SSEGE, HIGH);
  digitalWrite(SSEGG, HIGH);
  delay(period2);
  }
    
  
  }
  
  digitalWrite(STARTLED, HIGH);
  }

If you can get to grips with arrays, for loops and the && operator, your code will be much smaller and more readable.

Comments would help greatly.

I'm brand new to writing code. I know there has to be a better way of doing it but this is the best I can currently do. I'm trying to learn a step at a time.

Ok, a really useful first step is using the auto format tool in the IDE

AWOL: Ok, a really useful first step is using the auto format tool in the IDE

Thanks! I will read up on using the auto format tool.

max-rockatansky:

AWOL:
Ok, a really useful first step is using the auto format tool in the IDE

Thanks! I will read up on using the auto format tool.

Not much to read up on - it’s a menu option in the IDE. Depending on your OS, command-T or control-T will likely invoke it too.

OK, I see how using the auto format can make things easier to read in the code. It works just fine in some of my sketches but in others I get this error and no changes to the formatting. How do I interpret what I need to fix from the info in the error code?

java.lang.ArrayIndexOutOfBoundsException: 10
    at processing.app.tools.AutoFormat.run(AutoFormat.java:867)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

How do I interpret what I need to fix from the info in the error code?

You don't. All that that message is telling you is that the auto formatter is thoroughly confused. There are some things that completely baffle it, like backslashes in comments.

Post the code that the Auto Format tool has problems with. Of course, you can help it out by indenting stuff as you go and always using { and }. I'd get confused by this, too:

  if (pinval1 == HIGH)
  if (pinval2 == HIGH)
  if (pinval3 == HIGH)
  if (pinval4 == HIGH)
  if (pinval5 == HIGH)
  if (pinval6 == HIGH)
  if (pinvalWRONG == HIGH)
  if (pinvalSTART == HIGH) 
  {

There are better ways.

PaulS:

How do I interpret what I need to fix from the info in the error code?

You don’t. All that that message is telling you is that the auto formatter is thoroughly confused. There are some things that completely baffle it, like backslashes in comments.

Post the code that the Auto Format tool has problems with. Of course, you can help it out by indenting stuff as you go and always using { and }. I’d get confused by this, too:

  if (pinval1 == HIGH)

if (pinval2 == HIGH)
  if (pinval3 == HIGH)
  if (pinval4 == HIGH)
  if (pinval5 == HIGH)
  if (pinval6 == HIGH)
  if (pinvalWRONG == HIGH)
  if (pinvalSTART == HIGH)
  {



There are better ways.

That error code if from my code above. I tried indenting things but I still get the same error. As far as better ways that is why I’m here - to learn. What I was trying to do there was to say if none of the buttons were pressed then do {}.

There are better ways.

You've got 8 values. There are 8 bits in a byte. Coincidence? Probably, but that does not matter.

Look at bitWrite(). Put the switch states in a byte. Then, there is only one if test needed, not 8.

PaulS:

There are better ways.

You've got 8 values. There are 8 bits in a byte. Coincidence? Probably, but that does not matter.

Look at bitWrite(). Put the switch states in a byte. Then, there is only one if test needed, not 8.

The Arduino reference page really did not give me an understanding of the technique. Can you post some examples?

Think of a byte as an 8 element array of bits. The bitWrite() function allows you to set one element - you specify which one (0 to 7) and which value (0 or 1).

int swByte = 0;
bitWrite(swByte, 5, 1);

Then, the if test would be something like:

if(swByte == 0xFF)
{
   // All switches were HIGH
}