Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Potentiometer Stablization algo, any/all feedback welcome

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    9

    Potentiometer Stablization algo, any/all feedback welcome

    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);
    }

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,929
    Have you looked at the ResponsiveAnalogRead library?

    https://github.com/dxinteractive/ResponsiveAnalogRead

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •