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!


Revision of LEDs and Poofballs: Digital Input and Outputs from Wed, 09/12/2007 - 14:44

Project Members: 
Lora Oehlberg

Description

My circuit is a basic three-LED control circuit using 220 Ohm resistors and three of the PWM/Digital Out pins on the Arduino.

My diffuser is a domed poofball made out of a crocheted safeway bag. I also used a twist tie at the bottom to add more structure around the base.

The modified keyboard input maps the alphabet to the three different LEDs. The user can type in a word into the serial input, and the LEDs will then adjust their brightness depending on how many letters are a) vowels (which varies the red), b) consonants rhyming with "EE" (for example the letters B, C, D, P, and Z), or c) consonants that do now rhyme with "EE" (for example the letters L, F, U, or Y). A "space" will "clear" the LEDs so the user can start again on a fresh "palate" of LED brightness.

Components Used

Circuit:

  • LEDs (Lab-Kit provided Blue and Green, my own Red)
  • Three Red-Red-Brown Resistors (220 Ohms)
  • Arduino

Diffuser:

  • Plastic Shopping Bag
  • Wire Twist Tie

Arduino Code

/* CNM 290-01: Theory and Practice of Tangible User Interfaces
* UC Berkeley, Fall 2007
*
* Original Code from TUI website (Tod Kurt, Ryan Aipperspach)
* Modified by Lora Oehlberg for TUI, Fall 2007
*/

char serInString[100]; //

char colorCode;
int colorVal;

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

int greenValue = 0; // Initialized Intensity of Green Pin (0<greenVal <255)
int redValue = 0; // Initialized Intensity of Green Pin (0<greenVal <255)
int blueValue = 255;

int i = 0; // Fade Counter (0 < i < 255)
int wait = 10;
int mode = 2; // indicates if the LED mode is fading (mode = 1) or responding to direct commands (mode = 2)

void setup()
{
pinMode(greenPin, OUTPUT); // sets the digital pin as output
pinMode(redPin,OUTPUT); // sets redLEDPin as output
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(greenPin, greenValue);
analogWrite(redPin, redValue);
analogWrite(bluePin, blueValue);
Serial.println("enter color command:");
}

void loop()
{
if (mode == 1){
i += 1;
if (i<254){
greenValue += 1;
redValue += 1;
blueValue -= 1;
}
else if (i<506){
greenValue -= 1;
redValue -= 1;
blueValue += 1;
}
else {
i = 1;
}
analogWrite(greenPin, greenValue);
analogWrite(redPin, redValue);
analogWrite(bluePin, blueValue);
}
else if (mode == 2){
//read the serial port and create a string out of what you read
readSerialString(serInString, 100);

//UNCOMMENT ONE OF THE FOLLOWING COMMANDS, OR NOTHING WILL HAPPEN WHEN YOU
//RUN THE PROGRAM...
//Uncomment the following line to read commands of the form 'r245' or 'b3'
//processNumericalCommands(serInString);
//Uncomment the following line to read commands of the form 'rrrb'
//processRepeatKeyCommands(serInString, 100);
//Or write your own function...
AlphabetSoup(serInString, 100);


//Erase anything left in the serial string, preparing it for the
//next loop
resetSerialString(serInString, 100);

}

// DEBUGGING!

// Serial.print(i);
// Serial.print("\t");
// Serial.print("G:");
// Serial.println(greenValue);
delay(wait);

}

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

//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray, int maxLength) {
int i = 0;

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

//go through the string, and increase the red value for each 'r',
//the green value for each 'g', and the blue value for each 'b'.
//For example "rrrg" increases red by 30 and green by 10.
void processRepeatKeyCommands(char *strArray, int maxLength) {
int i = 0;
//loop through the string (strArray)
//i = the current position in the string
//Stop when either (a) i reaches the end of the string or
// (b) there is an empty character '\0' in the string
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 + 10) % 255;
analogWrite(redPin, redValue);
Serial.print("setting color r to ");
Serial.println(redValue);
//If the character is g (green)...
} else if (colorCode == 'g') {
greenValue = (greenValue + 10) % 255;
analogWrite(greenPin, greenValue);
Serial.print("setting color g to ");
Serial.println(greenValue);
//If the character is b (blue)...
} else if (colorCode == 'b') {
blueValue = (blueValue + 10) % 255;
analogWrite(bluePin, blueValue);
Serial.print("setting color b to ");
Serial.println(blueValue);
}
//Move on to the next character in the string
//From here, the code continues executing from the "while" line above...
i++;
}
}

//change the value of the red, green, or blue LED according to the command received.
//for example, r240 sets the red LED to the value 240 (out of 255)
void processNumericalCommands(char *strArray) {
//read in the first character in the string
colorCode = serInString[0];
//if the first character is r (red), g (green) or b (blue), do the following...
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
//convert the string to an integer
//(start at the second character, or the beginning of the string '+1')
colorVal = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();

if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
}
}

//go through the string, and increase the red value for each 'r',
//the green value for each 'g', and the blue value for each 'b'.
//For example "rrrg" increases red by 30 and green by 10.
void AlphabetSoup(char *strArray, int maxLength) {
int i = 0;
//loop through the string (strArray)
//i = the current position in the string
//Stop when either (a) i reaches the end of the string or
// (b) there is an empty character '\0' in the string
while (i < maxLength && strArray[i] != '\0') {
//Read in the character at position i in the string
colorCode = serInString[i];
//If the character is a vowel (red)...
if (colorCode == 'a' || colorCode == 'e' || colorCode == 'i' || colorCode == 'o' || colorCode == 'u') {
//Increase the current red value by 10, and if you reach 255 go back to 0
redValue = (redValue + 10) % 255;
analogWrite(redPin, redValue);
Serial.print("setting color r to ");
Serial.println(redValue);
//If the character rhymes with EE (green)...
} else if (colorCode == 'b' || colorCode == 'c' ||colorCode == 'd' || colorCode == 'e' ||colorCode == 'g' ||colorCode == 'p' || colorCode == 't' ||colorCode == 'v' ||colorCode == 'z') {
greenValue = (greenValue + 10) % 255;
analogWrite(greenPin, greenValue);
Serial.print("setting color g to ");
Serial.println(greenValue);
//If the character doesn't rhyme with EE (blue)...
} else if (colorCode == 'f' || colorCode == 'h' || colorCode == 'i' || colorCode == 'j' || colorCode == 'k' || colorCode == 'l' || colorCode == 'm' || colorCode == 'n' || colorCode == 'o' ||colorCode == 'q' ||colorCode == 'r' ||colorCode == 's' || colorCode == 'u' ||colorCode == 'w' ||colorCode == 'x' || colorCode == 'y') {
blueValue = (blueValue + 10) % 255;
analogWrite(bluePin, blueValue);
Serial.print("setting color b to ");
Serial.println(blueValue);

//To "clear" ...
} else if (colorCode == ' ') {
blueValue = 0;
redValue = 0;
greenValue = 0;
analogWrite(bluePin, blueValue);
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);

Serial.print("CLEAR");
}
//Move on to the next character in the string
//From here, the code continues executing from the "while" line above...
i++;
}
}


//compare two strings to see if they are equal
//compares the first 'numCharacters' characters of string1 and string2 to
//see if they are the same
//
//E.g. stringsEqual("hello","hello",5) => true
// stringsEqual("hello","helaabbnn",3) => true
// stringsEqual("hello","helaa",5) => false
boolean stringsEqual(char *string1, char *string2, int numCharacters) {
if (strncmp(string1, string2, numCharacters) == 0) {
return true;
} else {
return false;
}
}

Items

TUI Lab 2 - Components: TUI Lab 2 Components: Circuit + Diffuser

TUI Lab 2 - Working Device: TUI Lab 2 Circuit, using LED diffuser


Powered by Drupal - Design by Artinet