User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Assignment 2: Digital Input and Output with Arduino Boards + Diffuser

Submitted by michael_lee on Fri, 09/12/2008 - 22:35

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

Description

For this lab, I repeated what I did for the previous lab x3 using the PWM digital pins. For the diffuser, I used a styrofoam peanut, and the measuring/dosage cup that comes with DayQuil medicine. The peanut by itself wasn't a good diffuser, but with the addition of the translucent cup on top, it was able to make a more uniform color mixture.

Components Used

  • x3 220 Ohm resistors
  • x3 LEDs - red, green, and blue
  • Wires
  • 1 Arduino NG board
  • 1/2 packing peanut
  • Dosage cup from Vick's Dayquil Medicine

Code + Extras

For the code, I have the assignment's required pressing "b," "g," or "r" increase the intensity of the specific LED up by 10% each time it is entered. Once it reaches 100% intensity, the LED resets.

For the extra portion, I have the LEDs work together based on a command. First, there are color combinations. If you enter the following into the monitor, the intensity of all three LEDs will change accordingly, making the color specified: "orange", "red", "blue", "green", and "purple".

Additionally, sending the following to the monitor does different things:

  • "off" - turns off all the LEDs (sets all three LEDs to analog value 0)
  • "party" - begins cheesy sequence of blinking LEDs making various colors.

 

Images

Full Setup

(full setup)

 

LEDs

(LEDs)

 

Peanut

(with peanut diffuser)

 

Cup

(with peanut and cup diffuser)

 

Ardunio Code

#include <string.h>

char serInString[100];
char colorCode;
int colorVal;

int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11

int redValue = 0;
int greenValue = 0;
int blueValue = 0;

boolean flag[] = {false, false, false};

void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("Enter color command (e.g. 'rrrrrrrrbbbb') :");
}

void loop () {
readSerialString(serInString, 100);

if (!checkSpecial(serInString))
processRepeatKeyCommands(serInString, 100);

resetSerialString(serInString, 100);
delay(100); // wait a bit, for serial data
}

void resetSerialString (char *strArray, int length) {
for (int i = 0; i < length; i++) {
strArray[i] = '\0';
}
}

boolean checkSpecial(char *serInString){

boolean matches = false;
int newredValue, newgreenValue, newblueValue;

if (stringsEqual(serInString, "party", 5)) {
Serial.println("\nParty Time!\n");
for (int j = 0; j < 50; j++){
analogWrite(random(9,12), 255);
delay(random(100,200));
analogWrite(random(9,12), 0);
delay(random(100,200));
delay(10);
}
//reset to original intensities after finishing "party"
newredValue = redValue;
newgreenValue = greenValue;
newblueValue = blueValue;
matches = true;
}
else if (stringsEqual(serInString, "orange", 6)){
newredValue = 179;
newgreenValue = 127;
newblueValue = 0;
matches = true;
Serial.println("\nSet to \"orange\"!\n");
}
else if (stringsEqual(serInString, "purple", 6)){
newredValue = 128;
newgreenValue = 0;
newblueValue = 128;
matches = true;
Serial.println("\nSet to \"purple\"!\n");
}
else if (stringsEqual(serInString, "red", 3)){
newredValue = 255;
newgreenValue = 0;
newblueValue = 0;
matches = true;
Serial.println("\nSet to \"red\"!\n");
}
else if (stringsEqual(serInString, "green", 5)){
newredValue = 0;
newgreenValue = 255;
newblueValue = 0;
matches = true;
Serial.println("\nSet to \"green\"!\n");
}
else if (stringsEqual(serInString, "blue", 4)){
newredValue = 0;
newgreenValue = 0;
newblueValue = 255;
matches = true;
Serial.println("\nSet to \"blue\"!\n");
}
else if (stringsEqual(serInString, "off", 3)){
newredValue = 0;
newgreenValue = 0;
newblueValue = 0;
matches = true;
Serial.println("\nLEDs turned off!\n");
}

if (matches){
analogWrite(redPin, newredValue);
analogWrite(greenPin, newgreenValue);
analogWrite(bluePin, newblueValue);
}
return matches;
}



void readSerialString (char *strArray, int maxLength) {
int i = 0;

if(!Serial.available()) {
return;
}
while (Serial.available() && i < maxLength) {
strArray[i] = Serial.read();
i++;
}
}



void processRepeatKeyCommands(char *strArray, int maxLength) {
int i = 0;

while (i < maxLength && strArray[i] != '\0') {
//Read in the character at position i in the string
colorCode = serInString[i];

//If the character is r (red)...
if (colorCode == 'r') {
//Increase the current red value by 10, and if you reach 255 go back to 0
redValue = (redValue + increment(0)) % 255;
if (redValue == 0)
redValue= 255;
analogWrite(redPin, redValue);
Serial.print("R set to ");
percentage(redValue);

//If the character is g (green)...
} else if (colorCode == 'g') {
greenValue = (greenValue + increment(1)) % 255;
if (greenValue == 0)
greenValue= 255;
analogWrite(greenPin, greenValue);
Serial.print("G set to ");
percentage(greenValue);

//If the character is b (blue)...
} else if (colorCode == 'b') {
blueValue = (blueValue + increment(2)) % 255;
if (blueValue == 0)
blueValue= 255;
analogWrite(bluePin, blueValue);
Serial.print("B set to ");
percentage(blueValue);
}
i++;
}
}

int increment(int index) {
if (flag[index]){
flag[index] = false;
return 26;
}
else{
flag[index] = true;
return 25;
}
}

void percentage(int value){
switch (value) {
case 25: Serial.println("10%");
break;
case 51: Serial.println("20%");
break;
case 76: Serial.println("30%");
break;
case 102: Serial.println("40%");
break;
case 127: Serial.println("50%");
break;
case 153: Serial.println("60%");
break;
case 178: Serial.println("70%");
break;
case 204: Serial.println("80%");
break;
case 229: Serial.println("90%");
break;
case 255: Serial.println("100%");
break;
default: Serial.println("Unknown");
break;
}
}


boolean stringsEqual(char *string1, char *string2, int numCharacters) {
if (strncmp(string1, string2, numCharacters) == 0) {
return true;
} else {
return false;
}
}