Circuit 6

Lots of LED’s

By now you should be getting the hang of it. Unplug, Build the circuit, modify the code from the last example and upload. This time, lets use the same circuit to run a couple of different versions of code.
And finally we get to make a cool game! Make sure you check out the last code block.

Here is the modified circuit, we have just added 4 more LED’s:



What happens when you press the button?
Does it move to the next LED or does it seem random?
This is called Button Bounce! Remove the “//” from the delay(200); line in the code , upload the changes and notice the difference. How small can you make the delay and still have it work correctly?

What is button debounce?

A button is mechanical and when you press it, the signal will flicker from HIGH to LOW many times before it settles. The Arduino will read every pulse and move the ON LED the same amount. So although you think you are pressing it fast it will see a signal something like the following:


Note: This graphic has been simplified, it is a representation only!


This Code cycles the LED’s back and fourth, You can speed it up by changing the delay time.
If we set the delay time as a variable we would only have to change it in one place and it would also make our code more readable if we give it a descriptive name.

We also introduce the For loop. A for loop has 3 components each separated by “;”
for( Initialise ; Test ; Increment/decrement ){
//code to run

In our first loop we initialise currentLed as 2, we test if it is less than or equal 6, if it is we run all the code between the parenthesis “{” and “}” and then we increment currentLed by 1.
currentLed++ is the same as writing currentLed  = currentLed + 1.

See how we reverse it in the third loop?


This game uses everything we have learnt so far. It’s a little more complicated so read through it and see if you can under stand it.

The aim is to press the button every time you land on the middle LED. When you do it speeds up.
When you finally miss one, it will give you a score out of 5.