Catch It - Steve Dickie

Description:

This is a simple game, there are five lights. When you hold the button each light will light in sequence. Your task is to let go when the green light is on. After catching the green light the sequence gets faster and you try to catch it again. If you miss five times it starts over.

Program Description

First we read the button, if it is being held then we run through the sequence of LEDs. This runs basically like a "for" loop, but will check to see if we're still holding the button at each step.

void loop()

{

val = digitalRead(inputPin); // read the pushbutton state

if (val == HIGH) // Button is being held

{

if (ledPin > 8){ // Iniates/continues loop to run through LEDs

ledPin = ledPin -1;

pinMode(ledPin, OUTPUT); // sets the digital pin as output

digitalWrite(ledPin, HIGH); // sets the LED-ledPin on

delay(wait); // waits for time based upon______

digitalWrite(ledPin, LOW); // sets the LED-ledPin off

}

else {

ledPin=14; // restarts the loop

}

}

If the button is released and val==LOW we then check to see if the right LED is lit. If the green LED is lit (pin 11). Then we freeze it for one second, reset ledPin to 14, which will start the loop over again. It will also decrease the "wait" time making the sequence run faster. If you release at the wrong time the "wrong" counter will increase and ledPin will be set to 14. While ledPin==14 the loop will basically wait for you to push and hold the button again. That's what "else if (ledPin<14)" is for. You can only be wrong if you release the button after the LED sequence has started.

else { // button released

if (ledPin == 11) // Is the green light lit?

{ // If green on leave on for one second

digitalWrite(ledPin, HIGH);

delay(1000);

digitalWrite(ledPin, LOW);

ledPin = 14; // reset loop

wait = wait*.9; // decrease wait to make game tougher

Serial.println(wait); // send wait time to computer

}

else if (ledPin<14) { // if wrong

wrong=wrong+1; // increase wrong counter

Serial.println(wrong);

ledPin=14; // reset loop

If you're wrong 5 times you loose. All the LEDs will light up for two seconds and then go out and all the variables will be reset to initial conditions.

if (wrong==5){ // Check to see if it's game over

// Begin end game, which will light all LEDs

// for 2 seconds and then reset the game

for (int ledPin = 13; ledPin >= 8; ledPin = ledPin-1)

{ // Turn on all LEDs

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, HIGH);

}

delay(2000); // Wait 2 seconds

for (int ledPin = 13; ledPin >= 8; ledPin = ledPin-1)

{ // Turn off all LEDs

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, LOW);

}

wrong=0; // Reset the game to inital conditions

ledPin=14;

wait=100;

}

Complete Program

/*

*This is a game. To play you push and hlod the button.

*Try to let go of the button when the green pin is lit.

*After catching the green light the sequence gets faster.

*Fail to catch the green light 5 times and you loose.

*The game will reset.

*

*To set up the circuit:

*Button on Pin 2, LEDs on pins 8-13. One green led in pin 11.

*/

int inputPin = 2; // declares the pushbutton pin

int ledPin = 14; // declares ledPin

int val = 0; // val stores the pushbutton state

int wait = 100; // sets the initial speed of the game

int wrong = 0; // iniates the miss counter

void setup ()

{

pinMode(inputPin, INPUT); // set the pushbutton as an input

Serial.begin(9600); // prepare the board for seial communication for debuging purposes

}

void loop()

{

val = digitalRead(inputPin); // read the pushbutton state

if (val == HIGH) // Button is being held

{

if (ledPin > 8){ // Iniates/continues loop to run through LEDs

ledPin = ledPin -1;

pinMode(ledPin, OUTPUT); // sets the digital pin as output

digitalWrite(ledPin, HIGH); // sets the LED-ledPin on

delay(wait); // waits for time based upon______

digitalWrite(ledPin, LOW); // sets the LED-ledPin off

}

else {

ledPin=14; // restarts the loop

}

}

else { // button released

if (ledPin == 11) // Is the green light lit?

{ // If green on leave on for one second

digitalWrite(ledPin, HIGH);

delay(1000);

digitalWrite(ledPin, LOW);

ledPin = 14; // reset loop

wait = wait*.9; // decrease wait to make game tougher

Serial.println(wait); // send wait time to computer

}

else if (ledPin<14) { // if wrong

wrong=wrong+1; // increase wrong counter

Serial.println(wrong);

ledPin=14; // reset loop

if (wrong==5){ // Check to see if it's game over

// Begin end game, which will light all LEDs

// for 2 seconds and then reset the game

for (int ledPin = 13; ledPin >= 8; ledPin = ledPin-1)

{ // Turn on all LEDs

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, HIGH);

}

delay(2000); // Wait 2 seconds

for (int ledPin = 13; ledPin >= 8; ledPin = ledPin-1)

{ // Turn off all LEDs

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, LOW);

}

wrong=0; // Reset the game to inital conditions

ledPin=14;

wait=100;

}

}

}

}