Lab 2 - PWM & Diffuser
Description
Serial commands to control the brightness of LEDs. Using a diffuser to create new colors. Reading string inputs to control LEDs.
Components Used
3 - 220 ohm resistors, 3 LEDs (red, green, blue), wiring, light bulb for the diffuser.
Code 1 - takes rrrr, rrbb as input and adjusts appropriate LEDs.
/*
* 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
*
* Alternate command structure is "<colorCode>*", where "colorCode" is
* one of "r","g", or "b".
* E.g. "r" increases the red LED brightness by 10
* "rrr" increases the red LED brightness by 30
* "ggb" increases the green LED brightness by 20 and the blue by 10
*
* 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>
*
*/
//include support for manipulating strings.
#include <stdio.h>
#include <string.h>
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 greenPin = 9; // Green LED, connected to digital pin 9
int redPin = 10; // Red LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int redValue = 1;
int greenValue = 1;
int blueValue = 1;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redValue); // set them all to mid brightness
analogWrite(greenPin, greenValue); // set them all to mid brightness
analogWrite(bluePin, blueValue); // set them all to mid brightness
Serial.println("enter color command (e.g. 'rr or rrrrrrrrbbbb') :");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString, 100);
//read commands of the form 'rrrb'
processRepeatKeyCommands(serInString, 100);
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'.
//For example "rrrg" increases red by 30 and green by 10.
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 the character is r (red)...
if (colorCode == 'r') {
//Increase the current red value by 10, and if you reach 255 go back to 0
redValue = (redValue + 10) % 255;
analogWrite(redPin, redValue);
Serial.print("setting color r to ");
Serial.println(redValue);
//If the character is g (green)...
} else if (colorCode == 'g') {
greenValue = (greenValue + 10) % 255;
analogWrite(greenPin, greenValue);
Serial.print("setting color g to ");
Serial.println(greenValue);
//If the character is b (blue)...
} else if (colorCode == 'b') {
blueValue = (blueValue + 10) % 255;
analogWrite(bluePin, blueValue);
Serial.print("setting color b to ");
Serial.println(blueValue);
}
//Move on to the next character in the string
//From here, the code continues executing from the "while" line above...
i++;
}
}
Code 2 - takes names of fruits as input, and changes the LEDs to mimic the color of the fruit.
/*
* Serial RGB LED Control
* Fruit Names control color of LEDs
*
* Credit for some portions of this code to:
* Tod E. Kurt <tod@todbot.com
* Ryan Aipperspach <ryanaip@alumni.rice.edu>
*
*/
#include <stdio.h>
#include <string.h>
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
// Output
int greenPin = 9; // Green LED, connected to digital pin 9
int redPin = 10; // Red LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
//initial values
int greenValue = 1;
int redValue = 1;
int blueValue = 1;
// Setup
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("Enter a fruit name! (Limited to apple, banana, blueberry, cherry and melon only) :");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString, 100);
doFruitName(serInString, 100);
resetSerialString(serInString, 100);
delay(100); // wait a bit, for serial data
}
//reset serial string
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
void readSerialString (char *strArray, int length) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available() && i < length) {
strArray[i] = Serial.read();
i++;
}
}
//read a string
void doFruitName(char *strArray, int length) {
if (stringsEqual(strArray, "cherry", 6)) {
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
else if (stringsEqual(strArray, "apple", 5)) {
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
}
else if (stringsEqual(strArray, "blueberry", 9)) {
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
}
else if (stringsEqual(strArray, "melon", 5)) {
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
}
else if (stringsEqual(strArray, "banana", 6)) {
analogWrite(redPin, 255);
analogWrite(greenPin, 20);
analogWrite(bluePin, 0);
}
}
boolean stringsEqual(char *string1, char *string2, int numCharacters) {
if (strncmp(string1, string2, numCharacters) == 0) {
return true;
}
else {
return false;
}
}
Images
blue
green
Comments
GSI Comments
Great work. I like the mapping between fruits and colors -- simple but creative! The light bulb works really well as a diffuser. (Which makes sense, since it's designed to.) Good use of a found object that suits the job perfectly. If only you had a fruit shaped light bulb...