Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
Assignment: Digital I/O with Arduino Boards + Diffuser
3 LEDs w/ Diffusion and Serial Commands
Donna Leo
I experimented with various diffusing materials. The packing shell from class created a nice even diffusion. I tried to use a small glass, and a plastic container, but wasn’t able to find the correct size or the lights shined through too clearly. I eventually used the opaque top from a deodorant bottle. I put poly-stuffing material inside the plastic top, and placed that on top of the 3 LEDs. It created a nice even diffusion. The photos upload are from the last attempt.
Mode1: Downloaded code to allow serial input.
* 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
*
Mode 2: If the user enters multiple values for a given color, each letter corresponds to 10% brightness (25), so rr = brightness of 50, rrg = brightness 50 red, 25 green.
Mode 3: The code controls the colors produced by the LEDs by typing in the color name "violet" or "emerald" or “orange.”
Components Used
- breadboard
- 3 220 Ohm resistors
- wires (yellow for pins to resistors toward LEDs, blue and black for ground)
- 3 LED (1 blue, 1 red, 1 green)
- Arduino board
- USB cable
- 1 rubber bands for stability
- For diffusion (poly-stuffing, plastic deodorant cap)
Modified Arduino Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
* All 3 Modes work simultaneously
*
* Mode 1:
* 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
*
* Homework - Assignment 2
*
* Mode 2:
* Create alternate command structure: "<colorCode>*"
* each press of a letter corresponds to 10% brightness (25)
* so rr = brightness of 50, rrg = brightness 50 red, 25 green
*
* Mode 3:
* Create alternate command structure: "<color name>"
* enter "violet" it will produce the color violet
* enter "emerald" it will produce the color emerald
* enter "orange" and it will produce the color orange
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Used the code by Aylin Selcukoglu as a template
*
* Modified 15 September 2009
* Donna Leo
*/
//include support for manipulating strings.
//for a useful string comparison function, see the bottom of this file... stringsEqual()
#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;
char valTwo = 0;
int colorVal = 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
int redVal = 0;
int blueVal = 0;
int greenVal = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127); // set them all to mid brightness
redVal = 127;
analogWrite(greenPin, 127); // set them all to mid brightness
greenVal = 127;
analogWrite(bluePin, 127); // set them all to mid brightness
blueVal = 127;
Serial.println("enter color command, valid formats: 'r066', 'bbgr', 'violet', 'emerald' or 'orange') :");
}
void loop () {
//read the serial port and create a string from the input
readSerialString(serInString); // store in array serInString
// with MODE 1 and MODE 2 colorCode will be r, g, or b
// with MODE 3 colorCode can be any character string spelling a color
colorCode = serInString[0];
// with MODE 1 valTwo will be a number
// with MODE 2 valTwo will be r, g, or b
// with MODE 3 valTwo will be some character string spelling a color
valTwo = serInString[1];
// if colorCode != r, g, or b then go to MODE 3, otherwise:
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
// if the second char = r, g or b then MODE 2:
if ( valTwo == 'r' || valTwo == 'g' || valTwo == 'b' || valTwo == '\0' ) {
int x = 0;
// while string still has new data (not null)
while ( serInString[x] != '\0' && x < 100 ){
colorCode = serInString[x];
// increase brightness of red by 10% (25)
if ( colorCode == 'r' ) {
redVal = (redVal + 25) % 255;
analogWrite(redPin, redVal);
Serial.print("color red set to ");
Serial.println(redVal);
}
// increase brightness of green by 10% (25)
else if ( colorCode == 'g' ) {
greenVal = (greenVal + 25) % 255;
analogWrite(greenPin, greenVal);
Serial.print("color green set to ");
Serial.println(greenVal);
}
// increase brightness of blue by 10% (25)
else if ( colorCode == 'b' ) {
blueVal = (blueVal + 25) % 255;
analogWrite(bluePin, blueVal);
Serial.print("color blue set to ");
Serial.println(blueVal);
}
// move to next character to evaluate
x++;
}
}
// else, Mode 1:
else {
colorVal = atoi(serInString+1); // converts string to integer
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r') {
analogWrite(redPin, colorVal);
redVal = colorVal;
}
else if(colorCode == 'g') {
analogWrite(greenPin, colorVal);
greenVal = colorVal;
}
else if(colorCode == 'b') {
analogWrite(bluePin, colorVal);
blueVal = colorVal;
}
}
}
// else, MODE 3
else {
// make violet
if ( stringsEqual(serInString, "violet", 6) ) {
redVal = 140;
greenVal = 40;
blueVal = 230;
analogWrite(redPin, redVal);
analogWrite(bluePin, blueVal);
analogWrite(greenPin, greenVal);
Serial.println("setting color to violet ( r = 140, g = 040, b = 230)");
}
// make emerald
if ( stringsEqual(serInString, "emerald", 7) ) {
redVal = 0;
greenVal = 210;
blueVal = 210;
analogWrite(redPin, redVal);
analogWrite(bluePin, blueVal);
analogWrite(greenPin, greenVal);
Serial.println("setting color to emerald (r = 0, g = 210, b = 210)");
}
// make emerald
if ( stringsEqual(serInString, "orange", 6) ) {
redVal = 200;
greenVal = 010;
blueVal = 010;
analogWrite(redPin, redVal);
analogWrite(bluePin, blueVal);
analogWrite(greenPin, greenVal);
Serial.println("setting color to orange (r = 200, g = 010, b = 010)");
}
}
// reset variables
// colorVal = 0;
// reset string - prepare it for new input data
resetSerialString(serInString, 100);
delay(100); // wait a bit, for serial data
}
// erase all data in string to make it fresh
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 i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
//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;
}
}
<!--EndFragment-->