Project 3.04 Potentiometer sets blink rate
Like Project 3.03, we use the input from the potentiometer to control an LED. In this case, we change the blink rate of the red channel of the RGB LED based on the potentiometer’s output voltage.
Concepts: ADC, analogRead, analogWrite
Circuits:
Concepts: ADC, analogRead, analogWrite
Circuits:
As in previous projects, we don’t want to use a delay statement to control the blink rate since the potentiometer can’t be read during the delay period. Instead, we record the time since the LED was last switch on or off using the variable now. The LED is switch again when the time exceeds now plus the value read by the potentiometer. We also take advantage of the fact that we can replace HIGH and LOW with 1 and 0 in the digitalWrite statement:
if(millis() > now + potRead){
digitalWrite(redLED,isOn);
isOn = 1 - isOn;
now = millis();
}
Remember, the value of the potentiometer can range between 0 and 1023. This means that our blink rate will be between 0 and and 1023 milliseconds between cycles. We could make the range shorter by dividing the value of potRead by an integer, or longer by multiplying it by an integer. Alternately, we could set the range using the map function.
if(millis() > now + potRead){
digitalWrite(redLED,isOn);
isOn = 1 - isOn;
now = millis();
}
Remember, the value of the potentiometer can range between 0 and 1023. This means that our blink rate will be between 0 and and 1023 milliseconds between cycles. We could make the range shorter by dividing the value of potRead by an integer, or longer by multiplying it by an integer. Alternately, we could set the range using the map function.