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!


Jill Blue Lin - assignment 2

Project Members: 
Jill Blue Lin

assignment2

Digital Input and Output

Description

Keyboard input changes the brightness of the LEDs on the Arduino board

Components Used

3 LEDs

3 Resistors

Image

assignment2

diffuser

Diffuser is an origami paper bunny.

Arduino Code

/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* 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>
*
* Adapted 12 September 2007 by Jill Blue Lin
*/

//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;
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 = 125;
int greenValue = 125;
int blueValue = 125;

void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600); //jbl: not sure what this line does
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. 'rrrrrrrrbbbb') :");
}

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 'rrrb'
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'.
//For example "rrrg" increases red by 30 and green by 10.
void processRepeatKeyCommands(char *strArray, int maxLength) {
int i = 0;
boolean redEntered = false; //variable for storing whether or not user entered r
boolean greenEntered = false; //variable for storing whether or not user entered g
boolean blueEntered = false; //variable for storing whether or not user entered b

//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') {
redEntered = true;
//Increase the current red value by 10, and if you reach 255 go back to 0
redValue = (redValue + 10) % 255;
analogWrite(redPin, redValue);

//If the character is g (green)...
} else if (colorCode == 'g') {
greenEntered = true;
greenValue = (greenValue + 10) % 255;
analogWrite(greenPin, greenValue);

//If the character is b (blue)...
} else if (colorCode == 'b') {
blueEntered = true;
blueValue = (blueValue + 10) % 255;
analogWrite(bluePin, blueValue);
}

//Move on to the next character in the string
//From here, the code continues executing from the "while" line above...
i++;
}

//JBL modification: changed this to print color setting once only, at the end of the while loop,
//instead of once for every character entered

//If r was entered, print the color it was set to
if (redEntered) {
Serial.print("setting color r to ");
Serial.println(redValue);
}

//If g was entered, print the color it was set to
if (greenEntered) {
Serial.print("setting color g to ");
Serial.println(greenValue);
}

//If b was entered, print the color it was set to
if (blueEntered) {
Serial.print("setting color b to ");
Serial.println(blueValue);
}
}

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


Comments

GSI Comments

Using paper for your diffuser is a cool idea, as you can create interesting effects with different thicknesses and folds. It reminds me of Kirsten Hassenfeld's work.

It's a little hard to see the image, and it would be great to see what the diffuser looks like when the room is dark. Do you have a way to create higher-resolution pictures? If not, let us know and we'd be glad to help out.


Powered by Drupal - Design by Artinet