Arduino LED project

I am working on a project for school where i have to meet the following criteria.

  1. Connect 7 LEDs in a row to the Arduino, each on its own output pin

  2. Connect a button/switch to an input on the Arduino for the user to play the game

  3. Gameplay is as follows:

a. The program lights the LEDs, one at a time, in sequence from one end of the row to the other and back

b. The user tries to press the input button while the center LED is lit

c. If the user succeeds, all LEDs flash 3 times, and the game is repeated with a shorter time interval between the LED's lighting

d. When the user fails, all LED's light, then turn off slowly, one at a time, to indicate the end of the game

e. Finally, the game should start over, with the original time period between the LEDs.

I am currently working in TinkerCAD and have the board wired up and most of the coding done ( i have yet to do part E)

I can get the LEDs to bounce back and forth but get no response when the button is pushed. If i jump the wires to set the button to high the light do the shut down sequence as programmed but will not when the button is pushed. Any help would be greatly appreciated.

int i = 0;

int buttonState = 0;

void setup()
{
pinMode(12, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(5, INPUT);
}

void loop()
{
buttonState = digitalRead(12);
while (buttonState == LOW) {
digitalWrite(2, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
}
if (buttonState == HIGH && digitalRead(5) == HIGH) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
} else {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(2, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(4, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(8, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(6, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(7, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(5, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
}

When you use the delay function, delay(), ‘all code execution stops’ for that amount of time.

You must write your code in a ‘non blocking’ fashion if you expect to get a responsive application.

Please review these links to learn to write non blocking code:

How and Why to avoid delay():
http://playground.arduino.cc/Code/AvoidDelay

Demonstration code for several things at the same time.
http://forum.arduino.cc/index.php?topic=223286.0

@tduds

Please follow the advice on posting code in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and using code tags when posting code here

And for bonus marks from teacher, give the pins names, preferably in an array

I would offer the following advice:

  1. Use millis() instead of delay for timing: BlinkWithoutDelay
  2. You should be checking for a CHANGE in the button inputs not for their state StateChangeDetection
  3. If you use the internal pullup resistor and wire button to GND you can eliminate the external resistor.
  4. This could be done with a simple state machine to make the code more readable: State Machine