Led Chaser Game
Hello, my name is Tharush Uthayananthan and for my computer engineering class final i have created an LED chaser game. In this game, the LEDs will be flashing in a random order. Using the RGB you will be given a colour. With the given, you will need to press the button at the same time the LED chaser passes over that colour. For example, if you are given green you will need to press the pushbutton right as it passes over green. This circuit works through the usage of a 555 timer, a decade counter, and an arduino. The 555 timer and decade counter control the LED chaser, and then the arduino receives the signal from the LEDs and compares it to the push button. Now that you know how this project works, let's move onto the materials.
Supplies
Materials used in this projects
1. 555 Timer x1
2. Johnson's Decade Counter x1
3. Arduino Uno x1
4. 4 Digit Seven Segment Display x1
5. Potentiometer (10k) x1
6. Assorted LEDs (Preferably red, green and yellow to make the project easier to complete) x7
7. Pushbutton x1
8. Resistors
9. Electrolytic Capacitor (10 uF) x1
10. Wires
+ a breadboard
Setup
Gather every single one of your materials and place it on the breadboard and in the next step i'll explain the wiring
Wiring
Ensure all components are properly connected to both VCC and Ground to function correctly. To set it up:
555 Timer:
- Connect the Reset pin to power (VCC).
- Wire the Threshold pin to the Trigger pin.
- Add a 10 UF capacitor: connect the negative pin to the Ground pin of the 555 Timer and the positive pin to the Trigger pin.
Decade Counter:
- Connect the Output pin of the 555 Timer to the Clock pin of the Decade Counter. This allows the 555 Timer to send pulses to the Decade Counter, which in turn drives the LEDs.
- Attach power and ground wires to the Decade Counter as needed.
- Wire your LEDs to the Decade Counter's Output pins.
RGB LED and Pushbutton:
- Connect these components to the Arduino, including power connections and appropriate resistors.
Code
Starting off with an important part of the code and it is the variables. Initializing the variables for the Arduino pins is straight forward. Initialize the LEDs, RGB and the Pushbutton to their respective pins.
Next are the variables for the later code. The score code will allow you to see what your score is during the game, the level is used after each turn has been succeeded.The flags correctturn and buttonpressed will be used when we need to verify the button has been pressed or whether the correct colour has been pressed later on.Targetcolour will be used later to determine the colour the rgb chooses , and interval and timing will be used for the pattern and the time of how long the leds flash for.
Void Loop
The looping function
My int currentMillies = millies //measure time since the last led was lit up I didn't use delay due to since it's in void loop it would disrupt other tasks.
The if statement there is used since I wanted each led to be on for half a second the if statement will check if the leds have been on longer than the interval ( 500 milliseconds)
resetLeds() is a function that'll be explained soon
My switch case is used with module 6 as whatever value comes out of currentled = (currentled + 1) % 6 will determine what case is used and what led is on.
Now the long if statement is used for my button pressed detection
Since I flagged buttonpressed = false at the beginning doing (!buttonpressed) will evaluate it to true
isMatch is a function that'll also be explained soon but in short if isMatch evaluvates to true using the value it receives from currentLED it'll update score and change level
if level<10 it'll change level and the speed of each level by a multipler of 0.8 once the 10th level is completed a winner animation will show
and finally if correcturn it'll change rgb colour or if not evaluate to false.
Functions
Finally to understand the code each individual function:
targetColour() : Using the random (0,3) the code will return a random value that's either 0-2 ( computers start counting at 0 not 1) each case number represents a colour 0 is red , 1 is green and 2 is blue for example if 0 is returned the target colour would be red.
isMatch() : When we declared our variables we had six leds on the breadboard I placed them in a pattern 1 = red , 2 = green , 3 = blue , 4 = red , 5 = green , 6 = blue basically when the function is called when the button is pressed in the void loop function it'll check in the isMatch function whether the targetcolour matches the leds number for example red which was 0 in targetcolour has two red leds and will check if the target colour was red and whether either red leds were on and return true if not then return false.
resetLeds() : Basically just switches them all off by using LOW
winnerAnimation(): Switches all on delay for 200 milliseconds switched them all one and ends the game