Assignment 2 - Diffuser and serial control for 3 LEDs

Chelsey's picture

Description:

The diffuser consists of a ball of plastic wrap wrapped around a clear cylindrical contact lens case stuffed with cotton. The cotton is semi-transparent, so it helps to mix the colors, while the multiple layers of plastic wrap also diffuses light and makes the colors look more uniform.

The program takes input in the form of four different emoticons - :-)  :-(  :-P  >:-/ - and converts them into colors that match the emotion they represent.

Materials for diffuser:

plastic wrap

cylindrical contact lens case

cotton

Code:

/*
 * Serial RGB LED emoticon
 * ---------------
 * Serial commands in the form of emoticons control the brightness of R,G,B LEDs
 *
 * User enters one of four emoticons representing different emotions.
 * E.g. ":-)" sets the R, G, and B LEDs to a combined color of purple.  
 *      ":-(" sets the LEDs to blue.
 *      ":-P"  sets the LEDs to a multi-hued color.
 *      ">:-/" sets the LEDs to red.
 *
 * Created 13 September 2011
 * by Chelsey Tanaka
 */

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 emoticon;      // define variable emoticon

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 off
  analogWrite(greenPin, 0);   // set them all to off
  analogWrite(bluePin,  0);   // set them all to off
  Serial.println("Enter one of the following emoticons: ");
  Serial.println(":-)  :-(  :-P  >:-/");
}

void loop () {
  // clear the string
  memset(serInString, 0, 100);
  //read the serial port and create a string out of what you read
  readSerialString(serInString);
    
  emoticon = serInString[2]; //variable emoticon set to the 3rd character in the input string
  if( emoticon == ')' || emoticon == '(' || emoticon == 'P' || emoticon == '-' ) {
    Serial.print("Your mood is ");
    if(emoticon == ')')
      Serial.print("happy.");
    else if(emoticon == '(')
      Serial.print("sad.");
    else if(emoticon == 'P')
      Serial.print("playful.");
    else if(emoticon == '-')
      Serial.print("angry.");
    Serial.println();
    serInString[2] = 0;                   // indicates we've used this string
    if(emoticon == ')') {
      analogWrite(redPin, 255);
      analogWrite(greenPin, 12);
      analogWrite(bluePin, 127);
      //purplish color for happy
    }
    else if(emoticon == '(') {
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0);
      analogWrite(bluePin, 255);
      //blue for sad
    }
    else if(emoticon == 'P') {
      analogWrite(redPin, 255);
      analogWrite(greenPin, 127);
      analogWrite(bluePin, 127);
      //multi-hued color for playful
    }
    else if(emoticon == '-') {
      analogWrite(redPin, 255);
      analogWrite(greenPin, 0);
      analogWrite(bluePin, 0);
      //red for angry
    }
  }
 
  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()) {
    return;
  }
  while (Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
}

Chelsey_diffuser
0
Your rating: None