User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Colors and Lights. Digital Input with Diffuser

Submitted by ngandomi on Sat, 09/13/2008 - 23:02

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

Description:

This project uses the Arduino board to input color names from the user and display them using three LEDs.

Components:

Laptop running Arduino software

White tissue paper

3 220 Ohm resistors

3 LEDs (red, green, blue)

ground wire, 3 yellow wires

Arduino board

breadboard

USB connecting cable

Note:

User is prompted to enter a color (e.g. red, blue, green, etc...) and hit enter. Users can enter red, blue, yellow, orange, green, pink, purple and black (turns off LEDs). If users enter "flow", the program cycles through the different colors 10 times.

Picture:

Blue: Arduino board

Code:

 

/* 
 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs 
 *
 * Command structure is "<colorCode><colorVal>", where "colorCode" is
 * one of "r","g",or "b" and "colorVal" is a number 0 to 255.
 * E.g. "r0"   turns the red LED off.  
 *      "g127" turns the green LED to half brightness
 *      "b64"  turns the blue LED to 1/4 brightness
 *
 * Alternate command structure is "<colorCode>*", where "colorCode" is
 * one of "r","g", or "b".
 * E.g. "r"    increases the red LED brightness by 10
 *      "rrr"  increases the red LED brightness by 30
 *      "ggb"  increases the green LED brightness by 20 and the blue by 10
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 *
 * Adapted 5 September 2007
 * copylefter 2007 Ryan Aipperspach <ryanaip@alumni.rice.edu>
 *
 */
 
//include support for manipulating strings.
//for a useful string comparison function, see the bottom of this file... stringsEqual()
//#include <stdio.h>
#include <string.h>

char serInString[100];  // array that will hold the different bytes of the string. 100=100characters;
                        // -> you must state how long the array will be else it won't work properly
char colorCode;
int colorVal;
char color[3];

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

int redValue = 255;
int greenValue = 70;
int blueValue = 70;

void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin,   redValue);   // set them all to mid brightness
  analogWrite(greenPin, greenValue);   // set them all to mid brightness
  analogWrite(bluePin,  blueValue);   // set them all to mid brightness
  Serial.println("enter color (e.g. red, blue or green)");  
}

void loop () {
  //read the serial port and create a string out of what you read
  readSerialString(serInString, 100);

  processStringCommands(serInString);
    
  
    
  //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...
  //YOUR_FUNCTION_HERE(serInString);



  //Erase anything left in the serial string, preparing it for the 
  //next loop
  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';
    color[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++;
  }
  
}
//input types in the color they want, we display it for them. 
//"red" would output a red color, "blue" would output a blue, and so on.

void processStringCommands(char *strArray){
//read in the first three characters in the string

  for(int i = 0; i <3; i++){
  color[i] += serInString[i];
    }

if(color[0] == 'r' && color[1] == 'e' && color[2] == 'd'){
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
Serial.println("Setting to red...");
}
else if (color[0] == 'b' && color[1] == 'l' && color[2] == 'u'){
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
Serial.println("Setting to blue...");
} 
else if (color[0] == 'g' && color[1] == 'r' && color[2] == 'e'){
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
Serial.println("Setting to green...");
}
else if (color[0] == 'o' && color[1] == 'r' && color[2] == 'a'){
analogWrite(redPin, 255);
analogWrite(greenPin, 55);
analogWrite(bluePin, 0);
Serial.println("Setting to orange...");
}
else if (color[0] == 'p' && color[1] == 'i' && color[2] == 'n'){
analogWrite(redPin, 255);
analogWrite(greenPin, 60);
analogWrite(bluePin, 60);
Serial.println("Setting to pink...");
}
else if (color[0] == 'b' && color[1] == 'l' && color[2] == 'a'){
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
Serial.println("Setting to black...");
}
else if (color[0] == 'y' && color[1] == 'e' && color[2] == 'l'){
analogWrite(redPin, 200);
analogWrite(greenPin, 70);
analogWrite(bluePin, 0);
Serial.println("Setting to yellow...");
}
else if (color[0] == 'p' && color[1] == 'u' && color[2] == 'r'){
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 200);
Serial.println("Setting to purple..."); 
}
else if (color[0] == 'w' && color[1] == 'h' && color[2] == 'i'){
analogWrite(redPin, 255);
analogWrite(greenPin, 70);
analogWrite(bluePin, 70);
Serial.println("Setting to white..."); }

else if (color[0] == 'f' && color[1] == 'l' && color[2] == 'o'){

Serial.println("Flowing colors..."); 

for (int i=0; i<10; i++){// run over and over again 
analogWrite(redPin, 255);
analogWrite(greenPin, 70);
analogWrite(bluePin, 70); // sets the LED on 
delay(400); // waits for a second
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 200); // sets the LED on
delay(400); 
analogWrite(redPin, 200);
analogWrite(greenPin, 70);
analogWrite(bluePin, 0); // sets the LED on
delay(400); 
analogWrite(redPin, 255);
analogWrite(greenPin, 60);
analogWrite(bluePin, 60); // sets the LED off 
delay(400); // waits for a second 
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0); // sets the LED off 
delay(400); // waits for a second 
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0); // sets the LED off 
delay(400); // waits for a second 
}


}

}




//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);
  }
}


//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;
  }
}