Serial LED's and Diffuser

stephen.backer's picture

Description:

Initially, I tried making diffusers using glass, aluminum foil, toilet paper tubes, etc. 

I've settled instead on using a disposable soup bowl to contain the light, the cut-off bottom of a plastic container, and cotton to diffuse the light within the bowl. 

My program uses a prompt that allows users to type in their favorite type of fruit from a given list.  The LEDs then light up in the color of that fruit. 

Materials for diffuser:

8 oz. soup bowl

Cut up bottom of a trader joe's plastic container

cotton

Code:

 

/*
 * Serial RGB LED
 * ---------------
 * Serial commands, designated by fruits, 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
 *
 * Created 13 September 2011 by Stephen Backer
 */

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 Fruit;      // define variable fruit names
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);
  Serial.begin(9600);
  analogWrite(redPin,   0);   // set them all to mid brightness
  analogWrite(greenPin, 0);   // set them all to mid brightness
  analogWrite(bluePin,  0);   // set them all to mid brightness
    Serial.println("Please choose your favorite fruit from the following options:");
  Serial.println("Grape, Blueberry, Lime, Pineapple, and Sour Cherry.");
 
}

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


  Fruit = serInString[0];
  if( Fruit == 'g' || Fruit == 'b' || Fruit == 'l' || Fruit == 'p' || Fruit == 's') {
    Serial.print("Enjoy that delicious... ");
    Serial.println();
    serInString[0] = 0;                   // indicates we've used this string
    if(Fruit == 'g') {
      analogWrite(redPin, 150);
      analogWrite(greenPin, 0);
      analogWrite(bluePin, 255);
      Serial.print("GRAPE!");
      // purple
    }
    else if(Fruit == 'b'){
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0);
      analogWrite(bluePin, 255);
      Serial.print("BLUEBERRY!");
      //blue
    }
    else if(Fruit == 'l') {
      analogWrite(redPin, 140);
      analogWrite(greenPin, 255);
      analogWrite(bluePin, 0);
      Serial.print("LIME!");
      //lime green
    }
    else if(Fruit == 'p') {
      analogWrite(redPin, 255);
      analogWrite(greenPin, 100);
      analogWrite(bluePin, 0);
      Serial.print("PINEAPPLE!");
      //yellow-ish
  }
  else if(Fruit == 's') {
      analogWrite(redPin, 255);
      analogWrite(greenPin, 2);
      analogWrite(bluePin, 3);
      Serial.print("SOUR CHERRY!");
      //red
   }
      Serial.println();
      Serial.println();
      Serial.println("Please choose your favorite fruit from the following options:");
  Serial.println("Grape, Blueberry, Lime, Pineapple, and Sour Cherry.");
  }

  delay(1000);  // 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()) {
    return;
  }
  while (Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
}
 

LEDs and Diffuser glowing blue after "blueberry" is typed in to Serial Monitor
Breadboard & Arduino wired with 3 LEDs in series
0
Your rating: None