Description:
Pyramid color is controlled by entering a number from 1 to 7 on the keyboard. The colors were chosen based on the RBG (CMY) color model.
Diffuser Materials:
Styrofoam block
Vellum lining
Frosted plastic
Arduino Code:
*/
* Serial RGB/CMYK LED Color Mixer
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* User enters a number to see one of seven colors, based on the primary colors of light.
* E.g. '1' sets the LEDs to red (255,0,0).
* '2' sets the LEDs to yellow (255,255,0).
* '3' sets the LEDs to green (0,255,0).
* '4' sets the LEDs to cyan (0,255,255).
* '5' sets the LEDs to blue (0,0,255).
* '6' sets the LEDS to magenta (255,0,255)
* '7' sets all LEDS on (255,255,255) .
*
* Created on 13 September 2011 by Andrea Hsieh
*
*/
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
int keynumber; // define number
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, 0); // set them all to off
analogWrite(greenPin, 0); // set them all to off
analogWrite(bluePin, 0); // set them all to off
Serial.println(" Enter a number from 1-7: ");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
keynumber = serInString[0];
if( keynumber == '1' || keynumber == '2' || keynumber == '3' || keynumber == '4' || keynumber == '5' || keynumber == '6' || keynumber == '7') {
Serial.print("setting color to ");
if(keynumber == '1')
Serial.print("Red");
else if(keynumber == '2')
Serial.print("Yellow");
else if(keynumber == '3')
Serial.print("Green");
else if(keynumber == '4')
Serial.print("Cyan");
else if(keynumber == '5')
Serial.print("Blue");
else if(keynumber == '6')
Serial.print("Magenta");
else if(keynumber == '7')
Serial.print("All");
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(keynumber == '1') {
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
// red color
}
else if(keynumber == '2') {
analogWrite(redPin, 255);
analogWrite(greenPin, 50);
analogWrite(bluePin, 0);
// yellow color (green LED altered to appear "more yellow")
}
else if(keynumber == '3') {
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
// green color
}
else if(keynumber == '4') {
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
// cyan color
}
else if(keynumber == '5') {
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
// blue color
}
else if(keynumber == '6') {
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 50);
// magenta color
}
else if(keynumber == '7') {
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
// all color
}
}
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++;
}
}