Light Catching Game

This is a game where you want to push the button when the middle LED lights up. Just a little more advanced while loop than the last one.

int button = 2; // declares pushbutton

int pin= 4; // declares LEDs

int pin1= 6;

int pin2= 8;

int pin3= 10;

int pin4= 12;

int wait= 100; // this will be used for delays

int score = 0; // this will tell you how many times you got it

int wrong = 0; // if your wrong too many times the game starts over

void setup ()

{

pinMode(pin, OUTPUT); // sets as output

pinMode(pin1, OUTPUT);

pinMode(pin2, OUTPUT);

pinMode(pin3, OUTPUT);

pinMode(pin4, OUTPUT);

pinMode(button, INPUT); // sets as input

Serial.begin(9600);

}

void loop ()

{

while (digitalRead(button) == LOW) // when the button is low, do the following

{

for (int x = 4; x<=12; x=x+2) // sets loop to cycle through lighting up LEDs

{

digitalWrite(x, HIGH);

delay(wait);

if (digitalRead(button) == HIGH) // if the button is press

{

if (x == 8) // and the LED 8 is on

{

delay(1500); // wait 1.5 seconds

score= score+1; // add one to the score

Serial.println(score); // relay score back to computer

wait = wait*.9; // decrease delay between LEDs

}

else

{

digitalWrite(x, LOW); // if button is pressed and its not the right LED

x = 2; // loop starts over

wrong= wrong+1; // the wrong count increases

if (wrong == 5) // if you get 5 wrong

{

for (int x = 4; x<=12; x=x+2)

{

digitalWrite(x, HIGH); // all lights will come on

delay(1000);

wrong = 0; // resets wrong count

score = 0; // resests score

wait = 100; // resets delay time

}

}

}

}

digitalWrite(x, LOW);

}

}

}