L3 - Potentiometer-based LED controls

Posted by rowyn

rowyn's picture

Description

Use Arduino to read potentiometer values and control the brightness and blink rate of an LED.

Components Used

  • Light Emitting Diodes (LEDs)
  • Resistors (220 ohms)
  • Potentiometers

Arduino Code

/*
 * one pot dims, the other pot changes the blinking rate
 * modification of the following
 * http://www.arduino.cc/en/Tutorial/AnalogInput
 */
int pot1Pin = 0;   // select the input pin for the potentiometer 1
int pot2Pin = 1;   // 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 ledPin = 9;   // select the pin for the LED
void setup() {
   pinMode(ledPin, OUTPUT);  // declare the led1Pin as an OUTPUT
}
void loop() {
   pot1Val = analogRead(pot1Pin);   // read the value from pot 1, between 0 - 1024, for dimming
   pot2Val = analogRead(pot2Pin);   // read the value from pot 2, between 0 - 1024, for blinking
   
   int delayVal = 1024 - pot2Val;  // higher values from the potentiometer this way correspond to faster blinking
 
   analogWrite(ledPin, pot1Val/4); // dim LED to value from pot1
   delay(delayVal);                  // stop the program for some time, meaning, LED is ON for this time
   analogWrite(ledPin, 0);         // dim LED to completely dark (zero) 
   delay(delayVal);                  // stop the program for some time, meaning, LED is OFF for this time
}
 
pot_circuit_0.jpg
0
Your rating: None
Tags: