Trouble with an RGB LED and accelorometer

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.

1 4 23 15 26 47 23 34 4 

Running Average: 17,75
Regular Average: 19,6

Running Average Pros:

  • Resource friendly
  • Easy to understand (no arrays==easier for beginners)

Running Average Cons:

  • Does not provide a ‘real’ average.
  • Does not maintain a ‘history’ or a current value. [not this code that is]

I doubt this being the source of the error/flicker.

[edit]
You could try this:
[UNTESTED CODE]

#define AVERAGE_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;

int xValues[AVERAGE_READINGS] = {0};
int yValues[AVERAGE_READINGS] = {0};
int zValues[AVERAGE_READINGS] = {0};

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()
{
for (byte i=0; i<AVERAGE_READINGS; i++){
xValues = analogRead(Xpin);
_ yValues = analogRead(Ypin);_
_ zValues = analogRead(Zpin);
* }_
int x = average(xValues,AVERAGE_READINGS);
int y = average(yValues,AVERAGE_READINGS);
int z = average(zValues,AVERAGE_READINGS);
_ 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);
}
//average an array*
unsigned int average(int values[],byte length){
* unsigned int average = 0;
for (byte i=0; i<length; i++){
average += values;
}
average /= length;
return average;
}
[/quote]
[/edit]*_

Too complex you don’t need arrays just do:-

{
 xValue =  yValue =  zValue = 0;
 for (byte i=0; i<AVERAGE_READINGS; i++){
   xValue += analogRead(Xpin);
   yValue  += analogRead(Ypin);
   zValues += analogRead(Zpin);
 }

xValue = xValue /AVERAGE_READINGS;
   yValue  = yValue /AVERAGE_READINGS;
   zValues = zValue /AVERAGE_READINGS;

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.

I can’t believe I did not think of that.

Thank you Mike.

Yet again I need a stamp on my forehead that says KISS :-[