here is a video of the project, I have two RGB LEDs reading accelerometer values of XYZ.
Here is a video of the flickering that happens in certain positions. These are major Hue shifts not just jumpy readings from a sensitive accelerometer.http://vimeo.com/4400183
here is the code that I am running.
#define AVARAGE_READINGS 10
int Rled = 9;
int Gled = 10;
int Bled = 11;
int Xpin = 0;
int Ypin = 1;
int Zpin = 2;
int Rled2 = 3;
int Gled2 = 5;
int Bled2 = 6;
void setup()
{
Serial.begin(9600);
pinMode(Rled, OUTPUT);
pinMode(Gled, OUTPUT);
pinMode(Bled, OUTPUT);
pinMode(Rled2, OUTPUT);
pinMode(Gled2, OUTPUT);
pinMode(Bled2, OUTPUT);
}
void loop()
{
int x = analogRead(Xpin);
int y = analogRead(Ypin);
int z = analogRead(Zpin);
for (byte i=0; i<AVARAGE_READINGS; i++){
x += analogRead(Xpin);
x /= 2;
y += analogRead(Ypin);
y /= 2;
z += analogRead(Zpin);
z /= 2;
}
x = map(x, 268, 405, 0, 255);
y = map(y, 270, 420, 0, 255);
z = map(z, 354, 450, 0, 255);
Serial.print("X ");
Serial.print(x);
Serial.print(" Y ");
Serial.print(y);
Serial.print(" Z");
Serial.println(z);
analogWrite(Rled, x);
analogWrite(Gled, y);
analogWrite(Bled, z);
analogWrite(Rled2, x);
analogWrite(Gled2, y);
analogWrite(Bled2, z);
}
You are not doing the averaging correctly. What you are doing is a running average. This means that that last sample taken amounts for 50% of the total reading.
Take all the readings and add them up first, then divide by the number of samples you have taken.
As the analogue read is only 10 bits you will not overflow the value of the int until AVERAGE_READINGS is greater than 32. If you want more than this just make the readings a long int.