loading
ButtonHero is a game made on an Arduino that involves the use of buttons, LEDs, wires, and resistors. In this game, you have to hit the corresponding button to the light that is randomly selected before the light changes. You start with 5 lives and missing or being too slow subtracts one life. The high score is 56. Good luck!


Video of the Serial Monitor of the same game: http://screencast.com/t/PTaS3JwUu

Step 1: Supplies

Supplies include:
• 4 LEDs
• 4 pushbuttons
• 1 Arduino (we used a Dueilanove)
• 4  8.2kΩ resistors
• 4  100Ω resistors
• 4  0.56kΩ resistors
• lots of wire

Step 2: Schematic

Here is the schematic for ButtonHero. The Fritzing file is also attached.

Step 3: The Code

Attached is the Arduino code file. 

Code:
int ledDelay; // LED timeout
int score; // points you get
byte currentLed; // the one that is lit up right now
boolean started; // Are you playing the game?
boolean needNewLed = true; // we need a new LED
byte health = 6; // health remaining
boolean updateScreen=true; // do we need to update the screen?
boolean lostHealth=false; // do we need to subtract one from the health?

int val2,val3,val4,val5;                        // variable for reading the pin status
int buttonState2,buttonState3,buttonState4,buttonState5;                // variable to hold the last button state


void setup() {
  // mode the pins
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  // for showing the score
  Serial.begin(9600);
  Serial.println("+---------------------------------------------------------------+");
  Serial.println("|Welcome to ButtonHero!                                         |");
  Serial.println("|When a light lights up, press the corrisponding button.        |");
  Serial.println("|You lose points by being too slow or pressing the wrong button.|");
  Serial.println("|Press any button to start...                                   |");
  Serial.print("+---------------------------------------------------------------+");
}

void loop() {
  //
  if (digitalRead(2)==HIGH || digitalRead(3)==HIGH || digitalRead(4)==HIGH || digitalRead(5)==HIGH){
    started=true; //start the game if any button is pressed
  }
  if (started==true){
    if (needNewLed ==true){ //we need another LED
      digitalWrite(currentLed, LOW); // turn off the old one
      byte randomLed = random(9,13); // pick a random LED
      while(currentLed == randomLed){ //make sure it's not the same as the last one
        randomLed = random(9,13);
      }
      currentLed = randomLed;
      digitalWrite(currentLed, HIGH); //light up the new LED
      delay(100); //wait a little bit
      needNewLed = false;
    }

    val2 = digitalRead(2);      // read input value and store it in val
    val3 = digitalRead(3);      // read input value and store it in val
    val4 = digitalRead(4);      // read input value and store it in val
    val5 = digitalRead(5);      // read input value and store it in val

    if (val2 != buttonState2 && val2 == HIGH || val3 != buttonState3 && val3 == HIGH || val4 != buttonState4 && val4 == HIGH || val5 != buttonState5 && val5 == HIGH) {          // the button state has changed!
      if (currentLed == 9 && digitalRead(5) == HIGH){ // did they hit the right button?
        score++;
        needNewLed = true;
        ledDelay = 0;
        updateScreen=true;
      }
      else if (currentLed == 10 && digitalRead(4) == HIGH){ // did they hit the right button?
        score++;
        needNewLed = true;
        ledDelay = 0;
        updateScreen=true;
      }
      else if (currentLed == 11 && digitalRead(3) == HIGH){ // did they hit the right button?
        score++;
        needNewLed = true;
        ledDelay = 0;
        updateScreen=true;
      }
      else if (currentLed == 12 && digitalRead(2) == HIGH){ // did they hit the right button?
        score++;
        needNewLed = true;
        ledDelay = 0;
        updateScreen=true;
      }
      else if (digitalRead(2)==HIGH || digitalRead(3)==HIGH || digitalRead(4)==HIGH || digitalRead(5)==HIGH) { // did they hit the wrong button?
        loseHealth();
        lostHealth=true;
      }
    }
    else { // they didnt press anything
      ledDelay++;
      delay(1);
    }

    buttonState2 = val2;                 // save the new state in our variable
    buttonState3 = val3;                 // save the new state in our variable
    buttonState4 = val4;                 // save the new state in our variable
    buttonState5 = val5;                 // save the new state in our variable
   
    // did the LED reach the timeout threshold?
    if (score <= 10){
      if(ledDelay == 1000){
        loseHealth();
        lostHealth=true;
      }
    }
    else if (score <= 15){
      if(ledDelay == 800){
        loseHealth();
        lostHealth=true;
      }
    }
    else if (score <= 20){
      if(ledDelay == 600){
        loseHealth();
        lostHealth=true;
      }
    }
    else if (score <= 25){
      if(ledDelay == 400){
        loseHealth();
        lostHealth=true;
      }
    }
    else if (score <= 30){
      if(ledDelay == 200){
        loseHealth();
        lostHealth=true;
      }
    }
    else if (score > 30){
      if(ledDelay == (220-score)){
        loseHealth();
        lostHealth=true;
      }
    }
   
    // update the display
    if(updateScreen==true){
      Serial.println(' ');
      Serial.println("+---------------------------------------------------------------+");
      Serial.println("|                                                               |");
      Serial.print("| Score: ");
      Serial.print(score);
      if(score<10){
        Serial.println("                                                      |");
      }
      else if(score<100){
        Serial.println("                                                     |");
      }
      else if(score<1000){
        Serial.println("                                                    |");
      }

      if(health==6){
        health=5;
      }

      if(health==5){
        Serial.println("| Lives: <3 <3 <3 <3 <3                                         |");
        Serial.println("|                                                               |");
      }
      else if(health==4){
        Serial.println("| Lives: <3 <3 <3 <3                                            |");
        Serial.println("|                                                               |");
      }
      else if(health==3){
        Serial.println("| Lives: <3 <3 <3                                               |");
        Serial.println("|                                                               |");
      }
      else if(health==2){
        Serial.println("| Lives: <3 <3                                                  |");
        Serial.println("|                                                               |");
      }
      else if(health==1){
        Serial.println("| Lives: <3                                                     |");
        Serial.println("|                                                               |");
      }
      else if(health==0){
        Serial.println("| Lives:                                                        |");
        Serial.println("|                          YOU LOSE!                            |");
        health = 6;
        score = 0;
      }
      Serial.print("+---------------------------------------------------------------+");
      updateScreen=false;
    }
  }
  lostHealth=false;
}

// subtract one health
void loseHealth(){
  if(lostHealth==false){
    health--;
    updateScreen=true;
    needNewLed=true;
    ledDelay = 0;
    if(health <= 0){ // did they lose the game?
      started=false;
      updateScreen=true;
      // blink 3 times
      digitalWrite(9,HIGH);
      digitalWrite(10,HIGH);
      digitalWrite(11,HIGH);
      digitalWrite(12,HIGH);
      delay(200);
      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
      digitalWrite(11,LOW);
      digitalWrite(12,LOW);
      delay(200);
      digitalWrite(9,HIGH);
      digitalWrite(10,HIGH);
      digitalWrite(11,HIGH);
      digitalWrite(12,HIGH);
      delay(200);
      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
      digitalWrite(11,LOW);
      digitalWrite(12,LOW);
      delay(200);
      digitalWrite(9,HIGH);
      digitalWrite(10,HIGH);
      digitalWrite(11,HIGH);
      digitalWrite(12,HIGH);
      delay(200);
      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
      digitalWrite(11,LOW);
      digitalWrite(12,LOW);
    }
  }
}

Step 4: Done!

Open the Serial Monitor (9600 baud) and resize it to be 6 lines high and wide enough so that the horizontal scrollbar disappears. 
Have fun!
<p>Hello. I was trying to make this project and I have completed all the steps. However, I am having trouble with the LEDs. The LEDs are flashing but they always flash on the 6th time as if its GAME OVER, even when I click the correct corresponding button. Please help me. Thanks.</p>
<p>It's as if the buttons are unresponsive.</p>
<p>This isn't really something I can help you troubleshoot. It sounds like a software issue, so you'll have to step through your code to find the problem. It's likely you have a typo somewhere or conditions are triggering early due to an oversight on when they will be triggered.</p>
<p>Are you sure there is no error in the code because I have discussed with my teachers and even a specialist who have all claimed that the code was the issue. This is because what else can be the issue? When I click on the correct buttons, the serial monitor does not register them. However, the specialist checked each button and said that the circuit was functioning properly.</p>
An unavoidable part of programming is troubleshooting and debugging. It can take hours to days and, in extreme cases, months. It involves close examination of the code and lots of Googling. It's frustrating and tedious, but it must be done to find and solve your mistakes. <br><br>I wrote this Instructable years ago. I barely remember how it works, and I don't have time to get back into it. <br><br>This is something you need to troubleshoot. I can't help you on this specific issue. I don't know what your code is or what your circuit looks like because I am not there. Sorry, but I cannot help you here. Please stop asking me. It sounds like you have some very supportive advisers who can help you on-site. They are much more suited to help you then I am. Take their advice. You said your specialist said there's an issue with your code. Start troubleshooting there.
<p>I appreciate you trying to put you full effort in helping me. Thank you, and I will take your advice. </p>
<p>I feel like the reason why it stops after 6 is because the &quot;byte health&quot; = 6. I changed it to a random number but that only affects the first time it is played, and after that, it flashes after 6. I feel like the reason is because of this part in the code below. How can I make it so the score is not fixed. </p><p>if(health==6){</p><p> health=5;</p><p> }</p><p> if(health==5){</p><p> Serial.println(&quot;| Lives: &lt;3 &lt;3 &lt;3 &lt;3 &lt;3 |&quot;);</p><p> Serial.println(&quot;| |&quot;);</p><p> }</p><p> else if(health==4){</p><p> Serial.println(&quot;| Lives: &lt;3 &lt;3 &lt;3 &lt;3 |&quot;);</p><p> Serial.println(&quot;| |&quot;);</p><p> }</p><p> else if(health==3){</p><p> Serial.println(&quot;| Lives: &lt;3 &lt;3 &lt;3 |&quot;);</p><p> Serial.println(&quot;| |&quot;);</p><p> }</p><p> else if(health==2){</p><p> Serial.println(&quot;| Lives: &lt;3 &lt;3 |&quot;);</p><p> Serial.println(&quot;| |&quot;);</p><p> }</p><p> else if(health==1){</p><p> Serial.println(&quot;| Lives: &lt;3 |&quot;);</p><p> Serial.println(&quot;| |&quot;);</p><p> }</p><p> else if(health==0){</p><p> Serial.println(&quot;| Lives: |&quot;);</p><p> Serial.println(&quot;| YOU LOSE! |&quot;);</p><p> health = 6;</p><p> score = 0;</p><p> }</p><p> Serial.print(&quot;+---------------------------------------------------------------+&quot;);</p><p> updateScreen=false;</p><p> }</p><p> }</p><p> lostHealth=false;</p>
<p>You can download the Arduino IDE here.</p><p>The Serial Monitor can be opened with the button in the top right corner. You can change the baud rate at the bottom of the serial monitor but 9600 should be the default.</p><p>There's a lot of great resources for learning Arduino online. The Arduino website has some <a href="https://www.arduino.cc/en/Tutorial/HomePage" rel="nofollow">here</a>. Try Googling your questions. </p>
<p>What's the code if I will use LCD 16x2? the Liquid Crystal Display?</p>
<p>It depends on your display. Google the name of your display and there will usually be a library for you to use.</p>
<p>Can you help me with my issue? Thanks.</p>
<p>I made it! But i need more buttons! How to add and do the coding?</p>
<p>It works! First try! Thank you for the easy to follow instructable. I like being able to see where everything goes. The pictures were definitely helpful, since there wasn't a schematic to go by. I had to use different resistors cause I didn't have the ones you suggested. But other than that I didn't have any problems. Thanks again. Great job! </p>
<p>Thanks! If you were looking for the more traditional schematic, you can download the Fritzing file in Step 2 and switch the view to schematic. Fritzing is a great piece of circuit creation software and is available here: http://fritzing.org/home/</p>
dude you totally copied this doug. i had this idea before you but you posted this first. youre a phony. A BIG FAT PHONY
<p>you're just lazy</p>
ARDUINO DOUGSTER 123456789
I noticed that you used random insted of true random. If you use the &quot;normal&quot; random it will use the same sequence. Therefor it is not really random. Thankfully on the arduino site you can get the true random! Great idea.
Great work !! <br>How come you didnt hade debouncing or interrupts ???
Nice 'ible! <br>One suggestion to make it easier is to eliminate the 8k2 resistors and the 560 ohm resistors by using the arduino's internal pull-up resistors on the input pins and wire the buttons to ground when pressed instead of 5v (and change the SW appropriately). Saves 8 resistors. <br> <br>Best Wishes

About This Instructable

27,139views

58favorites

License:

More by EpicWolverine:ButtonHero 
Add instructable to: