Skip to main content

Project

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

Popular posts from this blog

Project #2: Repeating with for Loops When designing a sketch, you’ll often repeat the same function. You could simply copy and paste the function to duplicate it in a sketch, but that’s inefficient and a waste of your Arduino’s program memory. Instead, you can use for loops. The benefit of using a for loop is that you can determine how many times the code inside the loop will repeat. To see how a for loop works, enter the following code as a new sketch: Project 2 - Repeating with for Loops int d = 100; void setup() { pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); } void loop() { First Steps 47 for ( int a = 2; a < 7 ; a++ ) { digitalWrite(a, HIGH); delay(d); digitalWrite(a, LOW); delay(d); } } .................................................................................................................................................................. The for loop will repeat the code within th...
Project #3: Demonstrating PWM Now let’s try this with our circuit from Project 2. Enter the following sketch into the IDE and upload it to the Arduino: Project 3 - Demonstrating PWM int d = 5; void setup() { pinMode(3, OUTPUT); // LED control pin is 3, a PWM capable pin } void loop() { for ( int a = 0 ; a < 256 ; a++ ) { analogWrite(3, a); delay(d); } for ( int a = 255 ; a >= 0 ; a-- ) { analogWrite(3, a); delay(d); } delay(200); } .................................................................................................................................................................. The LED on digital pin 3 will exhibit a “breathing effect” as the duty cycle increases and decreases. In other words, the LED will turn on, increasing in brightness until fully lit, and then reverse. Experiment with the sketch and circuit. For example, make five LEDs breathe at once, or have them do so sequentially.