Friday, July 22, 2016

Filled Under: , ,

For Loop Iteration (aka The Knight Rider)

05. Control For Loop Iteration library

Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs attached to the Arduino by using a for() loop to cycle back and forth through digital pins 2-7. The LEDs are turned on and off, in sequence, by using both the digitalWrite() and delay() functions .

We also call this example "Knight Rider" in memory of a TV-series from the 80's where David Hasselhoff had an AI machine named KITT driving his Pontiac. The car had been augmented with plenty of LEDs in all possible sizes performing flashy effects. In particular, it had a display that scanned back and forth across a line, as shown in this exciting fight between KITT and KARR. This example duplicates the KITT display.




Step 1: What You Need?



1 x Arduino Board ( Arduino UNO R3 used in this tutorial.)
6 x LEDs (RED)

6 x 220 Ohm Resistors
1 x Mini Breadboard 
1 x USB Type-B Cable 
Male-to-Male Jumper Wires 


Don't have components? Don't worry. Just click the component's name. 


Step 2: Build Your Circuit.



Connect six LEDs, with 220 ohm resistors in series, to digital pins 2-7 on your board.



Step 3: Upload The Code.




1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.



2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.


Select the right port: Tools >> Port >> Select the port number.


3. You can find this code in the example of Arduino IDE. 

Select File >> Examples >> 05. Control >> ForLoopIteration

Click press the "upload" button (see the button with right arrow mark).



The code below begins by utilizing a for() loop to assign digital pins 2-7 as outputs for the 6 LEDs used.

In the main loop of the code, two for() loops are used to loop incrementally, stepping through the LEDs, one by one, from pin 2 to pin seven. Once pin 7 is lit, the process reverses, stepping back down through each LED.


Download: 



Unknown

Author & Editor

My Robot Education Sdn. Bhd. (Robotedu.my) was founded in 2015 as the first robotics education centre in Malaysia to provide Arduino-based robotics courses for youths. Our vision is to be able to provide robotics education to every youth in Malaysia.

0 comments:

Post a Comment

 

  • Copyright © Arduino Tutorial ™ is a registered trademark.
    Designed by Templateism. Hosted on Blogger Templates.