Description:
In this lab exercise, I played with PWM in order to create an RGB color diffusor. In order to spread out the sharp LED lights more and to smoothen them, I used cotton and Fell paper (tracing paper) over it. In order to increase the brightness of lights in my diffusor, the command to be entered is colorCode '+/-'. For example, r+ would increase the brightness of the red LEd by 50%. Similary, g- would decrease the brightness of the green LED by 50%.
Components:
Arduino Board
3 resistors (red,red, brown, gold)
connecting wires
3 LEDS (RGB)
Cotton pad
Fell Paper
Sticks for support
Code:
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is <colorVal> + or <colorVal> -
* For example, r+ would increase the brightness of the red LED by 50% and b- would decrease the blue LED brightness by 50%
* Created 24 September 2013
*/
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 colorVal;
int bluePin = 9; // Red LED, connected to digital pin 9
int redPin = 10; // Green LED, connected to digital pin 10
int greenPin = 11; // Blue LED, connected to digital pin 11
int redOrigin = 10;
int blueOrigin = 10;
int greenOrigin = 10;
char brightness;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redOrigin); // set them all to mid brightness
analogWrite(greenPin, greenOrigin); // set them all to mid brightness
analogWrite(bluePin, blueOrigin); // set them all to mid brightness
Serial.println("Enter the color and the command to control brightness. Your options are: r. Red b. Blue g. Green + Brighter by 50% - Dimmer by 50%. For example, r+ will make red brighter by 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 == 'b' || colorCode == 'g' ) {
brightness = char(serInString[1]);
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r'){
if(brightness=='+'){
if(redOrigin>255){
Serial.println();
Serial.print("Red maximum brightness reached");
return;
}
Serial.print("Increasing red brightness by 50%");
Serial.println();
Serial.print(redOrigin);
redOrigin=redOrigin*1.5;
Serial.print(redOrigin);
}else if(brightness=='-'){
if(redOrigin<10){
Serial.println();
Serial.print("Red minimum brightness reached");
return;
}
Serial.print("Decreasing red brightness by 50%");
Serial.println();
Serial.print(redOrigin);
redOrigin=redOrigin*0.5;
Serial.print(redOrigin);
}
analogWrite(redPin, redOrigin);
}
else if(colorCode == 'g'){
if(brightness=='+'){
if(greenOrigin>255){
Serial.println();
Serial.print("Green maximum brightness reached");
return;
}
Serial.print("Increasing green brightness by 50%");
Serial.println();
Serial.print(greenOrigin);
greenOrigin=greenOrigin*1.5;
Serial.print(greenOrigin);
}else if(brightness=='-'){
if(greenOrigin<10){
Serial.println();
Serial.print("Green minimum brightness reached");
return;
}
Serial.print("Decreasing green brightness by 50%");
Serial.println();
Serial.print(greenOrigin);
greenOrigin=greenOrigin*0.5;
Serial.print(greenOrigin);
}
analogWrite(greenPin, greenOrigin);
}
else if(colorCode == 'b'){
if(brightness=='+'){
if(blueOrigin>255){
Serial.println();
Serial.print("Blue maximum brightness reached");
return;
}
Serial.print("Increasing blue brightness by 50%");
Serial.println();
Serial.print(blueOrigin);
blueOrigin=blueOrigin*1.5;
Serial.print(blueOrigin);
}else if(brightness=='-'){
if(blueOrigin<10){
Serial.println();
Serial.print("Blue minimum brightness reached");
return;
}
Serial.print("Decreasing blue brightness by 50%");
Serial.println();
Serial.print(blueOrigin);
blueOrigin=(blueOrigin*0.5);
Serial.print(blueOrigin);
}
analogWrite(bluePin, blueOrigin);
}
}
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++;
}
}
- Login to post comments