User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Rainbow Flash with Pots

Submitted by agreiner on Sun, 09/21/2008 - 21:53

Assignment: Sensing: Potentiometers

Collaborators:

Description

This lab was about using potentiometers to sense analog input to control LEDs. After setting things up to control a single LED with a single pot, I chose to do the two-pot variation, where one controls the blink rate and one controls the brightness. Finally, I made my own version where one pot controls the blink rate of three LEDs and the other controls the color mixed from them.

Materials

3 resistors
3 LEDs
wires
breadboard
pots
Arduino board
diffuser (fruit protector with gauze inside)

Arduino Code

/*
* one pot changes the color mixed by three leds, another changes the blink rate
* Annette Greiner 9/21/2008
*/
int potPin1 = 1;    // select the input pin for the blink rate potentiometer
int potPin2 = 2;   // select the input pin for the brightness potentiometer
int redPin = 9;   // select the pin for the red LED, pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11
int brightval = 0;      // variable to store the value coming from the brightness sensor
int blinkval = 0; //variable to store the value coming from the blink rate sensor
int blinkdelay = 0; // variable to store the blink delay time (so higher value gives faster blink)
int redVal, blueVal, greenVal;

void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT);  // declare each pin as an OUTPUT
pinMode(greenPin, OUTPUT);  
pinMode(bluePin,  OUTPUT);
}
void loop() {
blinkval = analogRead(potPin1);    // read the value from the sensor
blinkdelay = 1024-blinkval;
brightval = analogRead(potPin2);    // read the value from the sensor, between 0 - 1024
brightval = (int)(brightval/4);   // analogWrite can be between 0-255
redVal = brightval; //red is at max when brightval is 255 and min when 0
blueVal = (255 - brightval); //blue is at max when brightval is 0 and min when 255
if (brightval <= 127) greenVal = brightval * 2; // green is at max when brightval = 127
else greenVal = (255 - brightval) * 2 ; //and min when brightval = 0 or 255

analogWrite(redPin, redVal); // analogWrite can be between 0-255
analogWrite(greenPin, greenVal); // analogWrite can be between 0-255
analogWrite(bluePin, blueVal); // analogWrite can be between 0-255
delay(blinkdelay);                  // stop the program for some time
analogWrite(redPin, 0); // analogWrite can be between 0-255
analogWrite(greenPin, 0); // analogWrite can be between 0-255
analogWrite(bluePin, 0); // analogWrite can be between 0-255
delay(blinkdelay);                  // stop the program for some time
Serial.print(redVal);   //print out the red, green, and blue values and the delay
Serial.print(" ");
Serial.print(greenVal);
Serial.print(" ");
Serial.print(blueVal);
Serial.print(" with delay of ");
Serial.println(blinkdelay);
}

 

final circuit