Introduction: ButtonHero
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.
Attachments
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);
}
}
}
Attachments
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!
11 Comments
6 years ago
You can download the Arduino IDE here.
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.
There's a lot of great resources for learning Arduino online. The Arduino website has some here. Try Googling your questions.
6 years ago
What's the code if I will use LCD 16x2? the Liquid Crystal Display?
Reply 6 years ago
It depends on your display. Google the name of your display and there will usually be a library for you to use.
Reply 6 years ago
Can you help me with my issue? Thanks.
6 years ago
I made it! But i need more buttons! How to add and do the coding?
9 years ago on Introduction
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!
Reply 9 years ago on Introduction
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/
10 years ago on Introduction
ARDUINO DOUGSTER 123456789
10 years ago on Step 3
I noticed that you used random insted of true random. If you use the "normal" 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.
10 years ago on Introduction
Great work !!
How come you didnt hade debouncing or interrupts ???
10 years ago on Introduction
Nice 'ible!
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.
Best Wishes