Scaling analog readings to stable values

Hello,

I am trying to scale my analog readings and I get into a problem of ‘unstable’ values.
My goal is to scale 0-1023 to 50-250. I am using the map function – map(A0, 0, 1023, 50, 250). I have set up a display to check the values.

The problem is that sometimes I run into ‘middle’ values, where the display is flickering between two – for example 69 and 70.
How would you approach this topic? Some DSP low-pass filtering?

Thanks for any help!

Post your code using the "#" tool button and we can talk about it.

int value;

void setup() {
}

void loop() {
  value = map(analogRead(0), 0, 1023, 50, 250);
  show_me_on_display(value);
}

there is nothing else then what i've already described

mrkva: Hello,

I am trying to scale my analog readings and I get into a problem of 'unstable' values. My goal is to scale 0-1023 to 50-250. I am using the map function – map(A0, 0, 1023, 50, 250). I have set up a display to check the values.

The problem is that sometimes I run into 'middle' values, where the display is flickering between two – for example 69 and 70. How would you approach this topic? Some DSP low-pass filtering?

Thanks for any help!

Usual first method is to try to take multipule samples and divide by the number of samples taken, thus getting a average value. That acts like a first order low pass filter. Start with maybe 8 samples, divide by 8 then perform your map function. If that settles down the value you can then try with just 4 samples, etc.

Lefty

You might try a dead band setup like in the below code.

//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor

#include <Servo.h> 
Servo myservoS1;
Servo myservoS2;

int potpinS1 = 0;  //analog input pin A0
int potpinS2 = 1;

int newvalS1, oldvalS1;
int newvalS2, oldvalS2;

void setup() 
{
  Serial.begin(9600);  
  myservoS1.attach(2);  
  myservoS2.attach(3);
  Serial.println("testing dual pot servo");  
}

void loop() 
{ 
  newvalS1 = analogRead(potpinS1);           
  newvalS1 = map(newvalS1, 0, 1023, 0, 179); 
  if (newvalS1 < (oldvalS1-2) || newvalS1 > (oldvalS1+2)){  
    myservoS1.write(newvalS1);
    Serial.print("1- ");
    Serial.println(newvalS1);
    oldvalS1=newvalS1;
  }

  newvalS2 = analogRead(potpinS2);
  newvalS2 = map(newvalS2, 0, 1023, 0, 179);
  if (newvalS2 < (oldvalS2-2) || newvalS2 > (oldvalS2+2)){  
    myservoS2.write(newvalS2);
    Serial.print("2- ");    
    Serial.println(newvalS2);
    oldvalS2=newvalS2;
  }
  delay(50); //slow down looping to better read serial monitor 
}

I would use a single stage butter worth filter on the data I assume you have a known sample time interval, so just google code samples in C for simple butterworth filters utilities that to limit the effects of random results etc. Alternatively use a moving average with dead band on the raw data this however will not exclude random read results that will occur due to extenrnal influences.

So the ideal is a filter first followed by a moving average IMO.

Happy coding // 8) 8)