Sequence LEDs with If

Let's say you want to sequence LEDs, that is to say turn them on and off one after the other, but you're lazy. You did this in an earlier lesson but your sketch had a lot of repetition. As it turns out, computers are great at repetition. They can do the same thing over and over again while each time changing one thing. Create a circuit with LEDs in pins 9-13. Don't forget you'll need a resistor for each LED.

In this sketch we will let our Arduino do the hard work for us. The Arduino really is a computer, so we'll let it do some repetitive calculations. Load this sketch on your Arduino and see what happens. Hit the reset button on your board to run it again.

This sketch uses no new functions. Everything we're doing here we've done in previous lessons. One of the reasons we use variables when we do a line of code like, digitalWrite (ledPin, HIGH); is so that we can change its value if we need to.

  ledPin=ledPin-1;               //Advance to the next pin
This line of code does just what it appears to say. However, it can appear confusing if you think about it in the math sense, but this is not an algebraic expression. In plane english this says, "Set the new value of ledPin to be the old value of ledPin minus one."

So with ledPin=ledPin-1 in our program the value of ledPin will decrease by one each time through the void loop(). The first time through ledPin is 13 so the LED in pin 13 will light up and then go out. We then subtract one and ledPin is given the value of 12, causing the LED in 12 to go on and then off, and so on. After a few times through it will be lower than our lowest pin and in fact will eventually be a negative number unless we do something about it! Add a bit of code to make it start over again at the LED plugged into pin 13. You may have to take inspiration from the last unit.

Time to play:
    • Add a couple more LEDs and change your code to accommodate them.
    • Make the sequence go in the other direction. That is to say from 9 to 13.