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 pinThis 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: