Code
Two blinking lights controlled by one POT
/*
* 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.
*
* http://www.arduino.cc/en/Tutorial/AnalogInput
*
* Simple Modification to run TWO, yes TWO blinking LEDs
* by n8agrin.
*/
int potPin = 2; // select the input pin for the potentiometer
int bluePin = 11; // select the pin for the LED
int greenPin = 10;
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(bluePin, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(greenPin, OUTPUT);
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(bluePin, LOW); // turn the ledPin on
digitalWrite(greenPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(bluePin, HIGH); // turn the ledPin off
digitalWrite(greenPin, LOW);
delay(val); // stop the program for some time
}
Two POTs, one controls brightness, the other blinking
/*
* one pot fades one led
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int brightnessPotPin = 2; // select the input pin for the potentiometer
int blinkPotPin = 3;
int greenPin = 9; // select the pin for the LED
int bluePin = 10;
int redPin = 11;
int brightness = 0; // variable to store the value coming from the sensor
int blink = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
brightness = analogRead(brightnessPotPin); // read the value from the sensor, between 0 - 1024
blink = analogRead(blinkPotPin);
Serial.println(brightness);
Serial.println(blink);
analogWrite(greenPin, brightness/4); // analogWrite can be between 0-255
analogWrite(bluePin, brightness/4);
analogWrite(redPin, brightness/4);
if (blink > 1) { //added this to elminate some weird blinking artifacts
delay(blink);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
analogWrite(redPin, 0);
delay(blink);
}
}
One Pot,controls the hue, or the other controls brightness
/*
* Code for making one potentiometer control 3 LEDs, red, grn and blu, or one tri-color LED
* The program cross-fades from red to grn, grn to blu, and blu to red
* Debugging code assumes Arduino 0004, as it uses Serial.begin()-style functions
* Clay Shirky <clay.shirky@nyu.edu>
*
* n8agrin small mod to control brightness with a pot as well as hue
*/
// INPUT: Potentiometer should be connected to 5V and GND
int potPin = 3; // Potentiometer output connected to analog pin 3
int potVal = 0; // Variable to store the input from the potentiometer
int brightPin = 2;
float brightVal = 0;
// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED's cathodes should be connected to digital GND
int redPin = 9; // Red LED, connected to digital pin 9
int grnPin = 10; // Green LED, connected to digital pin 10
int bluPin = 11; // Blue LED, connected to digital pin 11
// Program variables
int redVal = 0; // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;
int DEBUG = 1; // Set to 1 to turn on debugging output
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
}
// Main program
void loop()
{
potVal = analogRead(potPin); // read the potentiometer value at the input pin
brightVal = ((float) analogRead(brightPin) / 1023); // get the brightness (0-1023)
if (potVal < 341) // Lowest third of the potentiometer's range (0-340)
{
potVal = (potVal * 3) / 4; // Normalize to 0-255
redVal = 256 - potVal; // Red from full to off
grnVal = potVal; // Green from off to full
bluVal = 1; // Blue off
}
else if (potVal < 682) // Middle third of potentiometer's range (341-681)
{
potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255
redVal = 1; // Red off
grnVal = 256 - potVal; // Green from full to off
bluVal = potVal; // Blue from off to full
}
else // Upper third of potentiometer"s range (682-1023)
{
potVal = ( (potVal-683) * 3) / 4; // Normalize to 0-255
redVal = potVal; // Red from off to full
grnVal = 1; // Green off
bluVal = 256 - potVal; // Blue from full to off
}
analogWrite(redPin, (int) (redVal * brightVal)); // Write values to LED pins
analogWrite(grnPin, (int) (grnVal * brightVal));
analogWrite(bluPin, (int) (bluVal * brightVal));
}