http://www.youtube.com/watch?v=qobngrJLDNw
/*
* The AnnoyAlatron 2000
*
* Accelorometer controled red, green, blue leds, and piezo buzzer
* ADXL335
*/
#define NOTE_C6 1047
#define NOTE_E6 1319
#define NOTE_G6 1568
int notes[] = {
NOTE_C6, NOTE_E6,NOTE_G6 };
int piezoPin = 8;
int greenPin = 9; // LED connected to digital pin 9
int redPin = 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;
float xg, yg, zg;
void rgb (int r, int g, int b)
{
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
//From the Adafruit forums
float to_g(const int axis_value)
{
const float arduino_power_supply = 5;
const float sensor_power_supply = 3.3;
const float zero_g_bias = sensor_power_supply / 2;
float voltage = axis_value * arduino_power_supply / 1024;
return (voltage - zero_g_bias) * 1000 / 330;
}
void setup()
{
Serial.begin(19200);
Serial.println("Setup done");
tone(piezoPin, notes[0], 200);
tone(piezoPin, notes[1], 200);
tone(piezoPin, notes[2], 200);
}
void loop()
{
r = analogRead(Z);
g = analogRead(Y);
b = analogRead(X);
xg = to_g(b);
yg = to_g(g);
zg = to_g(r);
Serial.print(" X: ");
Serial.print(b);
Serial.print(" xg: ");
Serial.print(xg);
Serial.print(" Y: ");
Serial.print(g);
Serial.print(" yg: ");
Serial.print(yg);
Serial.print(" Z: ");
Serial.print(r);
Serial.print(" zg: ");
Serial.println(zg);
xmin = min(xmin, b);
ymin = min(ymin, g);
zmin = min(zmin, r);
xmax = max(xmax, b);
ymax = max(ymax, g);
zmax = max(zmax, r);
tone(piezoPin, (r + notes[0]), 10);
tone(piezoPin, (g + notes[1]), 10);
tone(piezoPin, (b + notes[2]), 10);
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);
}
No comments:
Post a Comment