[Description]
- Finding a good diffuser: Film case & Parchment Paper
- Controll colors of LEDs by
a) multiple key presses
b) color names (yellow, orange and purple)
[Components]
- LED X 3 (R, G, B)
- Resistor X 3 (stripes: red-red-brown-gold)
- White film case
- parchment paper
[Adurino code]
/* Enhanced RGB LEDs */
char inputString[100];
char c;
char color;
int value[3] = {0, 0, 0};
int pin[3] = {9, 10, 11};
void setup() {
pinMode(pin[0], OUTPUT); // Red
pinMode(pin[1], OUTPUT); // Green
pinMode(pin[2], OUTPUT); // Blue
Serial.begin(9600);
analogWrite(pin[0], 250);
analogWrite(pin[1], 20);
analogWrite(pin[2], 50);
Serial.println("enter color info: r, g, b, yellow, orange or purple");
}
void loop () {
readInputString(inputString);
c = inputString[0];
int i=0, j=0;
if ( c == 'r' || c == 'g' || c == 'b' ) { // *** [homework No. 2] ***
while (i < 100 && inputString[i] != '\0') {
color = inputString[i];
switch (color) {
case 'r': j=0; break;
case 'g': j=1; break;
case 'b': j=2; break;
}
value[j] = (value[j] + 10) % 255;
analogWrite(pin[j], value[j]);
Serial.print("setting color ");
Serial.print(color);
Serial.print(" to ");
Serial.println(value[j]);
i++;
}
}
else if (c>='a' && c<='z') { // *** [homework No. 3] ***
if (!strcmp(inputString, "yellow")) {
analogWrite(pin[0], 255);
analogWrite(pin[1], 255);
analogWrite(pin[2], 0);
}
else if(!strcmp(inputString, "orange")) {
analogWrite(pin[0], 180);
analogWrite(pin[1], 127);
analogWrite(pin[2], 0);
}
else if(!strcmp(inputString, "purple")) {
analogWrite(pin[0], 255);
analogWrite(pin[1], 0);
analogWrite(pin[2], 255);
}
Serial.print("Setting color to ");
Serial.print(inputString);
Serial.println();
}
int k;
for (k=0;k<10;k++) {
inputString[k] = 0; // initialize all strings
}
delay(100);
}
void readInputString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
[Images]
- Currently being uploaded...