Description:
The goal of this assignment was to build upon our previous knowledge of programming an Arduino Uno to make an LED blink. In Parts 1 & 2 of this lab, I learned to make the LED fade and then built a more complicated circuit with three LEDs of different colors. Part 3 of this lab introduced me to serial communications. I modified some example code to, when given a command in the serial monitor:
* change the brightness levels of the LEDs
(typing 'rrrrr' will turn the red LED to 50% brightness)
* turn all of the LEDs off
(typing 'x' will turn all of the LEDs to a brightness of 0)
* turn all of the LEDs to 100% brightness
(typing 'f' will turn all of the LEDs to a brightness of 255)
After successfully being able to control the LEDs, I made a diffuser out of paper by creating a small iambic icosahedron oragami star.
Components Used:
1 - Arduino Uno
1 - Breadboard
3 - 220-ohm Resistors
1 - Red LED
1 - Green LED
1 - Blue LED
wires
Diffuser:
small pieces of paper folded into an oragami shape
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.
* Alternate command structure is "<colorCode>*", where "colorCode" is
* one of "r","g", or "b".
* E.g. "r" increases the red LED brightness to 25 (10% brightness)
* "rrrrr" increases the red LED brightness to 127 (50% brightness)
* "ggb" increases the green LED brightness by 20 and the blue by 10
* The code presented in this lab assignment is modified from the example code:
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Adapted 5 September 2007
* copylefter 2007 Ryan Aipperspach <ryanaip@alumni.rice.edu>
*
*/
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
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 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); // red LED is turned off
analogWrite(greenPin, greenValue); // green LED is turned off
analogWrite(bluePin, blueValue); // blue LED is turned off
Serial.println("enter color command (e.g. 'rrr') :");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString, 100);
//Uncomment the following line to read commands of the form 'r245' or 'b3'
//processNumericalCommands(serInString);
processRepeatKeyCommands(serInString, 100);
//Erase anything left in the serial string, preparing it for the
//next loop
resetSerialString(serInString, 100);
delay(100); // wait a bit, for serial data
}
void resetSerialString (char *strArray, int length) {
for (int i = 0; i < length; i++) {
strArray[i] = '\0';
}
}
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray, int maxLength) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available() && i < maxLength) {
strArray[i] = Serial.read();
i++;
}
}
//go through the string, and increase the red value for each 'r',
//the green value for each 'g', and the blue value for each 'b'.
void processRepeatKeyCommands(char *strArray, int maxLength) {
int i = 0;
//loop through the string (strArray)
//i = the current position in the string
//Stop when either (a) i reaches the end of the string or
// (b) there is an empty character '\0' in the string
while (i < maxLength && strArray[i] != '\0') {
//Read in the character at position i in the string
colorCode = serInString[i];
if (colorCode == 'r') {
//Increase the current red value by 25, and if you reach 256 go back to 0
redValue = (redValue + 25) % 256;
analogWrite(redPin, redValue);
Serial.print("setting color r to ");
Serial.println(redValue);
} else if (colorCode == 'g') {
greenValue = (greenValue + 25) % 256;
analogWrite(greenPin, greenValue);
Serial.print("setting color g to ");
Serial.println(greenValue);
} else if (colorCode == 'b') {
blueValue = (blueValue + 25) % 256;
analogWrite(bluePin, blueValue);
Serial.print("setting color b to ");
Serial.println(blueValue);
} else if (colorCode == 'x') {
redValue = 0;
analogWrite(redPin, redValue);
greenValue = 0;
analogWrite(greenPin, greenValue);
blueValue = 0;
analogWrite(bluePin, blueValue);
Serial.println("turning all LEDs off ");
} else if (colorCode == 'f') {
redValue = 255;
analogWrite(redPin, redValue);
greenValue = 255;
analogWrite(greenPin, greenValue);
blueValue = 255;
analogWrite(bluePin, blueValue);
Serial.println("turning all LEDs to 100% brightness");
}
//Move on to the next character in the string
//From here, the code continues executing from the "while" line above...
i++;
}
}
//change the value of the red, green, or blue LED according to the command received.
//for example, r240 sets the red LED to the value 240 (out of 255)
void processNumericalCommands(char *strArray) {
//read in the first character in the string
colorCode = serInString[0];
//if the first character is r (red), g (green) or b (blue), do the following...
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
//convert the string to an integer
//(start at the second character, or the beginning of the string '+1')
colorVal = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
}
}
//compare two strings to see if they are equal
//compares the first 'numCharacters' characters of string1 and string2 to
//see if they are the same
//
//E.g. stringsEqual("hello","hello",5) => true
// stringsEqual("hello","helaabbnn",3) => true
// stringsEqual("hello","helaa",5) => false
boolean stringsEqual(char *string1, char *string2, int numCharacters) {
if (strncmp(string1, string2, numCharacters) == 0) {
return true;
} else {
return false;
}
}
- Login to post comments