Motor has to stop at right position when commanded to stop

Hey,

in my current Project i am using Accelstepper library and everything works fine, yet i want to improve my projekt: but i cannot figure out how its done :slightly_frowning_face:

in my project there is one motor u can command it to start and do x rotations and u can stop it on command.

now i want the motor to stop allways at the exact same position, no matter when u command it to stop.

I tested it with while loops and different stuff but it all doesnt seem to work :confused:

Thats the reason too i cannot put code here because i m not even close to solving it ^^

maybe anyone has allready experience with this method,

any advice is appreciated

thanks RWTH_MASCHI

maybe heres the old code: I think updated it yet it should work even when poorly implemented

#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(1,10,11); 
int counter = 12;
int schalter = 6;
int LED=13;
int i;
long steps;
boolean on = false;
//int schalterState = 0;
void setup()
{  
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(900);
  stepper.setAcceleration(800);
  stepper.moveTo(-5400);

  Serial.begin(9600);
  pinMode(schalter,INPUT);
  pinMode(LED,OUTPUT);
  steps=12*26.85*200;
}
void loop()
{
  
  if (digitalRead(schalter) == HIGH&&stepper.distanceToGo()!=0){
    counter=12;
    on=true;
    digitalWrite(LED,LOW);
     for(i=0; i<counter; i++){
      stepper.moveTo(steps);
      stepper.run();
      
       }
     if(stepper.distanceToGo()==0)digitalWrite(LED,HIGH);
    }
    
    if(digitalRead(schalter)==LOW&&stepper.distanceToGo()==0){
    counter=12;
    stepper.setCurrentPosition(0);  
    stepper.moveTo(steps);
    } 
    else{
      counter=0;
      digitalWrite(LED,HIGH);
    }
   
}

RWTH_MASCHI:
maybe heres the old code: I think updated it yet it should work even when poorly implemented

If that is not the latest (and best) version of your program it seems a waste of time to study it.

In any case you need to tell us exactly what it does and what you want it to do that is different.

Based on my knowledge of the AccelStepper library I cannot make sense of this statement

now i want the motor to stop allways at the exact same position, no matter when u command it to stop

AFAIK the library always stops the motor at the position you tell it to.

If you want to interrupt the movement of a motor before it reaches its destination that can be done, but by definition it won't stop at any particular place.

...R

Hi,
Will the position you want to park the stepper in, be ALWAYS the same?

If so you could use a limit switch to let your controller know when the stepper is in that position.

What model Arduino are you using?

Thanks.. Tom... :slight_smile:

RWTH_MASCHI:
Hey,

in my current Project i am using Accelstepper library and everything works fine, yet i want to improve my projekt: but i cannot figure out how its done :slightly_frowning_face:

in my project there is one motor u can command it to start and do x rotations and u can stop it on command.

now i want the motor to stop allways at the exact same position, no matter when u command it to stop.

Then don't call stop(). That's for emergencies, just tell it where to move to and it will move there and stop.

Or perhaps you mean to stop at a particular angle, doing no more complete turns. In that case read the current position, and tell it to move to the next multiple of 360 degrees (200 steps, or whatever if microstepping).