Description:
I used an Arduino-Uno as a microprocessor to light three LED lights dependent on input from a keyboard. There are three different modes: ROYGBIV explicit color input, RGB explicit color input, or step-dimness color input. As a diffuser, I used a house that I 3D designed and printed from an Ultimaker 2+ out of PLA and ABS material, with some cotton distributed by the windows. The LEDs are capable of entering the house via the chimney.
Components Used:
- 1 Arduino
- 3 LEDs (red, green, and blue)
- 3 Resistor (220Ω)
- 1 Breadboard
- A keyboard
- A 3D printed house with holes in it.
- Cotton.
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/ */ 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; String colorCodeString; // necessary for first conditional check for ROYGBIV float colorVal; // necessary for step-change conditional int colorValInt; // necessary for clean println statements float colorDuplicateInput; // necessary for computation of LED power struct RGB { //instantiate a datastructure for RGB values char colorName[50]; int r; int g; int b; }; struct RGB variable[9]; // declare a variable array that contains data types of data structure RGB 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() { // ROYGBIV defined strcpy(variable[0].colorName, "white"); variable[0].r = 255; variable[0].g = 255; variable[0].b = 255; strcpy(variable[1].colorName, "red"); variable[1].r = 255; variable[1].g = 0; variable[1].b = 0; strcpy(variable[2].colorName, "orange"); variable[2].r = 255; variable[2].g = 165; variable[2].b = 0; strcpy(variable[3].colorName, "yellow"); variable[3].r = 255; variable[3].g = 255; variable[3].b = 0; strcpy(variable[4].colorName, "green"); variable[4].r = 0; variable[4].g = 255; variable[4].b = 0; strcpy(variable[5].colorName, "blue"); variable[5].r = 0; variable[5].g = 0; variable[5].b = 255; strcpy(variable[6].colorName, "indigo"); variable[6].r = 75; variable[6].g = 0; variable[6].b = 130; strcpy(variable[7].colorName, "violet"); variable[7].r = 238; variable[7].g = 130; variable[7].b = 238; strcpy(variable[8].colorName, "black"); variable[8].r = 0; variable[8].g = 0; variable[8].b = 0; pinMode(redPin, OUTPUT); // sets the pins as output pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600); // instantiate communication between the computer and the microprocessor analogWrite(redPin, variable[0].r); //default is R value for white analogWrite(greenPin, variable[0].g); //default is G value for white analogWrite(bluePin, variable[0].b); //default is B value for white Serial.println("Please enter color command (e.g. r43, red, rrr) :"); } void loop () { // clear the string memset(serInString, 0, 100); //read the serial port and create a string out of what you read readSerialString(serInString); //this is basically catching everything... if (atoi(serInString + 1) > 0 || atoi(serInString + 1) == 0) { colorCodeString = String(serInString); colorCode = serInString[0]; // check if we have the word in our dictionary if( colorCodeString == "red" || colorCodeString == "green" || colorCodeString == "blue" || colorCodeString == "white" || colorCodeString == "black" || colorCodeString == "yellow" || colorCodeString == "orange" || colorCodeString == "violet" || colorCodeString == "indigo") { for (int count = 0; count <= 8; count++) { if(colorCodeString == variable[count].colorName) { analogWrite(redPin, variable[count].r); analogWrite(greenPin, variable[count].g); analogWrite(bluePin, variable[count].b); Serial.print("Setting RGB values for default "); Serial.print(colorCodeString); Serial.print(" hue: "); String colorSentence_R = "R: " + String(variable[count].r); String colorSentence_G = ", G: " + String(variable[count].g); String colorSentence_B = ", B: " + String(variable[count].b); Serial.println(colorSentence_R + colorSentence_G + colorSentence_B); return; } } } // if not a full word in our dictionary... else if (colorCode == 'r' || colorCode == 'g' || colorCode == 'b') { // if not a step conditional but an explicit value if (serInString[0] != serInString[1]) { colorVal = atoi(serInString+1); } // the step conditional else { colorDuplicateInput = strlen(serInString); colorVal = (float)(colorDuplicateInput/10 * 255.0); } colorValInt = (int)colorVal; Serial.print("setting color "); Serial.print(colorCode); Serial.print(" to "); Serial.print(colorValInt); Serial.println(); serInString[0] = 0; // indicates we've used this string if(colorCode == 'r') analogWrite(redPin, colorValInt); else if(colorCode == 'g') analogWrite(greenPin, colorValInt); else if(colorCode == 'b') analogWrite(bluePin, colorValInt); } } 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++; } }