User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Controlling LEDs with Pots

Submitted by criley on Wed, 09/24/2008 - 20:34

Assignment: Sensing: Potentiometers

Collaborators:

Assignment: Sensing: Potentiometers
Collaborators:

I simply used three potentiometers to control three LEDs, but unlike many I harnessed the power of arrays to do it. Yay for short code!

/*
* three pots fade three LEDs
* modified version of AnalogInput
* by Connor Riley
*/
int potPins[] = {0, 1, 2};  // select the input pins for the potentiometers
int ledPins[] = {9, 10, 11}; // select the pins for the LEDs
int vals[] = {0, 0, 0};  // array to store the values coming from the sensors
void setup() {
Serial.begin(9600);
}
void loop() {
readwrite(0);
readwrite(1);
readwrite(2);
}
void readwrite(int num){
vals[num] = analogRead(potPins[num]); // read the value from the sensor, between 0 - 1024
Serial.print(num);
Serial.print(": ");
Serial.println(vals[num]);
analogWrite(ledPins[num], vals[num]/4); // analogWrite can be between 0-255
}