Potentiometer Stablization algo, any/all feedback welcome

Status
Not open for further replies.

ericneilj

Member
Hey y'all, below is an input stabilization segment that (for me) works quite well! Would greatly appreciate any optimization suggestions. It uses a combination of a value threshold and elapsedMillis

Code:
/*
Potentiometer Stabilization

Removes signal noise from potentiomer
 */
 elapsedMillis theTimer;

int sensorPin = A0;    // analog input pin
int sensorValue = 0;
int storedValue = 0;
float outputValue = 0.0;
int valueThresh = 8; // potentiometer movement threshold needed to trigger theTimer
int timeThresh = 200; // window of time (ms) raw potentiometer value is passed to outputValue

void setup() {
  theTimer = 0;
  Serial.begin(38400);
  sensorValue = analogRead(sensorPin);
  storedValue = sensorValue;
}

void loop() {
  
  sensorValue = analogRead(sensorPin);
  
  if (sensorValue >= storedValue + valueThresh || sensorValue <= storedValue - valueThresh) {
    theTimer = 0;
    storedValue = sensorValue;
  }

  if (theTimer < timeThresh) {
    outputValue = sensorValue;
  }
  Serial.println(outputValue);
  delay(10);
}
 
Status
Not open for further replies.
Back
Top