Stepper overshoot - deceleration issue?

I have a UNO running a stepper with a belt drive pulley. (earlier related thread - AccelStepper homing with Hall Sensor full rotation - #6 by ggcarmichael). It uses a Hall Effect sensor to determine its homing position. Once the homing position is determined, the stepper is instructed to execute a number of steps to get to that position. I've noticed that the stepper seems to be overshooting its target. I've read that accelstepper can handle acceleration/deceleration so I thought I might try to implement it instead of the current method but I am still trying to understand accelstepper and how it works. The current move x number of steps in my code is performed here:

 digitalWrite(dirPin,LOW); //Changes the rotations direction
  // Moves HomeMove number of steps
  for(int x = 0; x < HomeMove; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(micro_sec_delay);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(micro_sec_delay);

I've tried to defined Acellstepper's parameters and code to execute a move but the process doesn't move the stepper and appear to lock to program.

1.Can I execute the stepper.run in the setup or does it have to be in the loop?
2. Can I run traditional stepper and accelstepper in the same program?

Full code is here:

// testing a stepper motor with a Pololu A4988 driver board or equivalent
// on an Uno the onboard led will flash with each step
// this version uses delay() to manage timing

// Include the AccelStepper library:
#include <AccelStepper.h> 
#include <MultiStepper.h>

byte directionPin = 2;
byte stepPin = 3;
int numberOfSteps = 1120;
byte ledPin = 13;

int micro_sec_delay = 500; // micro sec between steps - or try 1000 for slower steps


#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
#define Hall_Sensor_D A5    //A5 Hall Sensor digital output



// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
  
void setup() { 

  stepper.setMaxSpeed(1000);
  stepper.setSpeed(400);
  stepper.setAcceleration(150);
  stepper.moveTo(5000);	

  Serial.begin(115200);
  Serial.println("Starting StepperTest");
  int Val1=0,Val2=0, Previous_Val2=0, Mark1=0, Mark2=0, Center=0, Homing=0, HomeMove=0, NewMark1=0;
  digitalWrite(ledPin, LOW);
  
  delay(2000);

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(Hall_Sensor_D,INPUT);
  
 
  digitalWrite(directionPin, HIGH);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(micro_sec_delay);
    Serial.print(n);
    Serial.print(",");
    Val2=digitalRead(Hall_Sensor_D);    //We read both values and display them raw on the serial monitor
    Serial.println(Val2);
    if (Previous_Val2>Val2){
     Mark1=n;
   }
   else if (Previous_Val2<Val2){
     Mark2=n;
   }

   Previous_Val2=Val2;

    digitalWrite(stepPin, LOW);
    
    delayMicroseconds(micro_sec_delay);

  }
   Serial.print("Mark1:");
   Serial.print(Mark1);
   Serial.print(",Mark2:");
   Serial.println(Mark2);

   if (Mark2<Mark1){
     NewMark1=1119-Mark1;
       Center=(NewMark1+Mark2)/2;
       if ((Mark2-Center)<0){
        Homing=Mark1+Center;
        }
         else{
           Homing=Mark2-Center;
         }
       }
   else{
     Center=(((Mark2-Mark1)/2)+0);
     Homing=(Mark1+Center);
   }
  
   HomeMove=1119-Homing;

   Serial.print("Center:");
   Serial.print(Center);
   Serial.print(", Homing:");
   Serial.print(Homing);
   Serial.print(", HomeMove:");
   Serial.println(HomeMove);
  
 //digitalWrite(dirPin,LOW); //Changes the rotations direction
  // Moves HomeMove number of steps
  //for(int x = 0; x < HomeMove; x++) {
  //digitalWrite(stepPin,HIGH);
  //delayMicroseconds(micro_sec_delay);
  //digitalWrite(stepPin,LOW);
  //delayMicroseconds(micro_sec_delay);

stepper.move(-HomeMove);
stepper.run();

  }
//}

void loop() { 
  

}

Mixing AccelStepper with own stepping functions is a very bad idea.

Please run the autoformat in the IDE and update the code.
Isn't AccelStepper.run supposed to be kicked over and over again? I might be wrong...
Stepping by Dir and Step will not be noticed by Accelstepper and goto a position will go wrong.

Thank you for the auto format advice (see reformatted code below).

In all the AccelStepper examples I have reviewed, the action is done in the loop.

I believe the #define dirPin and stepPin help to define the stepper motor. I use the directionPin and stepPin for the standard stepper function which locates the homing position.

// testing a stepper motor with a Pololu A4988 driver board or equivalent
// on an Uno the onboard led will flash with each step
// this version uses delay() to manage timing

// Include the AccelStepper library:
#include <AccelStepper.h>
#include <MultiStepper.h>

byte directionPin = 2;
byte stepPin = 3;
int numberOfSteps = 1120;
byte ledPin = 13;

int micro_sec_delay = 500;  // micro sec between steps - or try 1000 for slower steps


#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
#define Hall_Sensor_D A5  //A5 Hall Sensor digital output



// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {

  stepper.setMaxSpeed(1000);
  stepper.setSpeed(400);
  stepper.setAcceleration(150);
  stepper.moveTo(5000);

  Serial.begin(115200);
  Serial.println("Starting StepperTest");
  int Val1 = 0, Val2 = 0, Previous_Val2 = 0, Mark1 = 0, Mark2 = 0, Center = 0, Homing = 0, HomeMove = 0, NewMark1 = 0;
  digitalWrite(ledPin, LOW);

  delay(2000);

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(Hall_Sensor_D, INPUT);


  digitalWrite(directionPin, HIGH);
  for (int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(micro_sec_delay);
    Serial.print(n);
    Serial.print(",");
    Val2 = digitalRead(Hall_Sensor_D);  //We read both values and display them raw on the serial monitor
    Serial.println(Val2);
    if (Previous_Val2 > Val2) {
      Mark1 = n;
    } else if (Previous_Val2 < Val2) {
      Mark2 = n;
    }

    Previous_Val2 = Val2;

    digitalWrite(stepPin, LOW);

    delayMicroseconds(micro_sec_delay);
  }
  Serial.print("Mark1:");
  Serial.print(Mark1);
  Serial.print(",Mark2:");
  Serial.println(Mark2);

  if (Mark2 < Mark1) {
    NewMark1 = 1119 - Mark1;
    Center = (NewMark1 + Mark2) / 2;
    if ((Mark2 - Center) < 0) {
      Homing = Mark1 + Center;
    } else {
      Homing = Mark2 - Center;
    }
  } else {
    Center = (((Mark2 - Mark1) / 2) + 0);
    Homing = (Mark1 + Center);
  }

  HomeMove = 1119 - Homing;

  Serial.print("Center:");
  Serial.print(Center);
  Serial.print(", Homing:");
  Serial.print(Homing);
  Serial.print(", HomeMove:");
  Serial.println(HomeMove);

  //digitalWrite(dirPin,LOW); //Changes the rotations direction
  // Moves HomeMove number of steps
  //for(int x = 0; x < HomeMove; x++) {
  //digitalWrite(stepPin,HIGH);
  //delayMicroseconds(micro_sec_delay);
  //digitalWrite(stepPin,LOW);
  //delayMicroseconds(micro_sec_delay);

  stepper.move(-HomeMove);
  stepper.run();
}
//}

void loop() {
}

Thanks!
Much easier to read code eing indented properly.

Some AccelStepper function does the work and then returns. Other functions need regular kicks to fullfill the function. You have nothing done in loop so far.
I've just started using one AS function that is blocking. That's perfect in my application but not in Yours.
You need to check the end switch before every kick of AS.
Hopefully some much more experienced helper will join in.
On a little tablet for the moment but I'll try and see something.

Thank you

A clue that the voltage for the stepper needs to be much higher and the current limit set properly.

You need to consider the mass of whatever the stepper is accelerating/decelerating.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.