Description
After getting the pulsing to work for 1 LED, I set up the breadboard to run the DimmingLEDs.txt file, and was able to run it successfully. I kept the same configuration, and was able to run the serial_led_rgb.txt code as well.
For Homework, I made a diffuser out of taping together 2 cotton pads, and ran the DimmingLEDs.txt file whilst covering the lights with the pad. The mixture of blue and red can be seen in the attached photo.
For part 2 of the homework, I took the existing serial code, and modified it to fit the spec. I made it so that eveyr time r, g, or b is pressed, the respective LED's color value is incremented by 25. I also added the ability to reset the LEDs back to off, though it required me to change the readSerialString function.
Materials
1 - breadboard
1 - Arduino
7 - wires
3 - LEDs (red, green, and blue)
3 - 220-ohm resistors
2 - cotton pads (for diffuser)
2 - pieces of tape (for diffuser)
Code
Code for part 2 of the HW:
/*
* 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;
char resestLED; // character that triggers a reset of an LED
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 redColorVal = 0; // Red LED color value
int greenColorVal = 0; // Green LED color value
int blueColorVal = 0; // Blue LED color value
int colorVal = 25; //Rounded down value to change colors by.
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 0 brightness
analogWrite(greenPin, 0); // set them all to 0 brightness
analogWrite(bluePin, 0); // set them all to 0 brightness
Serial.println("enter color command (e.g. 'r') :");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
if(readSerialString(serInString) != 0) {
colorCode = serInString[0];
if(colorCode == 'q') {
resestLED = serInString[1];
Serial.print("resetting color ");
if (resestLED == 'r') {
redColorVal = 0;
Serial.print(resestLED);
Serial.println();
analogWrite(redPin, redColorVal);
} else if (resestLED == 'g') {
greenColorVal = 0;
analogWrite(greenPin, greenColorVal);
Serial.print(resestLED);
Serial.println();
} else if (resestLED == 'b') {
blueColorVal = 0;
analogWrite(bluePin, blueColorVal);
Serial.print(resestLED);
Serial.println();
}
} else if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
serInString[0] = 0; // indicates we've used this string
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
if(colorCode == 'r') {
redColorVal += colorVal;
Serial.print(redColorVal);
Serial.println();
analogWrite(redPin, redColorVal);
} else if(colorCode == 'g') {
greenColorVal += colorVal;
Serial.print(greenColorVal);
Serial.println();
analogWrite(greenPin, greenColorVal);
} else if(colorCode == 'b') {
blueColorVal += colorVal;
Serial.print(blueColorVal);
Serial.println();
analogWrite(bluePin, blueColorVal);
}
}
}
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
//Modified it to return an int to avoid looping bug.
int readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return 0;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
return 1;
}
- Login to post comments