Smoothing AnalogRead

Hi,

I have succesfully implemented smoothing on one analogRead pin with the help of: https://www.arduino.cc/en/Tutorial/Smoothing

Now i want to smooth another analog pin in the same skatch.
Tried like below but it didn't work. I got negative readings.

  if (digitalRead(EnterButton) == 1 or millis() < 6000){
    
    totalL = totalL - readingsL[readIndexL];
    totalA = totalA - readingsA[readIndexA];
  readingsL[readIndexL] = map(analogRead(potPinL), 0, 1023, mapMinSpeed, mapMaxSpeed);
  readingsA[readIndexA] = map(analogRead(potPinA), 0, 1023, mapMinSpeedA, mapMaxSpeedA);
  totalL = totalL + readingsL[readIndexL];
  totalA = totalA + readingsA[readIndexA];
  readIndexL = readIndexL + 1;
  readIndexA = readIndexA + 1;
  if (readIndexL >= numReadingsL) {
    readIndexL = 0;
  }
  
  if (readIndexA >= numReadingsA) {
    readIndexA = 0;
  }
  averageL = totalL / numReadingsL;
  averageA = totalA / numReadingsA;
 
  delay(2);        // delay in between reads for stability
      motorSpeedL = averageL;
      motorSpeedA = averageA; 
 
       int motorSpeedLDisplay = map(motorSpeedL,mapMinSpeed,mapMaxSpeed,0,100);
       int motorSpeedADisplay = map(motorSpeedA, mapMinSpeedA, mapMaxSpeedA,0,100);

    lcd.setCursor(0, 0);
    lcd.print("Links:");
    lcd.print(motorSpeedLDisplay);
    lcd.setCursor(0, 1);
    lcd.print("Rechts:");
    lcd.print(motorSpeedADisplay);

This is just a bit of my skatch. i will upload the whole code in a new replay (too big for one).

Hope that someone can help me with this.

Tnx a lot.
N

Whole program with working smoothing on one analog pin.

//DRV8825 VREF voltage NEMA 17= 0,65V can be 0.8V
  
  
  #include <AccelStepper.h>
  #include <LiquidCrystal.h>
  LiquidCrystal lcd(53, 50, 45, 41, 33, 31);
  
  //_____________________________________________________________
  unsigned long currentMillis;
  unsigned long endFirstRunMillis;
  const long interval = 2000;
  enum motorStateENUM {
    WAITING,
    FIRSTRUN,
    BETWEENRUNS,
    SECONDRUN,
    SECONDRUNDONE,
    THIRDRUN,
    FINISHED,
  };
  motorStateENUM motorState = WAITING;
  
  //_______________________________________________________________
  //LDR
  const int ledPinLDR = 52;
  const int crashPin1 = A1;
  int crashPin1State;
  boolean crashButtonPressed = false;
  
  //ACTUATOR
  AccelStepper stepperA(AccelStepper::DRIVER, 4, 3);     // (DRIVER, Step, Directon)
  int enableA = 2;
  const int homePinA = 26;
  int homePin2StateA;
  int potPinA = A4;
  int motorSpeedA;
  int mapMinSpeedA = 2000;
  int mapMaxSpeedA = 3500;
  
  
  //STARTBUTTON
  const int startButton = 39;
  int startButtonState = 0; //1 = open->not pressed
  
  //STRATBUTTON TOGGLE
  int startButtonReading;           // the current reading from the input pin
  int previouStartButtonReading = 0;    // the previous reading from the input pin
  
  //NEMA 23 (LINEAR MOTION MOTOR)
  AccelStepper stepperL(AccelStepper::DRIVER, 7, 8);     // (DRIVER, Step, Directon)
  int enableL = 9;
  const int homePinL = 34;
  int homePinStateL;
  int potPinL = A0;
  int motorSpeedL;
  int mapMinSpeed = 1500;
  int mapMaxSpeed = 3000;
  boolean directionL;
  
  boolean atStartPosition;
  
  //SERVO
  #include <known_16bit_timers.h>
  #include <Adafruit_TiCoServo.h>
  Adafruit_TiCoServo myservo;
  int servo = 11;
  int pos = 0;
  
  //STABELIZING ANALOG READ
const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int total = 0;                // the running total
//STEPPERL
int readIndexL = 0;              // the index of the current reading
int averageL = 0;                // the average
//STEPPERA
int readIndexA = 0;              // the index of the current reading
int averageA = 0;                // the average


//SPEEDCONTROL
  int EnterLED = 42;
  int EnterButton = 51;
  
  void setup() {//================================================================
    lcd.begin(16, 2);
    lcd.print("STYLEMATHOT");
    lcd.setCursor(0, 1);
    lcd.print("WRAPPER 2000!");
    pixels.begin();
    myservo.attach(servo);
    myservo.write(2);
    delay(500);
    pinMode(EnterLED,OUTPUT);
    pinMode(EnterButton,INPUT);
    pinMode(potPinL, INPUT);
    pinMode(potPinA, INPUT);
    pinMode(ledPinLDR, OUTPUT);
    pinMode(crashPin1, INPUT);
    pinMode(startButton, INPUT_PULLUP);
    pinMode(homePinA, INPUT_PULLUP);
    pinMode(homePinL, INPUT_PULLUP);
    pinMode(enableA, OUTPUT);
    pinMode(enableL, OUTPUT);
    digitalWrite(enableA, HIGH);
    digitalWrite(enableL, HIGH);
    Serial.begin(115200);
    delay(2000);
    
    for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
    
    homePin2StateA = digitalRead(homePinA);
    homePinStateL = digitalRead(homePinL);
    if (homePin2StateA == 0 or homePinStateL == 1) {
      atStartPosition = false;
    }
    else {
      atStartPosition = true;
      for (int i = 0; i < NUMPIXELS; i++) {
  
        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
        pixels.setPixelColor(i, pixels.Color(0, 150, 0)); // Moderately bright green color.
  
        pixels.show(); // This sends the updated pixel color to the hardware.
  
        delay(delayval); // Delay for a period of time (in milliseconds).
      }
      }
         lcd.clear();
  }
  void loop() {//========================================================
    stepperA.setMaxSpeed(motorSpeedA);
    stepperA.setAcceleration(30000);
    
  if (digitalRead(EnterButton) == 1 or millis() < 6000){
    total = total - readings[readIndexL];
  readings[readIndexL] = map(analogRead(potPinL), 0, 1023, mapMinSpeed, mapMaxSpeed);
  total = total + readings[readIndexL];
  readIndexL = readIndexL + 1;
  if (readIndexL >= numReadings) {
    readIndexL = 0;
  }
  
  averageL = total / numReadings;
 // Serial.print(averageL);
//Serial.print("-");
  delay(1);        // delay in between reads for stability
      motorSpeedL = averageL;//map(analogRead(averageL), 0, 1023, mapMinSpeed, mapMaxSpeed);
      motorSpeedA = map(analogRead(potPinA), 0, 1023, mapMinSpeedA, mapMaxSpeedA);
      
      
      
       //Serial.print(map(motorSpeedL,mapMinSpeed,mapMaxSpeed,0,100));
       int motorSpeedLDisplay = map(motorSpeedL,mapMinSpeed,mapMaxSpeed,20,99);
       
       //Serial.print("-");
       //Serial.println(motorSpeedLDisplay);
    
    
    
    lcd.setCursor(0, 0);
    lcd.print("Links:");
    lcd.print(motorSpeedLDisplay);
    lcd.setCursor(0, 1);
    lcd.print("Rechts:");
    lcd.print(motorSpeedA);
    
    
    digitalWrite(EnterLED,HIGH); 
    }
    else 
    digitalWrite(EnterLED,LOW);
    
    startbutton();
  
    crashPin1State = analogRead(crashPin1);
    homePin2StateA = digitalRead(homePinA);
    homePinStateL = digitalRead(homePinL);
    //////////////////////////READ THE LDR/////////////////////////////////////////
    if (crashPin1State <= 25 and homePin2StateA == 1 ) 
    {
      digitalWrite(ledPinLDR, HIGH);
      crashButtonPressed = true;
    }
    else {
      digitalWrite(ledPinLDR, LOW);
    }
  
    ////////////////////////ENABLE OR DISABLE STAPPER-L///////////////
   if (homePinStateL == 0 && startButtonState == 1)
    {
      digitalWrite(enableL, HIGH);
    }
    else {
      digitalWrite(enableL, LOW);
  }
    //////////////////////////STEPPER L DIRECTION/////////////////////////////////////////
  
    if (homePinStateL == 0 and stepperL.currentPosition() == 0)
    {
      directionL = 1;
    }
  
    if (stepperL.currentPosition() == 4920)
    {
      directionL = 0;
    }
  
    //////////////////////////FORWARD MOVE STEPPER L/////////////////////////////////////////
    if (directionL == 1)
    {
      if (startButtonState == 0 && homePin2StateA == 1 && crashButtonPressed == false
          && stepperL.currentPosition() == 0)
      {
        stepperL.setMaxSpeed(motorSpeedL);
        stepperL.setAcceleration(motorSpeedL * 2);
        stepperL.move(2600);
      }
      if (startButtonState == 0 && crashButtonPressed == false
          && stepperL.currentPosition() == 2600 && stepperL.targetPosition() == 2600)
      {
        stepperL.setMaxSpeed(motorSpeedL - 1050);
        stepperL.setAcceleration(motorSpeedL * 2);
        stepperL.setSpeed(motorSpeedL/2);
        stepperL.moveTo(4920);
      }
    }
  
    if (directionL == 0)
    {
      if (homePinStateL == 1) { 
        //homePinStateL = digitalRead(homePinL);
        if (stepperL.currentPosition() == 4300) {
          
          for (pos = 0; pos <= 80; pos += 1) {
          myservo.write(pos);
          }
        }
        if (stepperL.currentPosition() == 200) {
          
          for (pos = 80; pos >= 50; pos -= 1) {
          myservo.write(pos);
          }
        }
        
         if (stepperL.currentPosition() < 500) {
          stepperL.setMaxSpeed(500);
        stepperL.setSpeed(-500);
         stepperL.runSpeed();
        }
        
        if (stepperL.currentPosition() > 201) {
        stepperL.setMaxSpeed(1600);
        stepperL.setSpeed(-1600);
         stepperL.runSpeed();
        }
      }
        
        if (homePinStateL == 0)
        {
          stepperL.setCurrentPosition(0);
          startButtonState = 1;
            for (pos = 50; pos >= 0; pos -= 1) {
          myservo.write(pos);
          delay(15);
            }
        }
    }
  stepperL.run();
  stepperL.runSpeed();
  
      ///////////////////////////////ACTUATOR MOVE//////////////////////////
      
      
///// Here one stepper does a move forward and then backword.


    }

Are you trying to smooth these two readings ?

analogRead(potPinL)
analogRead(potPinA)

If so, this could be a candidate for a hysteresis solution. Look here and the latest code in post #25.

How much instability are you seeing on your analog reading in the range 0 to 1023. Is it +/- 5 Analog units or +/-10 Analog units or what ?.

Thank you this is very useful stuff.
With a lot of switches and buttons sometimes.

Just to be sure there is no hysteresis when digitalRead, right?
Only analog?

tnx for the tip!

On PotPinL 10 units or even more
On PotpinA around 5 units.

Thats why i don't have to average PotPinA. But just map it and it stays steady.
Problem solved.

Tnx

OK. you may be able to smooth potPinL by doing something like this:

#include "HystFilter.h"
. . . 

HystFilter potL( 1024, 32, 10 ) ;  // 10 bit ADC = 1024, 32 discrete output values required, margin = 10 units (of 1024)

. . . 

// and replacing  analogRead(potPinL)  with:

( 32 * potL.getOutputLevel( analogRead( potPinL ) )  )

You have to copy the HystFilter files into the sketch directory as explained in #25 in the link already supplied.

You then should not need the smoothing and can tune the resolution as required.