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!

Diffuser and controlling LEDs with keyboard input

Project Members: 
Katherine Ahern


I made the diffuser from the top of a hat, and propped it up with some square styrofoam from an electronics box, which I punched the top out of. 

I also modified the example program so the user can input 't' for teal, which sets the green and blue LEDs and turns off the red, or 'y' for yellow, which turns off the blue and lights the green and red in proportion to make yellow. I was unsuccessful doing the original assignment with multiple key presses, and even this modified code has some known bugs!

Components used:

hat top and styrofoam for the diffuser
arduino board, breadboard, and usb cable
3 LEDs - red, blue, and green
3 resistors
4 wires

Arduino code:

* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
* Command structure is "", 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
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt 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

void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);  
  pinMode(bluePin,  OUTPUT);
  analogWrite(redPin,   127);   // set them all to mid brightness
  analogWrite(greenPin, 127);   // set them all to mid brightness
  analogWrite(bluePin,  127);   // set them all to mid brightness
  Serial.println("enter color command (e.g. 'r43')"); 
  Serial.println("or enter 'y' for yellow or 't' for teal"); 

void loop () {
  //read the serial port and create a string out of what you read
  colorCode = serInString[0];
  if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' )
    colorVal = atoi(serInString+1);{    //I had some trouble modifying/understanding this line (kra)
    if(colorVal != 0) {
      Serial.print("setting color ");
      Serial.print(" to ");
      serInString[0] = 0;      // indicates we've used this string
      //colorCode = 'z';        //try to prevent the crazy loop I'm getting
    if(colorCode == 'r')
      analogWrite(redPin, colorVal);
    else if(colorCode == 'g')
      analogWrite(greenPin, colorVal);
    else if(colorCode == 'b')
      analogWrite(bluePin, colorVal);
    else if(colorCode == 'y') {         //this was as close to yellow as I found,
      analogWrite(greenPin, 50);        //with the discrepancy in how bright the LEDs are. (kra)
      analogWrite(redPin, 255);
      analogWrite(bluePin, 0);
    else if(colorCode == 't') {          //t is for teal, or blue-green
      analogWrite(greenPin, 100);        //again, I just decided on values based on experimentation (kra)
      analogWrite(redPin, 0);
      analogWrite(bluePin, 100);
  delay(100);  // wait a bit, for serial data

//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
  int i = 0;
  if(!Serial.available()) {
  while (Serial.available()) {
    strArray[i] =;



GSI Comments

Cool idea for the diffuser. I'm curious to see how it looks from other angles -- does the side of the hat light up, etc? The mesh pattern of the hat is really nice. It would also be interesting to combine your diffuser with some of the other student's work that had a really smooth color distribution. (E.g. spread out the color, and use the hat to add a pattern to it.) Then, I think, the mesh would really add a nice texture.

Thanks for working to modify the original code. I know its hard to learn how everything works and to debug problems -- having the 't' and 'y' modify different colors is a good start.

Powered by Drupal - Design by Artinet