Kelly Park - HW2

Submitted by kmpark on Sun, 02/10/2013 - 04:17

 

Description

This is where you will put a description of your assignment. Write a couple paragraphs describing your approach. What does it do? Why did you choose this approach over others? What problems did you run into? 

I build 3 LED diffuser with my Arduino board. Using PWM, I could control the brightness of each of the LEDs, and also the serial I/O made (almost) real-time color control. I basically followed the steps that Prof. Ryokai suggested. I put three LED together on the breadboard. I tried to be careful about the polarity of the LED and the wirings on Arduino board for PWM capability. Then, I made them blink in certain time frame with my code from the first assignment. To mix the light colors, I chose cotton and folded tracing paper. I initially tried with the paper only, but the LEDs were too bright to be perceived as a mixed color. I also had fun with a different diffuser found at home. My son's favorite toy cup made a great diffuser with a cotton ball. Afterward, I started working with serial I/O with changing brightness. Arduino code was little different from the only language that I know, which is python, but I could figure it out easily with help of Nahman. The serial input made the LED brightness 10% brighter. I added three other keys, T, H, N, which are right next to R, G, B to decrease brightness. 

 

Components Used

1- Arduino Uno

3- LEDs in Red, Green, Blue

3- Resistor

1- Breadboard

Code

 

/* 
 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs
 *
 * Command 
 * E.g. "r"   increase the red LED color value 10%.  
 *      "t" decrease the red LED color value 10%
*
 * adapted 10 Feb 2013
 * KellyP
 
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;
char colorCode2;
 
 
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
 
 
float colorValR = 0;
float colorValG = 0;
float colorValB = 0;
int colorMax = 255;
 
void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin,   colorValR);   // set them all to mid brightness
  analogWrite(greenPin, colorValG);   // set them all to mid brightness
  analogWrite(bluePin,  colorValB);   // set them all to mid brightness
  Serial.println("enter color command (e.g. 'r' to increase 10%) :");  
}
 
void loop () {
  // clear the string
  memset(serInString, 0, 100);
  //read the serial port and create a string out of what you read
  readSerialString(serInString);
    
  colorCode = serInString[0];
  if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
    
    Serial.print("Increasing color ");
    Serial.print(colorCode);
    Serial.print(" to ");
 
    if(colorCode == 'r'){ 
      if (colorValR<255){
        colorValR = colorValR+(colorMax*0.1);
      }
     
      Serial.print(colorValR);
      analogWrite(redPin, colorValR);
    }
    else if(colorCode == 'g'){
      if (colorValG<255){
        colorValG = colorValG+(colorMax*0.1);
      }
      Serial.print(colorValG);
      analogWrite(greenPin, colorValG);
    }
    else if(colorCode == 'b'){
      if (colorValB<255){
        colorValB = colorValB+(colorMax*0.1);
      }
      Serial.print(colorValB);
      analogWrite(bluePin, colorValB);
    }
    Serial.println();
    serInString[0] = 0;                   // indicates we've used this string
 
  }
  else if( colorCode == 't' || colorCode == 'h' || colorCode == 'n' ) {
    if( colorCode == 't'){
     colorCode2 = 'r';
    } 
    else if ( colorCode == 'h'){
     colorCode2 = 'g';
    }
   else if ( colorCode == 'n' ){
     colorCode2 = 'b';
    }
    Serial.print("Decreasing color ");
    Serial.print(colorCode2);
    Serial.print(" to ");
 
    if(colorCode == 't'){ 
      if (colorValR>0){
        colorValR = colorValR-(colorMax*0.1);
      }
 
      Serial.print(colorValR);
      analogWrite(redPin, colorValR);
    }
    else if(colorCode == 'h'){
      if (colorValG>0){
        colorValG = colorValG-(colorMax*0.1);
      }
      Serial.print(colorValG);
      analogWrite(greenPin, colorValG);
    }
    else if(colorCode == 'n'){
      if (colorValB>0){
        colorValB = colorValB-(colorMax*0.1);
      }
      Serial.print(colorValB);
      analogWrite(bluePin, colorValB);
    }
    Serial.println();
    serInString[0] = 0;                   // indicates we've used this string
 
  }
 
  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++;
  }
}
 

Images

Attached below :)

 

photo 4.JPG
photo 3.JPG
photo 1.JPG
photo 2.JPG
0
Your rating: None
Drupal theme by Kiwi Themes.