Description
For the first part of this lab, a light diffuser was constructed using waxpaper and folded into an origami ball. The origami ball had a hole at the bottom, and cotton was inserted into the ball in order to enhance the diffusion. The origami ball was placed over the red, green, and blue LEDs, creating a diffuser for the lights. The waxpaper origami ball worked fairly well as a diffuser, though in future work, a thicker paper could be used to make it even better.
In the second part of the lab, the code was changed to control the intensities of the red, green, and blue LEDs using key presses. If a lowercase "r" was entered, the red LED decreased in brightness by 20%. This could be performed multiple times until the LED brightness was 0%. If an uppercase "R" was entered, the red LED increased in brightness by 20%. This could be performed multiple times until the LED brightness was 100%. This functionality works for the red, green, and blue LEDS, and it is very intuitive to step up or step down the brightness using keystroke commands. Additionally, the original functionality of setting a specific LED's brightness to a numerical point between 0 and 255 was preserved.
In the optional part of the lab, the user could type in a keystroke that would create a specific color other than red, green or blue. For example, if the user types in "o," the LEDs will adjust their brightness such that the combined effect is an orange glow. This also worked when the user types in "p" to create a purple color. This added functionality is just the beginning of creating an array of colors that can be created.
Components Used
1 - Arduino Uno
3 - 220 ohm resistors
1 - Red LED
1 - Blue LED
1 - Green LED
1 - Breadboard
1 - Sheet of waxpaper to create an origami ball
1 - Piece of Cotton
Code
/*
* Digital I/O with Arduino Boards
*
* 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 10 February 2013
* Brian McRae
* i262 course
*
*/
#include <string.h>
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;
char colorName[100]; // Create a colorName variable that takes full color names (ie. red)
// Initialize red, blue, and green LED intensity values to 127
int redVal = 127;
int blueVal = 127;
int greenVal = 127;
int redPin = 10; // Red LED, connected to digital pin 9
int greenPin = 9; // 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, 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. 'r43'): ");
}
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 == 'g' || colorCode == 'b' ) {
if (serInString[1] != '\0') {
colorVal = atoi(serInString+1);
// Change a specific LED intensity value depending on if the red, blue, or green LED is specified.
if (colorCode == 'r') {
redVal = colorVal;
}
if (colorCode == 'g') {
greenVal = colorVal;
}
if (colorCode == 'b') {
blueVal = colorVal;
}
Serial.print("Setting color ");
// Add in the full color name to the print
if (colorCode == 'r') {
strcpy(colorName, "red");
}
if (colorCode == 'g') {
strcpy(colorName, "green");
}
if (colorCode == 'b') {
strcpy(colorName, "blue");
}
Serial.print(colorName);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r')
analogWrite(redPin, redVal);
else if(colorCode == 'g')
analogWrite(greenPin, greenVal);
else if(colorCode == 'b')
analogWrite(bluePin, blueVal);
}
}
// Functionality to increment/decrement the LED intensity by 20% by pressing a single key.
// Lowercase letter will decrement LED intensity by 20%.
// Uppercase letter will increment LED intensity by 20%.
// Minimum LED intensity value = 0
// Maximum LED intensity value = 255
if(colorCode == 'r') {
if (serInString[1] =='\0') {
redVal = redVal - 50;
if (redVal < 0) {
redVal = 0;
}
Serial.print("Red LED set to ");
Serial.print(redVal);
Serial.println();
analogWrite(redPin, redVal);
}
}
if(colorCode == 'R') {
if (serInString[1] =='\0') {
redVal = redVal + 50;
if (redVal > 255) {
redVal = 255;
}
Serial.print("Red LED set to ");
Serial.print(redVal);
Serial.println();
analogWrite(redPin, redVal);
}
}
if(colorCode == 'b') {
if (serInString[1] =='\0') {
blueVal = blueVal - 50;
if (blueVal < 0) {
blueVal = 0;
}
Serial.print("Blue LED set to ");
Serial.print(blueVal);
Serial.println();
analogWrite(bluePin, blueVal);
}
}
if(colorCode == 'B') {
if (serInString[1] =='\0') {
blueVal = blueVal + 50;
if (blueVal > 255) {
blueVal = 255;
}
Serial.print("Blue LED set to ");
Serial.print(blueVal);
Serial.println();
analogWrite(bluePin, blueVal);
}
}
if(colorCode == 'g') {
if (serInString[1] =='\0') {
greenVal = greenVal - 50;
if (greenVal < 0) {
greenVal = 0;
}
Serial.print("Green LED set to ");
Serial.print(greenVal);
Serial.println();
analogWrite(greenPin, greenVal);
}
}
if(colorCode == 'G') {
if (serInString[1] =='\0') {
greenVal = greenVal + 50;
if (greenVal > 255) {
greenVal = 255;
}
Serial.print("Green LED set to ");
Serial.print(greenVal);
Serial.println();
analogWrite(greenPin, greenVal);
}
}
// Adding in different color combinations.
// Create an orange color.
// LED intensity values were chosen to best produce an orange color with the chosen diffuser.
if(colorCode == 'o') {
if (serInString[1] =='\0') {
Serial.print("Orange!");
Serial.println();
analogWrite(redPin, 255);
analogWrite(bluePin, 0);
analogWrite(greenPin, 40);
}
}
// Create an purple color.
// LED intensity values were chosen to best produce an orange color with the chosen diffuser.
if(colorCode == 'p') {
if (serInString[1] =='\0') {
Serial.print("Purple!");
Serial.println();
analogWrite(redPin, 255);
analogWrite(bluePin, 160);
analogWrite(greenPin, 72);
}
}
// Check to make sure keyboard input is valid. If keyboard input is not valid, create an error message that tells the user which keyboard inputs are valid.
if (serInString[0] !='\0' ) {
if (serInString[0] != 'r' && serInString[0] != 'g' && serInString[0] != 'b' && serInString[0] != 'R' && serInString[0] != 'G' && serInString[0] != 'B' && serInString[0] != 'o' && serInString[0] != 'p') {
Serial.print("Command not recognized.");
Serial.println();
Serial.print("Please try r, g, or b, followed by a number between 0-255, ie. r155.");
Serial.println();
Serial.print("Or please try r, g, or b to decrement LED intensity by 20%.");
Serial.println();
Serial.print("Or please try R, G, or B to increment LED intensity by 20%.");
Serial.println();
Serial.print("Or please try o to create an orange color, or p to create a purple color.");
Serial.println();
}
}
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++;
}
}