Arduino Game Board - Q/A

Hello all. We are building a Arduino Game board for a school project. The idea is you will select Question & Correct Answer combination by pushing the corresponding momentary switches and if it is correct a Green Light will illuminate. If it is wrong you will receive a red light. Will probably tie in Audio buzzers as well. At first this seemed pretty straight forward until getting to the wrong answer combo. Need help on the else statement. The red light should not illuminate unless pressing buttons that do not match. Any help would be appreciated. Below is the code that works great for the right answer but having issues tacking the wrong answer combo.

const int ledPin13 = 13;       // Correct Answer LED
const int ledPin12 = 12;       // Wrong Answer LED
const int  greenbuttonPin2 = 2;    // first push button
const int  greenbuttonPin3 = 3;    // second push button
const int  orangebuttonPin4 = 4;   // first push button
const int  orangebuttonPin5 = 5;   // second push button


void setup() 
{
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(greenbuttonPin2, INPUT);
  pinMode(greenbuttonPin3,INPUT);
  pinMode(orangebuttonPin4,INPUT);
  pinMode(orangebuttonPin5,INPUT);
}


void loop() 
{
  // read GREEN BUTTONS :
  int pin2State = digitalRead(greenbuttonPin2);
  int pin3State = digitalRead(greenbuttonPin3);
  if (pin2State == HIGH && pin3State == HIGH ) 
  {
    digitalWrite(ledPin13,HIGH);
  }
     
  if (pin2State == LOW && pin3State == LOW) 
  {
    digitalWrite(ledPin13,LOW);
  }

  // read ORGANGE BUTTONS :
  int pin4State = digitalRead(orangebuttonPin4);
  int pin5State = digitalRead(orangebuttonPin5);
  if (pin4State == HIGH && pin5State == HIGH ) 
  {
    digitalWrite(ledPin13,HIGH);
  }

  if (pin4State == LOW && pin5State == LOW) 
  {
    digitalWrite(ledPin13,LOW);
  }


}
//else if{
//    digitalWrite(ledPin12,HIGH);
//    delay(10);
//    digitalWrite(ledPin12,LOW);  
//    }

Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

1 Like
const int ledPin13 = 13;       // Correct Answer LED
const int ledPin12 = 12;       // Wrong Answer LED
const int  greenbuttonPin2 = 2;    // first push button
const int  greenbuttonPin3 = 3;    // second push button
const int  orangebuttonPin4 = 4;   // first push button
const int  orangebuttonPin5 = 5;   // second push button


void setup()
{
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(greenbuttonPin2, INPUT);
  pinMode(greenbuttonPin3, INPUT);
  pinMode(orangebuttonPin4, INPUT);
  pinMode(orangebuttonPin5, INPUT);
}


void loop()
{
  // read GREEN BUTTONS :
  int pin2State = digitalRead(greenbuttonPin2);
  int pin3State = digitalRead(greenbuttonPin3);
  if (pin2State == HIGH && pin3State == HIGH )
  {
    digitalWrite(ledPin13, HIGH);
  }

  if (pin2State == LOW && pin3State == LOW)
  {
    digitalWrite(ledPin13, LOW);
  }

  // read ORGANGE BUTTONS :
  int pin4State = digitalRead(orangebuttonPin4);
  int pin5State = digitalRead(orangebuttonPin5);
  if (pin4State == HIGH && pin5State == HIGH )
  {
    digitalWrite(ledPin13, HIGH);
  }

  if (pin4State == LOW && pin5State == LOW)
  {
    digitalWrite(ledPin13, LOW);
  }


}
//else if{
//    digitalWrite(ledPin12,HIGH);
//    delay(10);
//    digitalWrite(ledPin12,LOW);
//    }


It's actually rather simple.
Change

else if {

to

else {
1 Like

LEDs require a series dropping resistor, see D5 and D6 in the image below.

1 Like

Use the 5v pin on the Mega instead of 3.3v.

1 Like

That works well accept for the last set of matching buttons.
2 Green buttons = Green Light
1 Green button + 1 Orange button = Red light
Pressing both orange buttons = RED & Green LED's being lit.

const int ledPin13 = 13;       // Correct Answer LED
const int ledPin12 = 12;       // Wrong Answer LED
const int  greenbuttonPin2 = 2;    // first push button
const int  greenbuttonPin3 = 3;    // second push button
const int  orangebuttonPin4 = 4;   // first push button
const int  orangebuttonPin5 = 5;   // second push button


void setup()
{
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(greenbuttonPin2, INPUT);
  pinMode(greenbuttonPin3, INPUT);
  pinMode(orangebuttonPin4, INPUT);
  pinMode(orangebuttonPin5, INPUT);
}


void loop()
{
  // read GREEN BUTTONS :
  int pin2State = digitalRead(greenbuttonPin2);
  int pin3State = digitalRead(greenbuttonPin3);
  if (pin2State == HIGH && pin3State == HIGH )
  {
    digitalWrite(ledPin13, HIGH);
  }

  if (pin2State == LOW && pin3State == LOW)
  {
    digitalWrite(ledPin13, LOW);
  }

  // read ORGANGE BUTTONS :
  int pin4State = digitalRead(orangebuttonPin4);
  int pin5State = digitalRead(orangebuttonPin5);
  if (pin4State == HIGH && pin5State == HIGH )
  {
    digitalWrite(ledPin13, HIGH);
  }

  if (pin4State == LOW && pin5State == LOW)
  {
    digitalWrite(ledPin13, LOW);
  }
  else {
    digitalWrite(ledPin12, HIGH);
    delay(10);
    digitalWrite(ledPin12, LOW);
  }

Provide us with a truth table.

So right now i'm just using colors for matching. Later it will be Question and correct answer with all the same color buttons.
What I would like it to do:
1 Green button Pressed + the 2nd Green Button Pressed = Green LED Light
1 Orange Button Pressed + the 2nd Orange Button Pressed = Green LED Light
Any other combo of buttons pressed should yield a red LED light.

What I'm getting now:
With the below code adjustments with the Else statement I am getting
2 Green buttons pressed = 1 Green Light + 1 RED Light (Not desired)
2 Orange buttons pressed = 1 Green Light + 1 Red Light (Not desired)
Any other combination of button presses = Only Red Light (which is desired)

const int ledPin13 = 13;       // Correct Answer LED
const int ledPin12 = 12;       // Wrong Answer LED
const int  greenbuttonPin2 = 2;    // first push button
const int  greenbuttonPin3 = 3;    // second push button
const int  orangebuttonPin4 = 4;   // first push button
const int  orangebuttonPin5 = 5;   // second push button
const int  redbuttonPin6 = 6;      // first push button
const int  redbuttonPin7 = 7;      // second push button


void setup()
{
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(greenbuttonPin2, INPUT);
  pinMode(greenbuttonPin3, INPUT);
  pinMode(orangebuttonPin4, INPUT);
  pinMode(orangebuttonPin5, INPUT);
  pinMode(redbuttonPin6, INPUT);
  pinMode(redbuttonPin7, INPUT);
}


void loop()
{
  // read GREEN BUTTONS :
  int pin2State = digitalRead(greenbuttonPin2);
  int pin3State = digitalRead(greenbuttonPin3);
  if (pin2State == HIGH && pin3State == HIGH )
  {
    digitalWrite(ledPin13, HIGH);
  }

  if (pin2State == LOW && pin3State == LOW)
  {
    digitalWrite(ledPin13, LOW);
  }
  else {
    digitalWrite(ledPin12, HIGH);
    delay(10);
    digitalWrite(ledPin12, LOW);
  }
  // read ORGANGE BUTTONS :
  int pin4State = digitalRead(orangebuttonPin4);
  int pin5State = digitalRead(orangebuttonPin5);
  if (pin4State == HIGH && pin5State == HIGH )
  {
    digitalWrite(ledPin13, HIGH);
  }

  if (pin4State == LOW && pin5State == LOW)
  {
    digitalWrite(ledPin13, LOW);
  }
  else {
    digitalWrite(ledPin12, HIGH);
    delay(10);
    digitalWrite(ledPin12, LOW);
  }

  // red RED BUTTONS :
  int pin6State = digitalRead(redbuttonPin6);
  int pin7State = digitalRead(redbuttonPin7);
  if (pin6State == HIGH && pin7State == HIGH )
  {
    digitalWrite(ledPin13, HIGH);
  }

  if (pin6State == LOW && pin7State == LOW)
  {
    digitalWrite(ledPin13, LOW);
  }

  else {
    digitalWrite(ledPin12, HIGH);
    delay(10);
    digitalWrite(ledPin12, LOW);
  }
}






You made it hard to follow by not naming the pins according to the LED colour, like 'redLED' 'greenLED' for example...

Same for the input pins, in the beginning you name them by buttons, but the state variables lose the button name and revert to pin number related names. So it gets obfuscated again.

Make it easy on yourself and others, name things what they are.
Others may be willing, but I'm going to wait until you change that.

Also keep in mind, once you set an output to some state, it remains in that state until explicitly changed.

For example:

  if (orange1State == LOW && orange2State == LOW)
  {
    digitalWrite(greenLED, LOW);
  }

Please add comments to your code lines so we can understand what you expect to happen.


BTW

  else {
    digitalWrite(ledPin12, HIGH);
    delay(10);
    digitalWrite(ledPin12, LOW);
  }

10ms is nothing you can see.

Excellent point.

Ok - named the LED's as well as added some comments. The issue i'm having is the Else for the Red light. I just need 1 else statement that says any other combo yields a red light without writing a ton of does not equal code for every combo.

const int GreenledPin13 = 13;       // Correct Answer LED
const int RedledPin12 = 12;       // Wrong Answer LED
const int  greenbuttonPin2 = 2;    // first push button
const int  greenbuttonPin3 = 3;    // second push button
const int  orangebuttonPin4 = 4;   // first push button
const int  orangebuttonPin5 = 5;   // second push button
const int  redbuttonPin6 = 6;      // first push button
const int  redbuttonPin7 = 7;      // second push button


void setup()
{
  pinMode(GreenledPin13, OUTPUT);
  pinMode(RedledPin12, OUTPUT);
  pinMode(greenbuttonPin2, INPUT);
  pinMode(greenbuttonPin3, INPUT);
  pinMode(orangebuttonPin4, INPUT);
  pinMode(orangebuttonPin5, INPUT);
  pinMode(redbuttonPin6, INPUT);
  pinMode(redbuttonPin7, INPUT);
}


void loop()
{
  // GREEN BUTTONS FOR A MATCH :
  int pin2State = digitalRead(greenbuttonPin2);
  int pin3State = digitalRead(greenbuttonPin3);
  if (pin2State == HIGH && pin3State == HIGH )
  {
    digitalWrite(GreenledPin13, HIGH);
  }
  if (pin2State == LOW && pin3State == LOW)
  {
    digitalWrite(GreenledPin13, LOW);
  }

  //THIS IS MY ATTEMPT AT SOLVING THE ELSE ISSUE: THIS CLEARLY IS NOT WHERE IT BELONGS.  IF ANY OTHER KEY COMBO PRESSED IT LIGHTS UP RED LED:
  //HAVING THE ELSE HERE CAUSES A CORRECT MATCH TO ALSO LIGHT THE RED LED
  else {
    digitalWrite(RedledPin12, HIGH);
    delay(10);
    digitalWrite(RedledPin12, LOW);
  }

  //  ORGANGE BUTTONS FOR A MATCH :
  int pin4State = digitalRead(orangebuttonPin4);
  int pin5State = digitalRead(orangebuttonPin5);
  if (pin4State == HIGH && pin5State == HIGH )
  {
    digitalWrite(GreenledPin13, HIGH);
  }

  if (pin4State == LOW && pin5State == LOW)
  {
    digitalWrite(GreenledPin13, LOW);
  }

  //THIS IS MY ATTEMPT AT SOLVING THE ELSE ISSUE: THIS CLEARLY IS NOT WHERE IT BELONGS.  IF ANY OTHER KEY COMBO PRESSED IT LIGHTS UP RED LED:
  //HAVING THE ELSE HERE CAUSES A CORRECT MATCH TO ALSO LIGHT THE RED LED
  else {
    digitalWrite(RedledPin12, HIGH);
    delay(10);
    digitalWrite(RedledPin12, LOW);
  }

  // RED BUTTONS FOR A MATCH:
  int pin6State = digitalRead(redbuttonPin6);
  int pin7State = digitalRead(redbuttonPin7);
  if (pin6State == HIGH && pin7State == HIGH )
  {
    digitalWrite(GreenledPin13, HIGH);
  }

  if (pin6State == LOW && pin7State == LOW)
  {
    digitalWrite(GreenledPin13, LOW);
  }
  //THIS IS MY ATTEMPT AT SOLVING THE ELSE ISSUE: THIS CLEARLY IS NOT WHERE IT BELONGS.  IF ANY OTHER KEY COMBO PRESSED IT LIGHTS UP RED LED:
  //HAVING THE ELSE HERE CAUSES A CORRECT MATCH TO ALSO LIGHT THE RED LED
  else {
    digitalWrite(RedledPin12, HIGH);
    delay(100);
    digitalWrite(RedledPin12, LOW);
  }
}






Ok - named the LED's as well as added some comments. The issue i'm having is the Else for the Red light. I just need 1 else statement that says any other combo yields a red light without writing a ton of does not equal code for every combo.

const int GreenledPin13 = 13;       // Correct Answer LED
const int RedledPin12 = 12;       // Wrong Answer LED
const int  greenbuttonPin2 = 2;    // first push button
const int  greenbuttonPin3 = 3;    // second push button
const int  orangebuttonPin4 = 4;   // first push button
const int  orangebuttonPin5 = 5;   // second push button
const int  redbuttonPin6 = 6;      // first push button
const int  redbuttonPin7 = 7;      // second push button


void setup()
{
  pinMode(GreenledPin13, OUTPUT);
  pinMode(RedledPin12, OUTPUT);
  pinMode(greenbuttonPin2, INPUT);
  pinMode(greenbuttonPin3, INPUT);
  pinMode(orangebuttonPin4, INPUT);
  pinMode(orangebuttonPin5, INPUT);
  pinMode(redbuttonPin6, INPUT);
  pinMode(redbuttonPin7, INPUT);
}


void loop()
{
  // GREEN BUTTONS FOR A MATCH :
  int pin2State = digitalRead(greenbuttonPin2);
  int pin3State = digitalRead(greenbuttonPin3);
  if (pin2State == HIGH && pin3State == HIGH )
  {
    digitalWrite(GreenledPin13, HIGH);
  }
  if (pin2State == LOW && pin3State == LOW)
  {
    digitalWrite(GreenledPin13, LOW);
  }

  //THIS IS MY ATTEMPT AT SOLVING THE ELSE ISSUE: THIS CLEARLY IS NOT WHERE IT BELONGS.  IF ANY OTHER KEY COMBO PRESSED IT LIGHTS UP RED LED:
  //HAVING THE ELSE HERE CAUSES A CORRECT MATCH TO ALSO LIGHT THE RED LED
  else {
    digitalWrite(RedledPin12, HIGH);
    delay(10);
    digitalWrite(RedledPin12, LOW);
  }

  //  ORGANGE BUTTONS FOR A MATCH :
  int pin4State = digitalRead(orangebuttonPin4);
  int pin5State = digitalRead(orangebuttonPin5);
  if (pin4State == HIGH && pin5State == HIGH )
  {
    digitalWrite(GreenledPin13, HIGH);
  }

  if (pin4State == LOW && pin5State == LOW)
  {
    digitalWrite(GreenledPin13, LOW);
  }

  //THIS IS MY ATTEMPT AT SOLVING THE ELSE ISSUE: THIS CLEARLY IS NOT WHERE IT BELONGS.  IF ANY OTHER KEY COMBO PRESSED IT LIGHTS UP RED LED:
  //HAVING THE ELSE HERE CAUSES A CORRECT MATCH TO ALSO LIGHT THE RED LED
  else {
    digitalWrite(RedledPin12, HIGH);
    delay(10);
    digitalWrite(RedledPin12, LOW);
  }

  // RED BUTTONS FOR A MATCH:
  int pin6State = digitalRead(redbuttonPin6);
  int pin7State = digitalRead(redbuttonPin7);
  if (pin6State == HIGH && pin7State == HIGH )
  {
    digitalWrite(GreenledPin13, HIGH);
  }

  if (pin6State == LOW && pin7State == LOW)
  {
    digitalWrite(GreenledPin13, LOW);
  }
  //THIS IS MY ATTEMPT AT SOLVING THE ELSE ISSUE: THIS CLEARLY IS NOT WHERE IT BELONGS.  IF ANY OTHER KEY COMBO PRESSED IT LIGHTS UP RED LED:
  //HAVING THE ELSE HERE CAUSES A CORRECT MATCH TO ALSO LIGHT THE RED LED
  else {
    digitalWrite(RedledPin12, HIGH);
    delay(100);
    digitalWrite(RedledPin12, LOW);
  }
}





What starts the testing the switches to be tested ?

If the RedledPin12 pin goes HIGH for only 10 milliseconds, your eye will not see it come on.

OK - Here is super simplified. One else statement at the end like I'm thinking it should have. But does not work as expected.

const int GreenledPin13 = 13;       // Correct Answer LED
const int RedledPin12 = 12;       // Wrong Answer LED
const int  greenbuttonPin2 = 2;    // first push button
const int  greenbuttonPin3 = 3;    // second push button
const int  orangebuttonPin4 = 4;   // first push button
const int  orangebuttonPin5 = 5;   // second push button
const int  redbuttonPin6 = 6;      // first push button
const int  redbuttonPin7 = 7;      // second push button


void setup()
{
  pinMode(GreenledPin13, OUTPUT);
  pinMode(RedledPin12, OUTPUT);
  pinMode(greenbuttonPin2, INPUT);
  pinMode(greenbuttonPin3, INPUT);
  pinMode(orangebuttonPin4, INPUT);
  pinMode(orangebuttonPin5, INPUT);
  pinMode(redbuttonPin6, INPUT);
  pinMode(redbuttonPin7, INPUT);
}


void loop()
{
  // GREEN BUTTONS FOR A MATCH :
  int pin2State = digitalRead(greenbuttonPin2);
  int pin3State = digitalRead(greenbuttonPin3);
  if (pin2State == HIGH && pin3State == HIGH )
  {
    digitalWrite(GreenledPin13, HIGH);
  }
  if (pin2State == LOW && pin3State == LOW)
  {
    digitalWrite(GreenledPin13, LOW);
  }


  //  ORGANGE BUTTONS FOR A MATCH :
  int pin4State = digitalRead(orangebuttonPin4);
  int pin5State = digitalRead(orangebuttonPin5);
  if (pin4State == HIGH && pin5State == HIGH )
  {
    digitalWrite(GreenledPin13, HIGH);
  }

  if (pin4State == LOW && pin5State == LOW)
  {
    digitalWrite(GreenledPin13, LOW);
  }


  // RED BUTTONS FOR A MATCH:
  int pin6State = digitalRead(redbuttonPin6);
  int pin7State = digitalRead(redbuttonPin7);
  if (pin6State == HIGH && pin7State == HIGH )
  {
    digitalWrite(GreenledPin13, HIGH);
  }

  if (pin6State == LOW && pin7State == LOW)
  {
    digitalWrite(GreenledPin13, LOW);
  }
  //ANY OTHER KEY COMBO PRESSED SHOULD INSTEAD LIGHT UP UP RED LED:

  else {
    digitalWrite(RedledPin12, HIGH);

  }
}





What starts the testing the switches to be tested ?

Yes - If the correct two buttons are pressed. One will be the question and the other will be the answer. They will not line up on the board so you will have to press the question and the corresponding answer which will yield a green light. If you select two buttons that are not a match it will yield a red light. The updated code I just pasted keeps the red light on all the time.

Read about state machines, it could be as simple as

  • you start in IDLE state
  • in IDLE state You want to test if the user selects a question by pressing a question button. When s/he does you remember in a variable the currentQuestionIndexand move to the QUESTION state
  • in QUESTION state, You want to test if the user selects an answer button. When s/he does you Check if this is the right answer for the currentQuestionIndex. If it’s OK then light the green led and go to the WON state otherwise light the red led and go to the LOST state
  • in WON state you want to wait a bit (may be play a happy tune) and then switch off the green led and if no button is pressed go back to IDLE state to handle the next question
  • in LOST state you want to wait a bit (may be play a sad tune) and then switch off the red led and if no button is pressed go back to the QUESTION state for a new attempt

if you need both buttons pushed at the same time (ie hold the question button down while you select the answer) then you need to add a test as well in the QUESTION state to ensure the selected button is still pressed.

something like this

a button library would make life easy

1 Like