ButtonHero

ButtonHero

by Brendan Ferracciolo and Daniel Mayers

Brief Description

ButtonHero is a game similar to guitar hero. To play you must hit the button the corresponds to the LED that lights up. The game is fully enclosed within a box made from a 3-D printer.

Detailed Description

ButtonHero is composed of four LEDs, four buttons, a screen, an Arduino Duemilanove, many resistors, wires, a 3-D printed box, and a prototyping shield. The program takes advantage of many If statements. All of the components are attached to the prototyping board which is connected to the Arduino. 8 digital pins are used from the Arduino along with the ground and +5 volt pins. The loop uses digitalRead() to look and see if a button has been pressed and stores this in a variable. This then checks to see if the correct button was pressed in which the code either contiunes the loop, adds a point, or detours to loseHealth() where one point is subtracted from the health and retrieves a new random seed for random(). With every 5-10 correct hits the timeout delay is shortened to make it harder and harder as the game progresses. The video below only shows the beginning due to an attempt at keeping the video fairly short, so it only demonstrates the easy beginning of the game..

Media

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

boolean pressed;

int screen = 0;

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.print("?G216?c0?S0");

Serial.print("?D7000A1F1F1F0E0400");

//Serial.print("?fCreated by?m?kBrendan & Daniel");

Serial.print("?f Created by?m?k Doug & Doug2.0");

delay(1750);

Serial.print("?f");

//type("Welcome to?m?kButtonHero!",25);

}

void loop() {

if (started==false){

if (health == 0){

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!

pressed=true;

started=true;

health = 6;

score = 0;

ledDelay = 0;

currentLed = 0;

needNewLed = true;

updateScreen = true;

lostHealth = false;

randomSeed(int(millis));

}

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

delay(1);

}

else{

Serial.print("?f");

if(screen==0){

Serial.print("Welcome to?m?kButtonHero! v");

screen++;

}

else if(screen==1){

Serial.print("Welcome to?m?kButtonHero!");

screen=0;

}

else if(screen==2){

Serial.print("If a LED lights?m?kup, press the v");

screen=3;

}

else if(screen==3){

Serial.print("If a LED lights?m?kup, press the");

screen=2;

}

else if(screen==4){

Serial.print("corrisponding?m?kbutton. v");

screen=5;

}

else if(screen==5){

Serial.print("corrisponding?m?kbutton.");

screen=4;

}

else if(screen==6){

Serial.print("You lose lives?m?kby being too v");

screen=7;

}

else if(screen==7){

Serial.print("You lose lives?m?kby being too");

screen=6;

}

else if(screen==8){

Serial.print("slow or pressingthe wrong v");

screen=9;

}

else if(screen==9){

Serial.print("slow or pressingthe wrong");

screen=8;

}

else if(screen==10){

Serial.print("button.?m?k v");

screen=11;

}

else if(screen==11){

Serial.print("button.");

screen=10;

}

else if(screen==12){

Serial.print("Press any buttonto start...");

}

for (int i=0; i < 750; i++){

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 (pressed==false){

pressed=true;

if(screen==12){

screen=13;

started=true;

randomSeed(int(millis));

}

else if(screen==10||screen==11){

screen=12;

Serial.print("?f");

//type("Press any buttonto start...",27);

}

else if(screen==8||screen==9){

screen=10;

Serial.print("?f");

//type("button.",7);

}

else if(screen==6||screen==7){

screen=8;

Serial.print("?f");

//type("slow or pressingthe wrong",25);

}

else if(screen==4||screen==5){

screen=6;

Serial.print("?f");

//type("You lose lives?m?kby being too",30);

}

else if(screen==2||screen==3){

screen=4;

Serial.print("?f");

//type("corrisponding?m?kbutton.",24);

}

else if(screen==0||screen==1){

screen=2;

Serial.print("?f");

//type("If a LED lights?m?kup, press the",32);

}

}

//break;

}

else{

pressed=false;

}

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

delay(1);

}

}

}

else 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 == 500){

loseHealth();

lostHealth=true;

}

}

else if (score <= 30){

if(ledDelay == 400){

loseHealth();

lostHealth=true;

}

}

else if (score > 30){

if(ledDelay == (420-score)){

loseHealth();

lostHealth=true;

}

}

// update the display

if(updateScreen==true){

Serial.print("?fScore: ");

Serial.print(score);

if(health==6){

health=5;

}

if (health==5){

Serial.print("?m?kLives: ?7 ?7 ?7 ?7 ?7");

}

else if(health==4){

Serial.print("?m?kLives: ?7 ?7 ?7 ?7");

}

else if(health==3){

Serial.print("?m?kLives: ?7 ?7 ?7");

}

else if(health==2){

Serial.print("?m?kLives: ?7 ?7");

}

else if(health==1){

Serial.print("?m?kLives: ?7");

}

else if(health<=0){

Serial.print("?m?k YOU LOSE");

}

updateScreen=false;

}

}

lostHealth=false;

}

// subtract one health

void loseHealth(){

if(lostHealth==false){

randomSeed(int(millis));

health--;

updateScreen=true;

needNewLed=true;

ledDelay = 0;

if(health <= 0){ // did they lose the game?

started=false;

Serial.print("?fScore: ");

Serial.print(score);

Serial.print("?m?k YOU LOSE");

// 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);

}

}

}

/*

void type(char message[],byte length){

for(int i; i<length; i++){

Serial.print(message[i]);

for (int a; a < 100; a++){

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 (pressed==false){

pressed=true;

if(screen==12){

started=true;

screen=13;

}

else if(screen==10||screen==11){

screen=12;

}

else if(screen==8||screen==9){

screen=10;

}

else if(screen==6||screen==7){

screen=8;

}

else if(screen==4||screen==5){

screen=6;

}

else if(screen==2||screen==3){

screen=4;

}

else if(screen==0||screen==1){

screen=2;

}

break;

}

}

else{

pressed=false;

}

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

delay(1);

}

}

}

*/

Other

Some problems that we encountered were whenever the far right button was pressed the circuit shorted, this was fixed by removing a small piece of solder that was bridging across two pins. Also the hole for the screen was slightly too small and had to be enlarged with sand paper.

With more money we would have like to make the box a different color and slightly larger. Additionally, re-printing the back would have been useful if we had enough time as it did not fit correctly and had to be glued on instead of snapping in.

Circuit Diagram