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]*_