Description
I modified the Analog Input code found on the Arduino website to have one potentiometer control the blinking rates of two LEDs. In addition, the code has one LED shine at a time, making the LEDs resemble the on and off blinking of christmas lights. (I used green and red lights to enhance this effect.)
Components
- Previous lab setup: green and red LEDs, resistors, breadboard, arduino, yellow and green wires
- Potentiometer with three soldered wires, red yellow and black wires
Code
/*
* AnalogInput
* by DojoDave <http://www.0j0.org>
*
* Turns on and off a light emitting diode(LED) connected to digital
* pin 13. The amount of time the LED will be on and off depends on
* the value obtained by analogRead(). In the easiest case we connect
* a potentiometer to analog pin 2.
*
* In Jess' modified version the code turns on and off two LEDs connected
* to digital pins 9 and 11. The potentiometer is connected to analog pin
* 0.
*/
int potPin = 0; // select the input pin for the potentiometer
int ledPin1 = 9; // select the pin for the first LED
int ledPin2 = 11; // select the pin for the second LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin1, OUTPUT); // declare the ledPin1 as an OUTPUT
pinMode(ledPin2, OUTPUT); // declare the ledPin2 as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin1, HIGH); // turn the ledPin1 on
digitalWrite(ledPin2, LOW); // turn the ledPin2 on
delay(val); // stop the program for some time
digitalWrite(ledPin1, LOW); // turn the ledPin off
digitalWrite(ledPin2, HIGH); // turn the ledPin off
delay(val); // stop the program for some time
}
Pictures
Dual color display as a result of fast blinking
Single color display as a result of slow blinking