# Lab 3

Submitted by zwasson on Tue, 02/19/2013 - 22:59

Description

The purpose of this lab was to explore analog input using potentiometers. First, a single potentiometer was used to vary the intensity of the LEDs. Next, a second potentiometer was added in order to vary the rate at which the LEDs blinked. In the code below, this functionality is the commented out portion.

The next part of the lab was to create an interesting mapping between the potentiometers and color. For this portion of the lab, I used three potentiometers (call them x, y, and z). The straightforward mapping of these potentiometers to color is one where x controls red, y controls green, and z controls blue. Instead, I used three linearly independent equations in x, y, and z to compute the intensity of red, green, and blue. The equations I used were as follows:

red = x - y - z
green = x - y + z
blue = x + y - z

For negative values, I added x+=256 in order to wrap the values around. Thus -1 maps to 255, -2 maps to 254, and so on. The end result is quite unintuitive, but it makes for an interesting puzzle if you try to set the LEDs to a particular color.

## Components

• Arduino Uno
• Red LED
• Green LED
• Blue LED
• 3 220ohm resistors
• 3 Potentiometers
• Packing Peanut
• Ping Pong Ball

## Code

/*
Lab3
This lab uses potentiometers to set RGB values for 3 LEDs.

Zach Wasson
*/

// pin definitions
int red_led = 11;
int green_led = 10;
int blue_led = 9;
int potentiometer0 = A0;
int potentiometer1 = A1;
int potentiometer2 = A2;

// RGB values
int red_val = 0;
int green_val = 0;
int blue_val = 0;

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pins as outputs.
pinMode(red_led, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(blue_led, OUTPUT);

Serial.begin(115200);
}

// the loop routine runs over and over again forever:
void loop() {
int plusplusplus = (analogRead(potentiometer0) >> 2) / 3;
int minusminusplus = (analogRead(potentiometer1) >> 2) / 3;
int minusplusminus = (analogRead(potentiometer2) >> 2) / 3;

red_val = plusplusplus - minusminusplus - minusplusminus;
if(red_val < 0)
red_val += 256;
green_val = plusplusplus - minusminusplus + minusplusminus;
if(green_val < 0)
green_val += 256;
blue_val = plusplusplus + minusminusplus - minusplusminus;
if(blue_val < 0)
blue_val += 256;

analogWrite(red_led, red_val);
analogWrite(green_led, green_val);
analogWrite(blue_led, blue_val);

Serial.print("red: ");
Serial.println(red_val);
Serial.print("green: ");
Serial.println(green_val);
Serial.print("blue: ");
Serial.println(blue_val);
Serial.println();
delay(1000);

/*
//
int intensity = analogRead(potentiometer0) >> 2;
if(rate < 100)
rate = 100;
Serial.println(intensity);
analogWrite(red_led, intensity);
analogWrite(green_led, intensity);
analogWrite(blue_led, intensity);
delay(rate);
analogWrite(red_led, 0);
analogWrite(green_led, 0);
analogWrite(blue_led, 0);
delay(rate);
*/
}

Drupal theme by Kiwi Themes.