Lab 2 Diffuser

Submitted by euiyoungkim on Tue, 02/12/2013 - 23:19

Description

I was try to make diffuser to mix RGB colors by covering LED lights with transparent plastic tubes. In order to prevent diffusion of lights at the end of tubes (which means the other sides towards top), I also added wrinkled napkins which makes effects to hold light at the end. Interesting observation is that the light gets clear/darker as it travels through the tubes from its origin.

In second, I added the types of input to change brightness of LEDs from 0-10 as color input is dependent on the number of characters "R/G/B". (e.g. rrrr means 20 out of 255. By typing another iteration of rrr actually increases the brightness from 20 to 35, which means 5 per each character. Once the brightness of color gets 255 by adding the number of character, the color get reset. (also, we could reset the brightness simply by typing r0, b0, or g0)

Components

1. Arduino UNO

2. Breadboard

3. Red/Red/Brown/Gold Resistor x 3

4. Green/Red/Blue LEDs

5. Transparent Tube x 3

6. Plastic Cub

7. Napkin

 

Code

 

/* 
 * Serial RGB LED
 * ---------------
 * Serial commands 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 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 * Modified for i262 HW 2, Feb. 12. 2013
 * Euiyoung Kim
 */
 
 
char serInString[100];  // array that will hold the different bytes of the string. 100=100characters;
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
 
int redValue = 127; // set them all to mid brightness
int greenValue = 127; // set them all to mid brightness
int blueValue = 127; // set them all to mid brightness
 
void setup() {
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin, OUTPUT);
  Serial.begin(9600);
  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') or 'rrrrr' :");  
 
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString);
 
if (serInString[1] >= 48 && serInString[1] <= 57) { //runs this code if second character is an integer character
colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
colorVal = atoi(serInString + 1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
 
if(colorCode == 'r') {
analogWrite(redPin, colorVal);
redValue = colorVal;
}
else if(colorCode == 'g') {
analogWrite(greenPin, colorVal);
greenValue = colorVal;
}
else if(colorCode == 'b'){
analogWrite(bluePin, colorVal);
blueValue = colorVal;
}
for(int i = 0 ; i <= 9; i+=1){
serInString[i] = 0;
}
Serial.print(serInString);
}
 
else if(serInString[0] != 0){ //runs this code if the second character is not an integer character (see above)
int j = 0; //and if the first character is not the null character
 
while (j < 10 & serInString[j] != 0) { //iterates through the string, incrementing LEDs as indicated by the characters found
if(serInString[j] == 'r') {
redValue = (redValue + 5) % 255; //Incrementing the appropriate LED's value
analogWrite(redPin, redValue); 
}
if(serInString[j] == 'g') {
greenValue = (greenValue + 5) % 255;
analogWrite(greenPin, greenValue);
if(serInString[j] == 'b') {
blueValue = (blueValue + 5) % 255;
analogWrite(bluePin, blueValue); 
}
j++;
}
Serial.print("Red is ");
Serial.print(redValue);
Serial.print("; Green is ");
Serial.print(greenValue);
Serial.print("; Blue is ");
Serial.print(blueValue);
Serial.println(".");
 
for(int i = 0 ; i <= 9; i+=1){ // indicates we've used this string and zeroes all characters in the string
serInString[i] = 0;
}
}
 
delay(100); // wait a bit, for serial data
}
 
//read a string from the serial and store it in an array
 
 
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
 

 

1.png
2.png
3.png
0
Your rating: None
Drupal theme by Kiwi Themes.