Fantastic Five LEDs

FANTAST!C F!VE LEDs

BY: JAKE MAY AND ALEX RYBKA

Our project is a series of 5 LEDs hooked up to a breadboard. We also have connected a push button to the breadboard. It cycles through the LEDs, and the object is to stop the cycle on the green LED. You have three chances to do this. If you cannot manage to stop the cycle on the green LED, then the LEDs will begin cycling in the other direction. If, however, you can stop the cycle on the green LED in less than 3 tries, the green LED will begin to flash to alert you that you have beaten the game.

The program is relatively simple. We start out by defining our variables (above 'void setup'). Under void setup, we designate pins on the Arduino for different things, namely an output for the LEDs and an input for the push button. We also set up the serial monitor to run at 9600 bps, but we never really needed to use it. We then moved on to void loop. This is where all of the most important things happen. We set it up so that if the position of the button is changed, the computer realizes it, and it then determines if the button was pressed. If it was, the number of presses total is increased by 1. If the button has been pressed and the green LED (ledPin 5) is HIGH (lit up), then it stops cycling through the LEDs and the green light begins to flash. This means you have beaten the game.

We encountered numerous problems when we were doing this project. The biggest problem was troubleshooting the code. There seemed to be an endless amount of problems here. If I had more money and time, I would like to have added more LEDs, a speaker that plays a tune when you beat the game, and perhaps, I would have put the LEDs in a circle as opposed to in a line.

int ledPin = 9; //Sets up the first LED that will light up, LED from pin 9

int switchPin = 13; //Sets up the push button in pin 13

int val; //Declares val as a variable

int buttonState = 0; //Declares a varable for reading the button state

int dir = -1; //Declares dir as a variable equal to -1

int buttonPresses = 0; //Declares a variable for reading the number of button presses

void setup() {

pinMode(9,OUTPUT); //Declares pins as outputs for the LEDs

pinMode(8,OUTPUT);

pinMode(7,OUTPUT);

pinMode(6,OUTPUT);

pinMode(5,OUTPUT);

pinMode(switchPin,INPUT); //Declares pin as an input for the push button

Serial. begin(9600); //Sets up the serial library at 9600 bits-per-second

buttonState = digitalRead(switchPin); //Tells the computer to check if the button is being pressed

}

void loop() {

pinMode (ledPin, OUTPUT); //Declares the LED as an output

digitalWrite (ledPin, HIGH); //Turns the LED on

for (int x=0; x<20; x++) //The LEDs begin cycling through

{ val = digitalRead(switchPin);

if (val != buttonState) { //If the button state has changed...

if (buttonState == LOW){ //If the button state is low...

buttonPresses++; //Increase the number of times you have pushed the button by 1

Serial.println(ledPin);

if (ledPin == 5){ //If LED 5 is on...

dir = 0; //Stop cycling through the LEDs

delay(100); //Delay of 100ms

buttonPresses = 0; //Resets your button presses

dir = -1; //Begin cycling through the LEDs

}

}

}

buttonState = val;

delay(29); //Delay of 29ms

}

digitalWrite (ledPin, HIGH); //Turns the LED on

if (ledPin<=5 || ledPin>=9) //If the LED is on, ledPin 5 or less -OR- ledPin 9 or more...

{

dir = -dir;

}

ledPin=ledPin-dir;

if (buttonPresses == 3) { //If you have pressed the button 3 times

dir = 0; //Stop cycling through the LEDs

delay(1000); //Delay of 1000ms

buttonPresses = 0; //Resets your button presses to 0

dir = -1; //Cycle through the LEDs again

}

}