Description
For this lab, we began with our assignment from Lab 1 but added two more LEDs and connected the wires to pins that support Pulse Width Modulation (PWM). We used a provided code which made the LEDs appear to fade into different color combinations.
Next we explored serial communication. I modified the provided serial led rgb code so that when you type 'r', 'g' or 'b' on the computer's keyboard, the brightness of the respective LED increases by approximately 10 percent. After full brightness of an LED is reached, the LED will return to its dimmest setting.
I created a diffusor for the LEDs by placing cotton squares and pieces into a miniature glass jam jar and inverting it over the lights.
Components Used
1- Arduino UNO
1- Breadboard
3- 220 ohm resistors
7- wires
3- LEDs (red, blue, green)
1- mini jam jar
1- cotton ball
1- cotton square
Code
/* This code is modified from the original author's code to increase the brightness of LEDs incrementally.
*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode>", where "colorCode" is
* one of "r","g",or "b"
* E.g. pressing "r" "g" or "b" will increase the brightness of the respective LED by 10 percent
*
* Original code by Tod E. Kurt <tod@todbot.com
* 18 October 2006
* http://todbot.com/
*
* Modified code by Sydney Mayes
* September 2013
*/
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 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;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redValue); // sets red LED to 0 brightness
analogWrite(greenPin, greenValue); // sets green LED to 0 brightness
analogWrite(bluePin, blueValue); // sets blue LED to 0 brightness
Serial.println("enter color command 'r' 'g' or 'b' to increase brightness by 10 percent :");
}
void loop () {
// set all the values in the array to zero
memset(serInString, 0, 100);
readSerialString(serInString);
colorCode = serInString[0];
if( colorCode == 'r' ) {
redValue=(redValue+25) % 255; // increase red brightness by 10 percent until full brightness is reached, //then return to dimmest setting
Serial.print("increasing ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(redValue);
Serial.println();
analogWrite(redPin, redValue);
}
else if(colorCode == 'g') {
greenValue=(greenValue+25) % 255; // increase green brightness by 10 percent until full brightness is //reached, then return to dimmest setting
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(greenValue);
Serial.println();
analogWrite(greenPin, greenValue);
}
else if(colorCode == 'b') {
blueValue=(blueValue+25) % 255; // increase blue brightness by 10 percent until full brightness is reached, //then return to dimmest setting
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(blueValue);
Serial.println();
analogWrite(bluePin, blueValue);
}
serInString[0] = 0;
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()) {
//..read each character into the array, one at a time
strArray[i] = Serial.read();
i++;
}
}
- Login to post comments