# Theory and Practice of Tangible User Interfaces

### Announcements

November 24, 2007
Reading for November 27th, are now posted. Enjoy!

October 2, 2007
To upload your thoughtless acts, create a new assignment page like any other lab. You'll see "Thoughtless Acts" listed as one of the assignment options.

May 24, 2008
This site has been archived and is no longer editable. Stay tuned for the next version, coming in the fall!

# Lab 3

Project Members:
n8agrin

## Assignment

1. Make the circuit with 3 LEDs (RGB). Make the RGB LED Fade from one of the codes
2. Take 6 wires (2 red, 2 yellow, and 2 black) and strip off about ΒΌβ of insulation at both ends of each wire
3. Solder the 3 wires (red, yellow, and black) to the pot. Total of two pots.
4. Make one LED to dim and blink at different rate
5. Use multiple pots to control your LEDs
Option 1: One pot controls brightness, another pot controls blinking
Option 2: 3 Pots for 3 LEDs (each pot controls brightness of RGB Hue / colors

Homework:

Come up with an interesting mapping between the rotational position pot and LED output

## Description

I attempted several different mappings of two pots to various behaviors of the LEDs. In some cases, the pot controlled the rate of blinking, in others the hue of the color displayed by the diffuser.

## Components Used

• 1 red, 1 blue, 1 green led
• 3 220-ohm resistors
• 1 diffuser
• 2 potentiometers
• 7 lengths of wire
• 1 arduino board

## Code

#### Two blinking lights controlled by one POT

/*
* 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.
*
*
* 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() {
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
* by DojoDave <http://www.0j0.org>
*/
int brightnessPotPin = 2; // select the input pin for the potentiometer

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

void setup() {
Serial.begin(9600);
}
void loop() {
brightness = analogRead(brightnessPotPin); // read the value from the sensor, between 0 - 1024

Serial.println(brightness);

analogWrite(greenPin, brightness/4); // analogWrite can be between 0-255
analogWrite(bluePin, brightness/4);
analogWrite(redPin, brightness/4);

analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
analogWrite(redPin, 0);
}
}

#### One Pot,controls the hue, 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));
}