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!


Lab 2: In-n-Out (sadly, not a cheeseburger)

Project Members: 
Ken-ichi Ueda

Description

  1. Find a diffuser for our LEDs
  2. Write a program that allows for serial control of LED brightness.
  3. Add some added functionality to the serial controls

My diffuser is an old rice measuring cup. It's not quite as opaque as I would like, but it does the job.

I wasn't sure how much of the example code we were supposed to copy in part 2, so I tried not to copy too much of it, resulting in a bit of mess, but a functional one. Lowercase color codes ('r', 'g', 'b') dim the brightness of their corresponding LEDs, and uppercase codes increase brightness. Codes can be strung together to control multiple lights in a single statemenet (e.g. 'rrrGGGbbbbbbb'). I also added two additional commands f and F, which fade LEDs to 0 and 255, respectively (e.g. 'fg' to fade the green LED to 0).

Components Used

3 LEDs (red, green, blue)

3 resistors

 

Arduino 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>
*
* Adapted by Ken-ichi Ueda <kueda@ischool.berkeley.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= 127;
int greenVal = 127;
int blueVal = 127;
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;

int wait = 5; // millesconds to wait btwn fade increments
int inc = 10; // increment value for brightening or dimming LEDs
int i = 0; // Loop counter
char cmd; // holds a command character
char tled; // target LED of a command ('r', 'g', 'b')

void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
writeAllValues();

Serial.println("Commands:");
Serial.println(" r -- decreases red LED brightness by 10");
Serial.println(" RRR -- increases red LED brightness by 30");
Serial.println(" fr -- fade out red LED");
Serial.println(" Fr -- fade in red LED to max brightness");
Serial.println();
Serial.println("Enter a command: ");
}

// Main program
void loop()
{
readSerialString(serInString, 100);
cmd = serInString[0];

// fade controls
if (cmd == 'f') {
//Serial.println("Fade out requested...");//test
tled = serInString[1];
fadeOut(tled);
}
else if (cmd == 'F') {
//Serial.println("Fade in requested...");//test
tled = serInString[1];
fadeIn(tled);
}
// brightness control: look for rgb chars, uppercase brightens and lowercase
// dims
else if (cmd == 'r' || cmd == 'R' || cmd == 'g' || cmd == 'G' || cmd == 'b' || cmd == 'B') {
Serial.println("Changing brightness...");//test
for(i=0; i < 100 && serInString[i] != '\0'; i++) {
//Serial.println("Looping over brightness commands...");//test
switch (serInString[i]) {
case 'r':
redVal -= inc;
break;
case 'R':
redVal += inc;
break;
case 'g':
greenVal -= inc;
break;
case 'G':
greenVal += inc;
break;
case 'b':
blueVal -= inc;
break;
case 'B':
blueVal += inc;
break;
}
}

trimValues();
writeAllValues();
}

resetSerialString(serInString, 100);
delay(100); // wait a bit, for serial data
}

// makes sure none of the brightness changes went out of the 0-255 range
void trimValues() {
if (redVal > 255) {
redVal = 255;
}
else if (redVal < 0) {
redVal = 0;
}
if (greenVal > 255) {
greenVal = 255;
}
else if (greenVal < 0) {
greenVal = 0;
}
if (blueVal > 255) {
blueVal = 255;
}
else if (blueVal < 0) {
blueVal = 0;
}
}

// write all brightness values to the pins
void writeAllValues() {
//Serial.println("Writing values...");//test
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}

// fade in LED in by pin number
void fadeIn(int pin) {
Serial.print("Fading in pin ");
Serial.print(pin);
Serial.println("...");
switch (pin) {
case 9:
while (redVal < 255) {
redVal += 1;
analogWrite(pin, redVal);
delay(wait);
}
break;
case 10:
while (greenVal < 255) {
greenVal += 1;
analogWrite(pin, greenVal);
delay(wait);
}
break;
case 11:
while (blueVal < 255) {
blueVal += 1;
analogWrite(pin, blueVal);
delay(wait);
}
break;
}
}

// fade in LED in by color
void fadeIn(char colorCode) {
Serial.print("Fading in ");
Serial.print(colorCode);
Serial.println(" pin...");
switch (colorCode) {
case 'r':
fadeIn(redPin);
break;
case 'g':
fadeIn(greenPin);
break;
case 'b':
fadeIn(bluePin);
break;
}
}

// fade out LED in by pin number
void fadeOut(int pin) {
Serial.print("Fading out pin ");
Serial.print(pin);
Serial.println("...");
switch (pin) {
case 9:
while (redVal > 0) {
redVal -= 1;
analogWrite(pin, redVal);
delay(wait);
}
break;
case 10:
while (greenVal > 0) {
greenVal -= 1;
analogWrite(pin, greenVal);
delay(wait);
}
break;
case 11:
while (blueVal > 0) {
blueVal -= 1;
analogWrite(pin, blueVal);
delay(wait);
}
break;
}
}

// fade out LED in by color
void fadeOut(char colorCode) {
switch (colorCode) {
case 'r':
fadeOut(9);
break;
case 'g':
fadeOut(10);
break;
case 'b':
fadeOut(11);
break;
}
}

// full input arr with null values
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++;
}
}

Item

DiffuserDiffuser

Three LED Bread BoardThree LED Bread Board


Comments

GSI Comments

Nice work! The diffuser does look nice, although I agree with your comment that it would be better if it were even more opaque.

You're always welcome to copy the code provided, but doing it yourself (as you did here) is a great way to learn about Arduino programming. So I'd continue to to that as much as possible.


Powered by Drupal - Design by Artinet