Worksheet Lesson 2:
Festive LED Patterns
with Array
Objectives:
Understand how to use arrays to control multiple LEDs with Arduino.
Learn to create custom functions for organizing repetitive tasks in Arduino sketches.
Build LED patterns using looping structures and arrays.
Part 1: Pre-Lab Questions
1. What is an array in Arduino, and how does it simplify working with multiple LEDs?
2. How does a for loop work, and why is it useful when dealing with arrays?
3. What are custom functions in Arduino programming, and how can they make your code more
efficient?
Part 2: Circuit Assembly
Components Needed:
Arduino board
8 LEDs
8 Resistors (220Ω)
Breadboard
Jumper wires
Wiring Instructions:
1. Place all 8 LEDs on the breadboard with the
anodes (long legs) facing the left.
2. Connect each LED's cathode (short leg) to GND
through a 220Ω resistor.
3. Connect the anodes of the LEDs to Arduino pins 2
to 9 using jumper wires.
Part 3: Programming Task
Upload the following code to your Arduino and observe the LED patterns:
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // Array storing LED pin numbers
const int numLeds = 8; // Number of LEDs
void setup() {
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT); // Set each LED pin as an output
}
}
void loop() {
chasePattern(100); // Call the chase pattern with a delay of 100ms
blinkAll(200); // Call the blink pattern with a delay of 200ms
}
void chasePattern(int delayTime) {
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], HIGH); // Turn on one LED at a time
delay(delayTime);
digitalWrite(ledPins[i], LOW); // Turn it off before moving to the next
}
}
void blinkAll(int delayTime) {
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], HIGH); // Turn on all LEDs
}
delay(delayTime);
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW); // Turn off all LEDs
}
delay(delayTime);
}
Part 4: Questions & Modifications
1. What happens when you change the value of delayTime in the function calls?
2. How does the for loop help simplify turning LEDs on and off?
Modifications:
1. Adjust the code so the LEDs chase in the reverse order (from pin 9 to pin 2).
2. Create a new pattern where LEDs light up two at a time, moving in pairs.
Part 5: Advanced Task
Task 1: Add a button to the circuit. Modify the code so:
The LEDs chase when the button is pressed.
All LEDs blink rapidly when the button is released.
Task 2: Create a toggle mode: pressing the button once starts the LED chase pattern, pressing it
again switches to the blink pattern, and so on.
Part 6: Challenge Task
Task:
Design a new pattern combining both chase and blink patterns.
Upload your step-by-step procedure for making the circuit.
Upload a copy of your code.
The End