How to round or otherwise buffer potentiometer reading flux ?
the following sketch almost works for me
I'm trying to display new potentiometer setting whilst it is being adjusted , then revert to displaying (count) value set elsewhere.
// serial.print potentiometer setting for short period when it is adjusted
// else display count value set elsewhere in loop
// PROBLEM HOW TO
// round off or otherwise smooth out noise on potentiometer
// mabe a better quality pot is my real problem ?
int count = 0; // used in main loop for default display of Serial.print(servocount)
int sensorPin = A0; // select the input pin for the potentiometer
int pot = 0; // variable to store the value current from potentiometer
int lastpot=0; // last potentiometer reading to compair with
void setup() {
Serial.begin(9600);// send serial to display
count=99;
}
void loop() {
// read the value from potentiometer and round off:
pot = analogRead(sensorPin); // need better way to
pot = pot/10; // need better way to
pot = pot*10; // need better way to
//do{ // alternative attempt
if(pot != lastpot) {
// if ((pot+20)>lastpot || (pot+20)<lastpot) { /// alternative attempt
Serial.print (pot);
Serial.println (" = pot delay setting");
delay(300); // display new potentiometer setting for short period
(lastpot = pot); // set value before next comapare (pot != lastpot)
}
//}while(pot != lastpot ); // alternative attempt
// servo control follows including new delay
delay(pot*5); // new value affecting servo delay
Serial.println (count);// display count that was breifly displaced by delay adjustment
}
Easiest way is to do averaging, make 5,10 or 20 readings and average them.
unsigned long pot = 0; // must be long to prevent overflow!
...
for(int i=0; i< 20; i++) pot += analogRead(sensorPin); // means pot = pot + ....
pot /= 20;
A moving average is another option, check playground for moving average class.
An often used smoothing technique is the following:
unsigned long pot = 0; // must be long to prevent overflow!
...
pot = (analogRead(sensorPin) + 5 * pot)/6; // new value only counts for 16%
or
pot = (analogRead(sensorPin) * 2 + pot) / 3; // new value counts for 66%
in general
pot = (analogRead(pin) * alpha + pot * beta) / (alpha + beta); // a & b are adjustable weights.
alpha > beta means more noise, but also more sensitive to changes.
alphs < beta means less noise, value adapts slower
Then there is the map() function which can be used if you want to scale the value down, also a sort of noise removal.
pot = map(analogRead(pin), 0,1023, 0,50); // pot will be between 0..50
And of course with the use of lastPot you can determine what counts as a change
pot = analogRead(sensorPin);
if (abs(lastpot - pot) < threshold) pot = lastpot;
else lastpot = pot;
Hopes one of these helps ...
great help! thanks
here's what I came up with:
// serial.print potentiometer setting for short period when it is adjusted
// else display count value set elsewhere in loop
// smooth out noise on potentiometer with map()
int count = 0; // used in main loop for default display of Serial.print(servocount)
int sensorPin = A0; // select the input pin for the potentiometer
int pot = 0; // variable to store the value current from potentiometer
int lastpot=0; // last potentiometer reading to compair with
void setup() {
Serial.begin(9600);// send serial to display
count=99;
}
void loop() {
// read the value from potentiometer scale adjustment from 1 to 10
pot = map(analogRead(sensorPin), 0,1023, 1,10); // pot will be between 0..50
// avoid 0 to insure delay is > 0
if(pot != lastpot) {
Serial.print (pot);
Serial.println (" = pot delay setting");
delay(300); // display new potentiometer setting for short period
(lastpot = pot); // set value before next comapare (pot != lastpot)
}
// servo control follows including new delay
// adjust pot multiplier to "scale" actual servo timing as appropriate
delay(pot*200);
Serial.println (count);// display count that was breifly displaced by delay adjustment
}
pot = map(analogRead(sensorPin), 0,1023, 1,10); // pot will be between 0..50
now just take care that the comments reflects the code, hint 10 <> 50 