Teach lesson
Basic Arduino 2/4: create a light sequence
Students program four external LEDs with arrays and loops, test a 1-2-3-4 sequence, and turn it into a back-and-forth pattern.
New to LabsLand? Create your teacher account
Learning Outcomes
Use several digital pins as outputs.
Understand a simple for loop.
Modify a light pattern.
Student activity preview
Activity Content
Preview only. In a class session, students can fill in responses and submit their work to the teacher.
One board, several LEDs
9 min
The board has four external LEDs connected to pins 8, 7, 6, and 5. You could write four almost identical blocks of code, but an array and a for loop avoid so much repetition. Think of the array as a list of pins, and the for loop as a way to go through that list.
Look at this part of the activity's base program:
const int totalLeds = 4;
void loop() {
for (int i = 0; i < totalLeds; i++) {
// Turn one LED from the list on and off.
}
}According to that snippet, how many times does the for loop inside loop() repeat?
Program the sequence
18 min
Test this sequence and then modify it so it goes back the other way: LED 1, 2, 3, 4, 3, 2. One simple way is to add a second for loop after the first one. Save the file before attaching the snapshot.
const int leds[] = {8, 7, 6, 5};
const int totalLeds = 4;
void setup() {
for (int i = 0; i < totalLeds; i++) {
pinMode(leds[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < totalLeds; i++) {
digitalWrite(leds[i], HIGH);
delay(300);
digitalWrite(leds[i], LOW);
}
}Open the lab.
Upload the base program and observe the LED order.
Add the return path to create a back-and-forth effect.
Save before capturing the code.
Submit your code
Capture the code for your back-and-forth sequence. Before attaching it, save main.ino in the lab environment. The snapshot must show the program you actually want your teacher to review.
Read the program
8 min
Which line would you change so that each LED stays on for longer? Name the line and the new value you would try.