This first example is quite easy to follow.
All we are doing is initially clearing every pin on PORTB. This means that as soon as we start running this program, that all LED's will be in the OFF state. From here it starts to look just like it did in our LED FLASHER program. We set PORTB pin 0, thus turning on the first LED.
Then we call the delay as usual to hold that first LED ON just for a moment, then we turn it off. Straight after turning it off - without any delay, we turn the next LED ON and hold it on for a moment by calling the delay. Then we turn it OFF and immediately turn the next successive LED ON - and so on and so on.
Once it gets to the last LED, it simply starts coming back the other way (towards our first LED. once it has come all the way back to our second LED, the program loops back to the beginning of the sourcecode and does it all again over and over untill we remove power.
As you may be able to see, we need quite a few lines of code (and therefor quite a bit of program memory) in order to perform this little task. How can we make it better? We will work towards making our code more efficient - but first let's have a quick look at a different way of achieving the same result.
You can download the second sourcecode here |