3. Pots and Blinking/Cycling LEDs

Posted by Erich

Erich's picture

The two potentiometers used control the blink-rate of the LEDs and which LED is lit up.

Video: http://www.youtube.com/watch?v=os9jFhn9iQo

 

 

/*
 * Erich Hacker
 * one pot cycles, the other pot changes the blinking rate
 * modification of the following
 * http://www.arduino.cc/en/Tutorial/AnalogInput
 */
int pot1Pin = 5;   // select the input pin for the potentiometer 1
int pot2Pin = 4;   // select the input pin for the potentiometer 2
int pot1Val = 0;   // variable to store the value coming from pot 1
int pot2Val = 0;   // variable to store the value coming from pot 2 
int led1Pin = 9;   // pin for the LED 1
int led2Pin = 10;  // pin for the LED 2
int led3Pin = 11;  // pin for the LED 3
int led = 1;       // pin for cycled LED
 
void setup() {
   pinMode(led1Pin, OUTPUT);  // declare the led1Pin as an OUTPUT
   pinMode(led2Pin, OUTPUT);  // declare the led2Pin as an OUTPUT
   pinMode(led3Pin, OUTPUT);  // declare the led3Pin as an OUTPUT
}
void loop() {
   pot1Val = analogRead(pot1Pin);   // read pot 1, between 0 - 1024, for cycling
   pot2Val = analogRead(pot2Pin);   // read pot 2, between 0 - 1024, for blinking
   
   if (pot1Val < 340){ 
     led=led1Pin;
   } else if (pot1Val < 680){
     led=led2Pin;
   } else {
     led=led3Pin;
   }
   
   digitalWrite(led, HIGH);         // dim LED to value from pot1
   delay(pot2Val);                  // stop the program for some time, meaning, LED is on for this time
   digitalWrite(led, LOW);      // dim LED to completely dark (zero) 
   delay(pot2Val);                  // stop the program for some time, meaning, LED is OFF for this time
}
Circuit with 2 Pots
0
Your rating: None
Tags: