Project 1.04 LED chase!
We’ve only used two LEDs so far, but on the Arno board we can control a total of four single LEDs and the RGB LED. In this project we’ll use the four single LEDs to create a “chase” illusion. Upload this project and watch the LEDs run in circles around the board.
Concepts: for loop, nested loop, arrays
Circuits:
Concepts: for loop, nested loop, arrays
Circuits:
We use an int array to access the four LEDs rather than referencing each one separately. This simplifies our sketch since we can use a for loop to go through elements 0 to 3 of the array, which contain the pin numbers for the single LEDs:
int LEDS[4] = {6,7,8,13};
We use our first for loop in the setup() block to set the pinMode of each LED. Remember, that the for loop repeats everything in the block that starts with { and ends with } until the conditional statement is no longer true. We start with k=0 since the first element in an array is always 0:
for(int k = 0; k < 4; k++){
pinMode(LEDS[k],OUTPUT);
}
In the loop() block we use nested for loops. This means that one loop is inside of the other. The outer loop controls how quickly we move between the LEDs. It decrements the variable wait between 250 and 10 in steps of 3:
for(wait = 250; wait >= 10; wait = wait - 3){
The inner loop goes through each of its values for every cycle of the outer loop. You might think of these loops like gears. Picture a small gear (the inner loop) up against a larger gear (the outer loop). Every time the large gear turns once, the small gear turns many times. Our inner loop cycles through each of the LEDs, switching them on and off according to the value of wait:
for(int k = 0; k < 4; k++){
digitalWrite(LEDS[k],HIGH);
delay(wait);
digitalWrite(LEDS[k],LOW);
}
Before we finish up, we need the closing brackets for the outer loop and the loop() block:
}
}
Now we’re done!
int LEDS[4] = {6,7,8,13};
We use our first for loop in the setup() block to set the pinMode of each LED. Remember, that the for loop repeats everything in the block that starts with { and ends with } until the conditional statement is no longer true. We start with k=0 since the first element in an array is always 0:
for(int k = 0; k < 4; k++){
pinMode(LEDS[k],OUTPUT);
}
In the loop() block we use nested for loops. This means that one loop is inside of the other. The outer loop controls how quickly we move between the LEDs. It decrements the variable wait between 250 and 10 in steps of 3:
for(wait = 250; wait >= 10; wait = wait - 3){
The inner loop goes through each of its values for every cycle of the outer loop. You might think of these loops like gears. Picture a small gear (the inner loop) up against a larger gear (the outer loop). Every time the large gear turns once, the small gear turns many times. Our inner loop cycles through each of the LEDs, switching them on and off according to the value of wait:
for(int k = 0; k < 4; k++){
digitalWrite(LEDS[k],HIGH);
delay(wait);
digitalWrite(LEDS[k],LOW);
}
Before we finish up, we need the closing brackets for the outer loop and the loop() block:
}
}
Now we’re done!