Project 3.05 LED Chase, Part II
In this project, we take control of the LED Chase! (Project 1.03) using the thumbwheel potentiometer and the SW1 button. We use the potentiometer to adjust the speed of the chase and we reverse its direction by pressing SW1.
Concepts: ADC, analogRead, arrays, debounce
Circuits:
Concepts: ADC, analogRead, arrays, debounce
Circuits:
Arrays help to simplify this program. The pin numbers for the single LEDs are contained in the array LEDS:
int LEDS[4] = {6,7,8,13};
The potentiometer value is used to set when to switch to the next LED. The variable now records when the LEDs where last switched. The next switch occurs when millis() is greater than now + potRead. We advance to the next LED in the array using the variable steps which can be either 1 (move forward in the array) or -1 (move backward):
potRead = analogRead(POT1)/4;
if(millis() > now + potRead){
digitalWrite(LEDS[thisLED],LOW);
thisLED = thisLED + steps;
if(thisLED < 0) thisLED = 3;
if(thisLED > 3) thisLED = 0;
digitalWrite(LEDS[thisLED],HIGH);
now = millis();
}
The variable steps switches between 1 and -1 when SW1 is pressed. This changes the direction of rotation. We use the variable lastPress to record when steps was last switched. We debounce SW1 by not allowing it to change steps more than once per second:
if(isPress==LOW && millis() > lastPressed + 1000){
steps = steps*-1;
lastPressed = millis();
}
int LEDS[4] = {6,7,8,13};
The potentiometer value is used to set when to switch to the next LED. The variable now records when the LEDs where last switched. The next switch occurs when millis() is greater than now + potRead. We advance to the next LED in the array using the variable steps which can be either 1 (move forward in the array) or -1 (move backward):
potRead = analogRead(POT1)/4;
if(millis() > now + potRead){
digitalWrite(LEDS[thisLED],LOW);
thisLED = thisLED + steps;
if(thisLED < 0) thisLED = 3;
if(thisLED > 3) thisLED = 0;
digitalWrite(LEDS[thisLED],HIGH);
now = millis();
}
The variable steps switches between 1 and -1 when SW1 is pressed. This changes the direction of rotation. We use the variable lastPress to record when steps was last switched. We debounce SW1 by not allowing it to change steps more than once per second:
if(isPress==LOW && millis() > lastPressed + 1000){
steps = steps*-1;
lastPressed = millis();
}