LED Chaser Game

by VithulanA in Circuits > Arduino

103 Views, 0 Favorites, 0 Comments

LED Chaser Game

asdff.jpg
Final Evaluation- LED chaser.png

On this instruction, I'll be showing you how to make a LED chaser game. The main premise of this is where there’s six LEDs with two of each colour from Red, Green and Yellow. The sequence of LEDS will randomize and will have a speed based on the potentiometer. Pressing the button with the right colour will award a point. The colour chosen will appear on the RGB LEDS. This circuit will include the usage of a 555 Timer, a decade counter, Arduino, LEDS, a capacitor and a push button.


Supplies

  1. Assorted Colours of LEDS x 6
  2. 555 Timer x 1
  3. Decade Counter x 1
  4. RGB LED x 1
  5. Potentiometer-10k x 1
  6. Resistors x 4 - 550 Ohms, 2x 330 Ohms, 1k Ohms
  7. Capacitator-10uf x 1
  8. Wires
  9. Arduino x 1

Wiring

image (13).png
  1. Connecting Power and Ground

When starting off, make sure to connect your components to both VCC and Ground where applicable in order to have them work

2. Building 555 Timer

The LED chaser revolves around the 555 Timer in order for it to display a sequence of LEDs flashing. Following the TinkerCAD, start off by connecting the power and ground. Next, add wires to the Trigger and Reset pins of the 555 Timer from the Power and Threshold pins. Connect the Power to the Reset pin and connect the Threshold to the Trigger pin. Then add a 10uf polarized capacitor by adding on a ground wire to the negative pin, and after that, wire the negative pin to the 555 Timer's Ground pin and wire the positive pin of the capacitor to the 555 Timer's Trigger pin. Lastly, add the potentiometer to the Discharge pin and a 550 ohm resistor to both Discharge and Threshold of the 555 Timer. This will allow you to change the speed of the LED sequence. Finishing the 555 Timer will allow us to connect it to the Decade Counter.

3. Building Decade Counter

Starting off with the Decade Counter, connect the Output pin of the 555 Timer to the Clock of the Decade Counter. Connecting the 555 Timer to the Decade Counter will allow it to send a pulse to it which can soon be sent to the LEDs. Secondly, add your power and ground wires to the Decade Counter as shown above. Thirdly, connect your six LEDs by wiring their Anode leg to any of the Output pins of the Decade Counter. Lastly, connect your LEDs to a 330 ohms resistor and connect wires that are wired to the Decade Counter to the Arduino's pin #8-13.

4. RGB LED and Pushbutton

Both the RGB LED and Pushbutton are straight forward, connect them to the Arduino and add both power, and their respective resistors. For the RGB LED, add your 330 Ohms resistor to the power rail instead of ground as shown in the TinkerCad version. As the RGB we're using is Common Cathode.


Coding-Variables

codevar.png

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, 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 randColour code is important to the later code as this will determine which LED you need to choose in the game. It will select from a range of 1-3 which will run a function for the respective Red, Green and Yellow. The x variable is used to reset the randColour code after the button is pressed to cycle through for a new number to choose a new colour.

Coding- Setup & SetColour

image (15).png

After initializing the variables, we'll need to setup either the input or output aspect of them in the void Setup as pinMode((Variable), (Input/Output);. For the LEDs, we'll set them up as inputs as they're receiving pulses from the Decade Counter. Pushbutton will be Input as it is used to input a pulse. The RGB pins will be set as output as we're using it to display a colour used for the LED chaser.

Our setColour function is used for the RGB LED in order to produce the colours needed to display.

Coding- Loop Function

image (16).png

Our Loop function will be the biggest part of the entire code. Inside the loop function. To start off, we'll set our randColour to, randColour = random(0,4); to randomize values from 1,2, and 3. Then we'll code three if statements to choose one of three colours. If randColour = 1, red is chosen. If randColour = 2, yellow is chosen. If randColour = 3, green is chosen. Inside our if statements, I added a while statement so that I can add two lines of code that is used to print the colour chosen and light up the RGB led of said colour. I will also add the code: x++; to only let this while loop be printed only once until the button is pressed. After, I initialized the Pushbutton state called buttonState to read the value of the Pushbutton connected to its pin. Inside of the if statement when pushing the button, it will check if the LEDs corresponding to the colour is HIGH. If this is true, a delay is added to only get one pulse of the Pushbutton. Your score will add by one and will be printed in the Serial Monitor. However, if the Pushbutton is pressed but a different LED is set to HIGH, then the score will subtract by one. At the end of both these, the line x=0; will reset the x variable to allow us to choose another colour. The same process repeats for the next two colours but, we change the LEDS that are HIGH and LOW to match with the corresponding LEDS that needed to be pressed or not to be pressed.

At the end of the final if statement, add a line of code to check if your score is either 10 or -10. If either of these numbers are met, your score will reset back to zero.

Completion

After you finish your code, you're done with the whole LED chaser game and can now play the game.

The code file is left incase you run into coding error to see the comments and main code.