Description
I created a diffuser out of a cotton pad, plastic bag, and rubber band. I initially started with only the plastic bag, but found that the diffusion wasn't very smooth. The cotton pad seemed to help a lot with it.
Following the in-class lab, I changed the code around so it listened to the user's input and add 10% brightness everytime s/he presses "r", "g", or "b". Additionally, if the user presses "y", "c", or "m", the LEDs will adjust to yellow, cyan, and magenta respectively. Finally, if the user presses "n", all the LEDs are reset to 0 brightness. The initial user instruction has also been updated to reflect these possible inputs.
Components
- 3x 220 Ohm Resistor
- 1x Red LED
- 1x Green LED
- 1x Blue LED
- 7x Jumpers
- 1x Arduino Uno
- 1x Breadboard
- 1x Macbook Pro
- 1x Plastic bag
- 1x Cotton pad
- 1x Rubber band
Code
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 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 redValue = 0;
int greenValue = 0;
int blueValue = 0;
int redStep = 0;
int greenStep = 0;
int blueStep = 0;
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 mid brightness
analogWrite(greenPin, 0); // set them all to mid brightness
analogWrite(bluePin, 0); // set them all to mid brightness
Serial.println("Press 'r', 'g', or 'b' to adjust the red, green, or blue LED individually.");
Serial.println("Press 'y', 'c', or 'm' to trigger yellow, cyan, or magenta.");
Serial.println("Press 'n' to reset.");
}
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];
// user input to change the LED brightness
if (colorCode == 'r' || colorCode == 'g' || colorCode == 'b' || colorCode == 'y' || colorCode == 'c' || colorCode == 'm' || colorCode == 'n') {
// if red
if (colorCode == 'r') {
if(redValue > 225) {
redValue = 0;
redStep = 0;
} else {
redValue += 25.5;
redStep += 10;
};
analogWrite(redPin, redValue);
char redMsg[100];
Serial.print(colorCode);
Serial.print(" = ");
Serial.print(itoa(redStep,redMsg,10));
Serial.print("%");
// if green
} else if(colorCode == 'g') {
if (greenValue > 225) {
greenValue = 0;
greenStep = 0;
} else {
greenValue += 25.5;
greenStep += 10;
};
analogWrite(greenPin, greenValue);
char greenMsg[100];
Serial.print(colorCode);
Serial.print(" = ");
Serial.print(itoa(greenStep,greenMsg,10));
Serial.print("%");
// if blue
} else if(colorCode == 'b') {
if (blueValue > 225) {
blueValue = 0;
blueStep = 0;
} else {
blueValue += 25.5;
blueStep += 10;
};
analogWrite(bluePin, blueValue);
char blueMsg[100];
Serial.print(colorCode);
Serial.print(" = ");
Serial.print(itoa(blueStep,blueMsg,10));
Serial.print("%");
// if yellow
} else if(colorCode == 'y') {
redValue = 127;
redStep = 50;
greenValue = 127;
greenStep = 50;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, 0);
char yellowMsg[100];
Serial.print("Yellow");
Serial.print("; r = ");
Serial.print(itoa(redStep,yellowMsg,10));
Serial.print("%");
Serial.print("; g = ");
Serial.print(itoa(greenStep,yellowMsg,10));
Serial.print("%");
// if cyan
} else if(colorCode == 'c') {
greenValue = 127;
greenStep = 50;
blueValue = 127;
blueStep = 50;
analogWrite(redPin, 0);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
char cyanMsg[100];
Serial.print("Cyan");
Serial.print("; g = ");
Serial.print(itoa(greenStep,cyanMsg,10));
Serial.print("%");
Serial.print("; b = ");
Serial.print(itoa(blueStep,cyanMsg,10));
Serial.print("%");
// if magenta
} else if(colorCode == 'm') {
redValue = 127;
redStep = 50;
blueValue = 127;
blueStep = 50;
analogWrite(redPin, redValue);
analogWrite(greenPin, 0);
analogWrite(bluePin, blueValue);
char magentaMsg[100];
Serial.print("Magenta");
Serial.print("; r = ");
Serial.print(itoa(greenStep,magentaMsg,10));
Serial.print("%");
Serial.print("; b = ");
Serial.print(itoa(blueStep,magentaMsg,10));
Serial.print("%");
// if reset
} else if(colorCode == 'n') {
Serial.print("Resetting all LEDs...");
redValue = 0;
greenValue = 0;
blueValue = 0;
redStep = 0;
greenStep = 0;
blueStep = 0;
analogWrite(redPin, 0);
analogWrite(bluePin, 0);
analogWrite(greenPin, 0);
}
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++;
}
}
- Login to post comments