Exercise 2—Back to Arduino
Rewrite the last program using an array:
int pinArray[] = {2, 3, 4, 5, 6, 7, 8, 9};
int timer = 100;
Look at your solution to the
bike light (you'll have to remove the interval code and add a timer variable)
- Unfortunately, you can't use a loop to set the pinMode so you'll have to do something like this in setup():
pinMode(pinArray[0],OUTPUT);
pinMode(pinArray[1],OUTPUT);
pinMode(pinArray[2],OUTPUT);
- When your lightOn variable is true turn the lights on, then off. Between the 2 loops add a slight delay.
While this works with the delay, it's not great, you'll have to hold the button down until the delay is complete and the chip can poll the button again.
Exercise 3—Just 1 at a time
By lighting the next led shortly after the current led is turned off, creates a different effect.
Using the last program modify the loops(). Here is the modification for the first loop:
digitalWrite(pinArray[your_var], HIGH);
delay(timer);
digitalWrite(pinArray[your_var], LOW);
delay(timer*2);
Exercise 4—Shooting Star
Connect 11 LEDs to 11 arduino digital pins, through a 220 Ohm resistance.
The program should light up LEDs until it reaches the number of LEDs equal to the size you have established for the tail.
Then it will continue lighting LEDs towards the left to make the line keep moving,
and will start erasing from the right, to make sure you see the tail (otherwise you would just light up the whole line of leds,
this will happen also if the tail size is equal or bigger than 11).
The tail size should be relatively small in comparison with the number of LEDs in order to see it.
Copy, paste and complete the following:
// Variable declaration
int pinArray [] = ___________; // Array of LED pins
int timer = 100; // delay
int tailLength = 4; // Num of LEDs that stay lit before you start turning them off
int lineSize = 11; // Number of LEDs connected
void setup(){
//turn all your leds to OUTPUTS
pinMode(pinArray[0],OUTPUT);
______________________
______________________
______________________
______________________
______________________
...
}
// Main loop
void loop(){
int tailCounter = tailLength; // set up the tail length in a counter
for (int i=0; i<length_of_array; i++){
// light up the LEDs consecutively
__________________________
// Add a delay. This time variable controls how fast you light them up
______________
if (tailCounter == 0){
// turn off the LEDs depending on tailLength
digitalWrite(pinArray[i-tailLength],___);
}else if (tailCounter > 0)
tailCounter--;
}
for (int i=0; i<length_of_array; i++){
// turn off the LEDs
digitalWrite(________);
//pause
_____________
}
}
Combine the code above with your toggle switch.