Announcements

November 24, 2007
Reading for November 27th, are now posted. Enjoy!

October 2, 2007
To upload your thoughtless acts, create a new assignment page like any other lab. You'll see "Thoughtless Acts" listed as one of the assignment options.

May 24, 2008
This site has been archived and is no longer editable. Stay tuned for the next version, coming in the fall!


Revision of LEDs and Repeat Keystrokes from Thu, 09/13/2007 - 09:08

Project Members: 
Shawna Hein

Description

Makes it so the LEDs get brighter on command, so you can enter in 'rrr' 'rrbb' etc to make each LED brighter.

Components Used

Arduino Diecimila
3 LEDS (1 red, 1 blue, 1 green)
3 resistors

Arduino Code

//*
* Serial RGB LED - extended
* ---------------
* 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
*
* OR
* Command structure is "<colorCode><colorCode>...<colorCode>", where "colorCode" is
* one of "r","g",or "b" and you can enter many at once to increase brightness
* E.g. "rr" turns the red LED up 20%.
* "rrgg" turns the red LED up 20% and the green LED up 20%
* "bggr" turns the blue LED to 1/4 brightness
*
* OR
* Command structure is "<colorName>", where "colorName" is "purple" or "pink"
* This sets the red LED, blue LED and green LED to the appropriate numbers.
*
*
* Created 13 September
*
* Shawna Hein
*
* extended version of Serial LED
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/

//include support for manipulating strings.
#include <stdio.h>
#include <string.h>

int MAXSIZE = 30;
char serInString[30]; // 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 redVal, greenVal, blueVal;

int redPin = 11; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 9; // 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);
redVal = 127;
greenVal = 127;
blueVal = 127;
analogWrite(redPin, redVal); // set them all to mid brightness
analogWrite(greenPin, greenVal); // set them all to mid brightness
analogWrite(bluePin, blueVal); // set them all to mid brightness
Serial.println("enter color command (e.g. 'r130','rrrbb', or 'purple') :");
}

void loop() {
readSerialString(serInString);
//processNumericalCommands(serInString);
processRepeats(serInString);
//processColorNames(serInString);
resetSerialString(serInString);
}

//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);
}
}

//change the value of the red, green, or blue LED according to the command received.
//for example, rrr raises the red LED by 30%, bg raises the blue by 10 and the green by 10
void processRepeats(char *strArray){
int i = 0;
while (strArray[i] != '\0' && i<MAXSIZE) {
colorCode = strArray[i];

if(colorCode == 'r'){
redVal = (redVal+10) % 255;
if(redVal>255){
redVal=0;
}
Serial.print("Incrementing Red Value to ");
Serial.print(redVal);
analogWrite(redPin, redVal);
}
else if(colorCode == 'b'){
blueVal = (blueVal+10) % 255;
if(blueVal>255){
blueVal=0;
}
Serial.print("Incrementing Blue Value to ");
Serial.print(blueVal);
analogWrite(bluePin, blueVal);
}
else if(colorCode == 'g'){
greenVal = (greenVal+10) % 255;
if(greenVal>255){
greenVal=0;
}
Serial.print("Incrementing Green Value to ");
Serial.print(greenVal);
analogWrite(greenPin, greenVal);
}
else{
Serial.print("This is an invalid letter");
}
i++;
}
}

//change the value of the red, green, or blue LED according to the command received.
//for example, purple sets the LED to appropriate numbers
//
//**NOTE
this isnt working right now and i have spent hours trying to figure out
why... it reads in the color name fine but stringsEqual doesn't work
//.. maybe string.h isn't being included somehow?? some input would help**
//
void processColorNames(char *strArray){
int i=0;
while (strArray[i] != '\0' && i<MAXSIZE) {
Serial.print(strArray[i]);
i++;
}
if (stringsEqual(strArray,"purple", 6)) {
analogWrite(redPin, 160);
analogWrite(greenPin, 32);
analogWrite(bluePin, 240);
Serial.print("Switching to Purple");
}
else if (stringsEqual(strArray,"pink",4)) {
analogWrite(redPin, 255);
analogWrite(greenPin, 20);
analogWrite(bluePin, 157);
Serial.print("Switching to Pink");
}
}

//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;
}
}

void resetSerialString (char *strArray) {
for (int i = 0; i < MAXSIZE; 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++;
}
}

Item

hmm can't figure out why these pix are so small

 LEDs with eggshellLEDs with eggshell

 

LEDS with ShellLEDS with Shell

 

 


Powered by Drupal - Design by Artinet