Description
Make diffuser:
Made a diffuser out of a yellow post-it note and a plastic grocery bag. While experimenting, discovered that a Macbook pro LCD screen with brightness turned all the way down would act as a diffuser if LEDs were placed directly behind white apple logo on back of screen.
Modify Code:
Modified provided arduino code so that it would increase LED brightness 10% for each LED specifier (r, g, or b) entered via serial string. Also added optional codes to turn off all LEDs (x) and to set all LEDs to 100% brightness (o)
Components
- 1ea Red LED, Green LED, Blue LED
- 3ea 220 Ohm Resistors
- 1ea Diffuser made from Post-it and plastic grocery bag
- 1ea Alternate diffuser: Macbook Pro LCD screen (not pictured)
Arduino Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode>*", where "colorCode" is
* one of "r","g","b", "x", or "o".
* 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%
*
* "x" turns all LEDs off
* "o" turns all LEDs to 100% brightness
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Adapted 5 September 2007x
* copylefter 2007 Ryan Aipperspach <ryanaip@alumni.rice.edu>
*
* Further edited by Matt Chew Spence 9-11-07
*
*/
//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;
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 = 127;
int greenValue = 127;
int blueValue = 127;
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 command (e.g. 'r43 or rrrrrrrrbbbb') :");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString, 100);
//Uncomment the following line to read commands of the form 'rrrb'
processRepeatKeyCommands(serInString, 100);
//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';
}
}
//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 + 25) % 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 + 25) % 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 + 25) % 255;
analogWrite(bluePin, blueValue);
Serial.print("setting color b to ");
Serial.println(blueValue);
//If the character is x (all off)...
} else if (colorCode == 'x') {
redValue = 0;
greenValue = 0;
blueValue = 0;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("turning all LEDs off");
//If the character is o (all on)...
} else if (colorCode == 'o') {
redValue = 255;
greenValue = 255;
blueValue = 255;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("turning all LEDs to full brightness");
}
//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;
}
}
Images
TUI Lab 2 pic 1
TUI Lab 2 pic 2
Comments
GSI Comments
Looks good. I like the top of the diffuser -- it looks kind of like a glowing cloud. I think that the edge between the post-it note and the cotton is a little sharp, though. Can you think of any ways to make a glowing piece of cotton that looks like it's suspended by itself?
The observation about the MacBook screen is really cool, too... If you have time to post a picture of it, I think it would be very cool to see.