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!


Diffuser and Controlling LEDs Through Pressing Keys

Project Members: 
Jess Kline

Part 1 - Find a diffuser

Description

I used the DimmingLEDs code and two household objects as diffusers: a wiffle ball and a nalgene bottle. The wiffle ball was especially effective because the LEDs fit nicely within its holes and the light projects nicely through the holes.

Components

  • Lab 2 Arduino Setup: LEDs, Resistors, Breadboard, Arduino, Yellow and green wires
  • Diffusers: Nalgene bottle, Wiffle ball

Code

/*
* Code for cross-fading 3 LEDs, red, green and blue, or one tri-color LED, using PWM
* The program cross-fades slowly from red to green, green to blue, and blue to red
* The debugging code assumes Arduino 0004, as it uses the new Serial.begin()-style functions
* Clay Shirky <clay.shirky@nyu.edu>
*/

// Output
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

// Program variables
int redVal = 255; // Variables to store the values to send to the pins
int greenVal = 1; // Initial values are Red full, Green and Blue off
int blueVal = 1;

int i = 0; // Loop counter
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial

void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
if (DEBUG) { // If we want to see the pin values for debugging...
Serial.begin(9600); // ...set up the serial ouput on 0004 style
}
}

// Main program
void loop()
{
i += 1; // Increment counter
if (i < 255) // First phase of fades
{
redVal -= 1; // Red down
greenVal += 1; // Green up
blueVal = 1; // Blue low
}
else if (i < 509) // Second phase of fades
{
redVal = 1; // Red low
greenVal -= 1; // Green down
blueVal += 1; // Blue up
}
else if (i < 763) // Third phase of fades
{
redVal += 1; // Red up
greenVal = 1; // Green low
blueVal -= 1; // Blue down
}
else // Re-set the counter, and start the fades again
{
i = 1;
}

analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);

if (DEBUG) { // If we want to read the output
DEBUG += 1; // Increment the DEBUG counter
if (DEBUG > 10) // Print every 10 loops
{
DEBUG = 1; // Reset the counter

Serial.print(i); // Serial commands in 0004 style
Serial.print("\t"); // Print a tab
Serial.print("R:"); // Indicate that output is red value
Serial.print(redVal); // Print red value
Serial.print("\t"); // Print a tab
Serial.print("G:"); // Repeat for green and blue...
Serial.print(greenVal);
Serial.print("\t");
Serial.print("B:");
Serial.println(blueVal); // println, to end with a carriage return
}
}
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}

Pictures

Part 1 - Wiffle1Part 1 - Wiffle1

Part 1 - Wiffle2Part 1 - Wiffle2

Part 1 - NalgenePart 1 - Nalgene

Part 2 - Control the RGB values using multiple key presses

Description

I used the Serial LED Enhanced code and modified it so that each key press increased the brightness by 25 rather than 10. In the attached photo the input was 'rrrgggbbb', making the LEDs shine at almost one third capacity.

Components

  • Lab 2 Arduino Setup: LEDs, Resistors, Breadboard, Arduino, Yellow and green wires

Code

/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode>*", where "colorCode" is
* one of "r","g", or "b".
* E.g. "r" increases the red LED brightness by 25
* "rrr" increases the red LED brightness by 75
* "ggb" increases the green LED brightness by 50 and the blue by 25
*
* 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;
// -> 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 = 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); // 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. 'r' or 'gbr' or 'bbbb') :");
}

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;

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

Pictures

Part 2Part 2

 

 


Comments

GSI Comments

Nice work! The wiffle ball does look cool -- I'd be interested to hear how such an object might be used in a tangible interface. What might the lights going in different directions do? Or, what would happen if you created a diffuser where users were supposed to look through the holes to the inside of the diffuser?

The modified code for part 2 is a good way to get familiar with Arduino programming. In the future, it would be helpful to try rewriting some of the provided code from scratch (e.g. without looking at the sample code first), which will help you get really familiar with all of the code.


Powered by Drupal - Design by Artinet