Description:
The purpose of the lab was to collect user input from the serial terminal and adjust as necessary the analog output of the three LEDs.
Using a diffuser, a combination of the red-green-blue light can create a range of colors.
I used two coffee filters positioned with ample room above the LEDs. The additional distance allowed light to reflect off of the walls of the coffee filter and diffuse together.
The program takes an arbitrary value of characters from the terminal and evaluates the number of occurrences of valid color-code characters corresponding to ‘r’, ‘g’, and ‘b’. Each validated color-code corresponds to an addition of LED light output corresponding to 10% of total up to a maximum of 100%.
Components Used:
1- Breadboard
1- Arduino Uno
3 LEDs (Red, Blue, Green)
1 USB interface cable to Arduino
3 220 Ohm Resistors
7 wires
Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is a series of "<colorCode><colorC>", where "colorCode" is
* one of "r","g",or "b". Each valid code equates into a 10% of maximum led illumination up to a maximum of 100% (255 analog)
* E.g. "rrrrrr" sets red brightness at 50% (5 times r)
* "rrggbb" sets red, blue, green brightness all at 20%.
*/
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 blue_value =0;
int green_value = 0;
int red_value = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, red_value); // set them all to mid brightness (127)
analogWrite(greenPin, green_value); // set them all to mid brightness
analogWrite(bluePin, blue_value); // 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' ) {
readWriteValues(serInString);
}
delay(100); // wait a bit, for serial data
}
void readWriteValues(char *strArray) {
char array_value;
int red_count = 0;
int green_count = 0;
int blue_count =0;
//Count the number of instances of ‘r’, ‘g’, and ‘b’ letters from the serial string
for(int counter =0; counter < 100; counter++){
array_value = strArray[counter];
if(array_value =='r'){
red_count++;
}
if (array_value == 'g'){
green_count++;
}
if (array_value == 'b'){
blue_count++;
}
}
// Write the appropriate values to the proper LED using the right analog output value
if (red_count >0){
Serial.println("Color value red = ");
Serial.println(red_count);
Serial.println(checkValue(red_count));
analogWrite(redPin, checkValue(red_count));
}
if (green_count >0){
Serial.println("Color value green = ");
Serial.println(green_count);
Serial.println(checkValue(green_count));
analogWrite(greenPin, checkValue(green_count));
}
if (blue_count > 0){
Serial.println("Color value blue = ");
Serial.println(blue_count);
Serial.println(checkValue(blue_count));
analogWrite(bluePin, checkValue(blue_count));
}
}
// Functions converts from a scale 1-10 the 10% increments and returns the proper analog values
int checkValue(int input_val){
if(input_val >10){
return 255;
}else{
return input_val *.1 * 255;
}
}
//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