Description:
For the second lab assignment, I set up a circuit to individually control three LEDs - Red, Green and Blue - and to mix their light to create all colors but black. In order to improve the light mixing, a custom skull-shaped diffusor was made.
The colors of the resulting skull light can be freely controlled by pressing the keys r, g and b to increase the power of the respective LED (red, green, blue) in 10% increments.
Components used:
Electronics:
- (1) Arduino Uno
- (3) 220 ohm resistors
- (1) LED (red)
- (1) LED (green)
- (1) LED (blue)
- (1) Breadboard
- (several) cables
Diffusor:
- (1) Halloween plastic skull
- (1) Artificial spider web
Building of diffusor
The main component is a skull made of translucent plastic that is commonly used on halloween. In order to improve the mixing of the three LED light sources, a piece of artificial spider web was inserted into the plastic skull. This leads to a more even distribution of light from all LEDs, and thus a better mix of colors.
Code used:
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Enter the letters "r", "b" or "g" in any order to increase the brightness of the Red, Blue or Green LED
* by 10%, respectively. When 100% brightness are exceeded, the brightness is set to 0% again.
* Example: bbbrrg will increase the brightness of Red by 20%, Blue by 20% and Green by 10%
*
* Created 24 September 2013
* by Clemens Meyer <clemens.meyer@ischool.berkeley.edu>
*
* Based on: Serial RGB LED
* 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 colorAdd;
bool colorChanged = false; // variable to see if color values have changed (and if new values should be printed).
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 redVal = 0; // Initial value of Red LED
int greenVal = 0; // Initial value of Green LED
int blueVal = 0; // Initial value of Blue LED
int redPerc = 0; // Initial percentage value of RED LED
int greenPerc = 0; // Initial percentage value of RED LED
int bluePerc = 0; // Initial percentage value of RED LED
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. 'rrrr' to increase Red LED brightness by 40%) :");
printColors(redPerc, greenPerc, bluePerc);
}
void loop () {
// set all the values in the array to zero
memset(serInString, 0, 100);
readSerialString(serInString);
// go through whole array and look for color values)
for (int i = 0; i < 100; i++) {
colorAdd = serInString[i];
// if character on current position is an 'r', increase red value by 10%
if(colorAdd == 'r') {
redPerc += 10;
// if red percentage value is over 100%, reset to 0%
if(redPerc > 100) {
redPerc = 0;
}
redVal = redPerc * 255/100;
analogWrite(redPin, redVal);
colorChanged = true; // signal that colors have changed and new colors should be printed
}
if(colorAdd == 'g') {
greenPerc += 10;
if(greenPerc > 100) {
greenPerc = 0;
}
greenVal = greenPerc * 255/100;
analogWrite(greenPin, greenVal);
colorChanged = true;
}
if(colorAdd == 'b') {
bluePerc += 10;
if(bluePerc > 100) {
bluePerc = 0;
}
blueVal = bluePerc * 255/100;
analogWrite(bluePin, blueVal);
colorChanged = true;
}
}
//If color values were changed, output new color values
if(colorChanged == true) {
printColors(redPerc, greenPerc, bluePerc);
colorChanged = false;
}
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()) {
//..read each character into the array, one at a time
strArray[i] = Serial.read();
i++;
}
}
//Print the current values of all three LEDs as percentage of their maximum brightness
void printColors (int redPerc, int greenPerc, int bluePerc) {
Serial.print("Red: ");
Serial.print(redPerc);
Serial.print("%, Green: ");
Serial.print(greenPerc);
Serial.print("%, Blue: ");
Serial.print(bluePerc);
Serial.println("%");
}
- Login to post comments