Hi,
i am trying to smooth my results from reading the value of a pot. I have been having trouble reading a steady value as the value usually fluctuates + or -1 and so gives flickery results. Is there a way of only reading the input if the value has changed by steps of 10 as opposed to every single number? i have tried taking an average but this still fluctuates, the below code works as i require but it seems a little clunky and reduces my resolution greatly. i eventually need the value to fall between 1000-5000 hence the final multiplication.
Any thoughts or comments would be helpful.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
float faderval;
float fadermap;
float stepup;
void setup()
{
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
faderval = analogRead(A0);
fadermap = map (faderval, 0, 1023, 2, 10);
total= total - readings[index];
readings[index] = fadermap;
total= total + readings[index];
index = index + 1;
if (index >= numReadings)
index = 0;
average = total / numReadings;
stepup = average * 500;
Serial.println(stepup);
delay(1);
}