Description:
In this lab we used the serial terminal to enter inputs controling 3 LEDs. Our next instruction was to find a diffusor to diffuse the light. I chose a mini-haloween lamp shade since haloween is coming up. With my code an entry of a any single letter, 'r', 'g', 'b', increases the brightness by 10%, while an entry of 'z' resets all brightness to zero.
Components Used:
1- Arduino Uno
1- Breadboard
3- LEDs
3-220 ohm resistors
7- wires
Code:
/*
* 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
*
*/
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 redVal = 0;
int greenVal = 0;
int blueVal = 0;
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
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redVal); // set them all to 0
analogWrite(greenPin, greenVal); // set them all to 0
analogWrite(bluePin, blueVal); // set them all to 0
Serial.println("enter color command (e.g. 'r43' or 'rrrrgb') :");
}
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]; //keep track of colorCode in array
redVal = redVal; //start color values at 0
greenVal = greenVal;
blueVal = blueVal;
if (colorCode == 'r' || colorCode == 'g' || colorCode == 'b'|| colorCode == 'z'){
for(int i=0; i<100; i++){ //this is a for loop to go through the array
if(serInString[i]== 'r') //increase brightness by 10
redVal = redVal + 25.5;
else if(serInString[i]== 'g') //g increases greenpin by 10
greenVal = greenVal + 25.5;
else if(serInString[i]== 'b')
blueVal = blueVal + 25.5;
else if(serInString[i]== 'z'){
redVal = 0; //reset color values at 0
greenVal = 0;
blueVal = 0;
}
}
if (redVal > 255)
redVal = 0;
else if (greenVal > 255)
greenVal = 0;
else if (blueVal > 255)
blueVal = 0;
Serial.println(); //tell user what level LED is on
Serial.print("Red Brightness:");
Serial.print(redVal);
Serial.println();
Serial.print("Green Brightness: ");
Serial.print(greenVal);
Serial.println();
Serial.print("Blues Brightness: ");
Serial.print(blueVal);
Serial.println();
analogWrite(redPin, redVal); //set the values of the leds to the levels that have been indicated
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
delay(100); //delay program and wait for serial data
}
void readSerialString(char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i]=Serial.read();
i++;
}
}
- Login to post comments