Projects
Project #1: Creating a Blinking LED Wave
Let’s put some LEDs and resistors to work. In this project, we’ll use five
LEDs to emulate the front of the famous TV show vehicle KITT from the
television show Knight Rider, creating a kind of wavelike light pattern.
The Algorithm
Here’s our algorithm for this project:
1. Turn on LED 1.
2. Wait half a second.
3. Turn off LED 1.
4. Turn on LED 2.
5. Wait half a second.
6. Turn off LED 2.
7. Continue until LED 5 is turned on, at which point the process reverses
from LED 5 to 1.
8. Repeat indefinitely.
The Hardware
Here’s what you’ll need to create this project:
• Five LEDs
• Five 560 W resistors
• One breadboard
• Various connecting wires
• Arduino and USB cable
We will connect the LEDs to digital pins 2 through 6 via the 560-ohm
current-limiting resistors.
The Sketch
Now for our sketch. Enter this code into the IDE:
// Project 1 - Creating a Blinking LED Wave
u void setup()
{
pinMode(2, OUTPUT); // LED 1 control pin is set up as an output
pinMode(3, OUTPUT); // same for LED 2 to LED 5
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
44 Chapter 3
v void loop()
{
digitalWrite(2, HIGH); // Turn LED 1 on
delay(500); // wait half a second
digitalWrite(2, LOW); // Turn LED 1 off
digitalWrite(3, HIGH); // and repeat for LED 2 to 5
delay(500);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
delay(500);
digitalWrite(3, LOW);
// the loop() will now loop around and start from the top again
}
Circuit layout for Project 1
Comments
Post a Comment