Lab2: plastic cup/wax paper/stuffing diffuser and keystroke LED control

Description

created a diffuser with a plastic cup, some wax paper, and pillow stuffing. Additionally, i modified the serial controlled LED example code to allow for 'r', 'g', or 'b' key strokes to control the brightness of the respective LED. 

Components Used

arduino uno

(3) 220 kohm resistors

1 red, green, blue LED

diffuser: 1 plastic cup, wax paper, stuffing

Arduino Code (modified from serial LED controller example)

 

/* 
 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs according to number of keystrokes of "r", "g", and "b" respectively
 *
 * Command structure is "<colorCode>", where "colorCode" is
 * one of "r","g",or "b". Additionally, commands can be written in series
 * E.g. "rr"   will increase the red LED output by 64 in a range from 0 to 256.  256 being the 100% brightness and 0 being 0% brightness
 *      "rrgg" will increase the red and green LED output by 64
 *      "bg"  increases blue and green LED by 32 in a range from 0 to 256. 256 being the maximum brightness and 0 being the 
 *
 * This code is modified from a 
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 */
 
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 redVal = 0; // red LED brightness value
int greenVal = 0;  // green LED brightness value
int blueVal = 0;  // blue red LED brightness value
 
 
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,   redVal);   // set them all to mid brightness
  analogWrite(greenPin, greenVal);   // set them all to mid brightness
  analogWrite(bluePin,  blueVal);   // set them all to mid brightness
  Serial.println("enter color command (e.g. 'rrr') :");  
}
 
 
//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++;
  }
}
 
void loop () {
  // clear the string
  memset(serInString, 0, 100);
  //read the serial port and create a string out of what you read
  readSerialString(serInString);
 // iterate through the string array and increase the brightness of each LED by 32 when its' respective color code is encountered
  for (int i = 0; i < sizeof(serInString); i++){  
  colorCode = serInString[i];
  if( colorCode == 'r') {
    redVal = (redVal +32)%256; // modulo operator rolls value over to 0 when it reaches 256
    analogWrite(redPin, redVal);
    Serial.print("setting color ");
    Serial.print(colorCode);
    Serial.print(" to ");
    Serial.print(redVal);
    Serial.println();
  }
  else if(colorCode == 'g'){
    greenVal = (greenVal + 32)%256; //  modulo operator rolls value over to 0 when it reaches 256
    analogWrite(greenPin, greenVal);
    Serial.print("setting color ");
    Serial.print(colorCode);
    Serial.print(" to ");
    Serial.print(greenVal);
    Serial.println();
  }
  else if(colorCode == 'b'){
     blueVal = (blueVal + 32)%256; //  modulo operator rolls value over to 0 when it reaches 256
    analogWrite(bluePin, blueVal);
    Serial.print("setting color ");
    Serial.print(colorCode);
    Serial.print(" to ");
    Serial.print(blueVal);
    Serial.println();
  }
  }
  
  delay(100);  // wait a bit, for serial data
}
 
 
 
diffused Red(128/255 brightness) Blue(96/255 brightness)
diffused Red (224/255 brightness)
Diffused Blue (96/255 brightness) Green (96/255 brightness)
0
Your rating: None