Tuesday, March 16, 2010

March Madness Challenge - Day 16

Wired and ADXL335 accelerometer to RGB leds. Thought neat I've got a simple program. Oops, the only difference between the Joystick controlled RGB LED was that I was measuring X, Y, and Z axis. Three variable between two programs. Yikes, that sounds a lot like a rule violation. So in the last few minutes I've added self calibration to the device. I didn't have time to work out the math and turn the numbers into something meaning so I did something to make it more meaningful to an LED. x, y, and z max and min values are now checked and set so depending on the most recent max and mins the widest led action occurs.


/*
* Accelerometer controlled red, green, blue leds
* ADXL35
*/


int redPin = 9; // LED connected to digital pin 9
int greenPin = 10;
int bluePin = 11;
int Z = 0;
int Y = 1;
int X = 2;
int r,g,b = 0;
int xmax, ymax, zmax = 0;
int xmin, ymin, zmin = 1024;

void rgb (int r, int g, int b)
{
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);

}

void setup() {
Serial.begin(9600);
Serial.println("Setup done");
}


void loop() {

r = analogRead(Z);
g = analogRead(Y);
b = analogRead(X);

xmin = min(xmin, b);
ymin = min(ymin, g);
zmin = min(zmin, r);

xmax = max(xmax, b);
ymax = max(ymax, g);
zmax = max(zmax, r);
/*
Serial.print("xmin: ");
Serial.print(xmin);
Serial.print(", xmax: ");
Serial.print(xmax);
Serial.print(" ymin: ");
Serial.print(ymin);
Serial.print(", zmax: ");
Serial.println(zmax);
*/


r = map(r, zmin, zmax, 0, 255);
g = map(g, ymin, ymax, 0, 255);
b = map(b, xmin, xmax, 0, 255);

rgb(r, g, b);
/*
Serial.print(r);
Serial.print(", ");
Serial.print(g);
Serial.print(", ");
Serial.println(b);
*/

}


1 comment:

Dan said...

Neat! The color's change as you zoom your "space ship" through the room. I hope you let Kris play with it for awhile.