Arduino LED Chaser!

by NK - 724177 in Circuits > Arduino

273 Views, 1 Favorites, 0 Comments

Arduino LED Chaser!

Screenshot_20230125_033956.png


Welcome to the Arduino LED Chaser!


Here you'll see the step by step process of making a chaser game! What is a chaser game? Essentially the user must press the stop button when the lights loop around in a circle. If they press the stop button on the correct light, they win a point! The game keeps tracks of your points and you'll lose points if you miss the right LED. That is the basic concept of an LED chaser.

Supplies

You will need for this project:

Evidently, an Arduino as our control center,

Arduino priced $27.95 on sparkfun.com.

6 PNP transistors in controlling our 12 LEDs,

Transistor pack $8.49 on Amazon, LED Pack $9.65 on Amazon

A potentiometer to control the speed of the game,

Potentiometer pack at $8.25 on Amazon

7 Resistors, to ensure the circuit can properly flow,

Pack of 10, 1k resistors at $0.95 on Pishop

A diode in helping direct current,

A pack is priced at $8.99 on Amazon

A pushbutton to determine when the player wants to stop the looping lights on their desired LED,

A pack is priced at $7.99 on Amazon

And lastly, the breadboard and wires.


You can click on any of the links above to see each product for yourself!

Research and Idea Building

Lets look at this idea more in detail, from the idea to the implementation and research.

So, first and foremost the idea of an LED chaser game comes clearly from various other previously made games that also implement similar ideas. For example, most arcades include one genre of game where you have to press a 'stop' button at the right time, such as Jackpot. So now that we've established a simple base for the idea, how did it develop?

Well we are first posed by the problem of how to in reality implement this idea onto the breadboard. One good source to use pins to power multiple LEDs is here. It explains how we can use current sourcing and sinking, explained in the next paragraph, to make this project possible.

Building the Circuit

Screenshot_20230125_064220.png

Part 1: Simple Connections

First and foremost, we must connect our Arduino's positive and negative flows to their corresponding place on the breadboard. You can use either the 5V or the 3.3V current source on the Arduino - But be aware, that using a 5V source will require you to have the sufficient diode to control that current.


Part 2: Getting our LEDS

We have 12 LEDs on this circuit, but with other functions, the Arduino is only limited to so many pins. So how can we independently control all of these LED's while using less pins?

One way to accomplish this is to use a transistor, alongside a diode.

We know digitalWrite(LED, HIGH); can be used to turn on one LED - but we want to turn on two instead.

Essentially, one way to accomplish this, is when we get a positive HIGH signal from a Arduino pin, one LED will turn on. When we get a LOW signal from the very same pin, we can redirect and turn on a different LED. This allows us to control multiple LEDs from a singular pin! So how do we get this configuration to work?


We're going to want to connect a pin that will control one pair of LEDs (two per each pin) to the base of the PNP. We're also going to directly wire a LED from this pin with its cathode coming from the pin and anode into the negative row.

This will get the first of the pair working: When the pin outputs HIGH, the first LED will turn on, simple.

Now out the emitter of the PNP, we can connect another LED, withs its anode coming from the collector of the transistor and cathode into the positive row. This will allow the LED to turn on in difference of current.

Now, when the pin outputs LOW, the second LED in the pair will turn on. You can use a diode when connecting any specific wire to control current flow to ensure that only that LED is lit up and that current does not move the other way.

This is an important concept of Sinking and Sourcing the current.

When a pin sources current, it sends out a positive signal. Just like in the first LED mentioned, this LED receives this positive signal.

When a pin sinks current, it takes in these positive signals. In the second example, the LED powers itself, and when the Arduino pin outputs LOW, the difference in voltage allows it to sink the current and complete the circuit.

You can make as many LEDs as you want, but this example will showcase 12 LEDs (and thereby 12 transistors)


Part 3: The Potentiometer and Pushbutton

And finally we get to the potentiometer and pushbutton. For the potentiometer, wire it as normal. An analog read pin should be used in the middle leg, while the outer two should be positive and negative.

For the pushbutton we once again wire it normally. Add a positive, negative and pin in 3 of the legs akin to the beginning picture.

The Code, Part 1

Screenshot_20230125_071440.png

Lets first look at the variables. Here is a quick explanation on how they are used:

Variable rnd and intro are variables that rotate between 0 and 1. If they are 0, their corresponding function, such as an intro or random number generator will fire, and then they will precedingly set themselves to 1 again, waiting for a 0.

Variable buffer is the time delay between the movement of LEDs. It will be changed by the potentiometer later.

Variable target is the target LED the user has to try to press the pushbutton on

Variable ledwheel is the actual led that is currently on in the loop

Variable Pbreading is the current state of the pushbutton (1 or 0).

And finally, a simple points variable, keeping track of the hits/misses.

Now for the setup, ensure that all the pins are activated and the required analog pin of the potentiometer and that of the pushbutton is implemented both as inputs alongside the rest of the pins.

A common mistake here is not setting ALL pins to INPUT in the setup. Why?

As explained before, a HIGH will turn on one led, and a LOW will turn on the other. But if we want neither on, we need to set the Arduino pin completely off, where it only takes in signals - It wont source or sink current. When we need to turn a LED on, we can manually turn the pinMode back to Output then choose a LOW or HIGH value.


Code, Part 2

Screenshot_20230125_073129.png

This part of the code showcases our functions. Lets look at first the LEDs and how we turn them on and off.

Essentially, we number our LED's one through 12, as there are twelve in them in this scenario. The first 6 LEDs each of the pins at HIGH, and the last 6 LEDs are each of the pins at LOW.

Before we turn on the LED with either digitalWrite(pin, HIGH/LOW);, we must use pinMode and set it to OUTPUT, and then once we are done with the LED, we must do the same pinMode and set it back to INPUT in order to completely turn is off.

We delay a certain time between turning on and off the LED. This time is the buffer, defined earlier as the delay time.

This function makes it easy for us to send a simple argument of which LED to turn on, from 1-12, and for that corresponding LED to be turned on, then off, depending on how long the buffer is.

Now lets look at the last two functions. The first function is a simple read of the pin that the potentiometer is connected to, in this instead A0. Make sure you use analogRead instead of digitalRead, another common mistake. We set this read value to the buffer variable, thus allowing us to change the speed of the buffer with the potentiometer.

And the very last function is a simple pushbutton read - whenever we call this function, we quickly read the state of the pushbutton to be used later.

Code, Part 3

Screenshot_20230125_073956.png

Remember in the beginning it was said that the intro variable would start once, then set itself to 1. This is what is happening here. This code will only run after the intro has been completed, as it is enclosed by an if statement. Be aware that this code is in the loop, and is not a function.

Ok, so what is happening here? First we check the potentiometer for changes, and reset the buffer.

Next, remember our variables: rnd only happens once and turns itself to 1. rnd is used to trigger the random number generator, as we using the random() function to call upon a random number. We set that new random number to our target. We additionally display some info about the target LED to the user.

Next, If rnd is not 0 and is instead 1, it means we don't have to generate a new random number. Instead, we will use the current LED, represented by the variable ledWheel, and add one to it. We will additionally turn on that LED by calling our onled function. Now we've essentially turned on the next LED, and the last LED will automatically be turned off by its function.

For the next part, in preparation we use the pushbutton function to read the pushbutton. If the current LED, which is LED wheel, is equal to the target, and the pushbutton is also at 1, we know the user has hit their target. Therefore, we add 1 to our points, output some text about the amount of points the user has, then reset rnd to 0. Because the target has been hit, we will regenerate a new random number which will become the new target.

And finally, if they pushed the pushbutton but did not hit it while the current LED was on the target, we subtract a point and output some info to the user.

And that is the entirety of the functioning game - There is only one part left, and that is the INTRO.

Code, Final Part

Screenshot_20230125_075052.png

And finally, the intro. The variable intro starts on 0, and will set itself to one once it runs. Therefore, it will only run once.

Here, we manually set the buffer between the lights to be 100, since we just want to put up a little bit of a flashing light show for the user as the game starts up, and don't want the potentiometer to play a factor in this.

The actual bulk of the code is fairly simple - We loop through various simple number iterations, and turn on the LED corresponding to the iteration number. Since the LEDs will turn on and off by themselves in the function, this will work perfectly fine on its own. The very last bracket is the end of the loop function, and therefore, our code.


The End

And that is the end of the project! If you want to see an example of how the looping circuit should look like, here is a link down below!

The Circuit