Stepper motor stop ?

hi,
I got a project for school which consists in realising arm, I’m working on the stepper motor control but I’m stuck. (i’m using the Stepper.h library) Actually what I want to do is to make x steps and to stop my motor, but cause of the loop, I don’t have any idea about the way to stop my motor and to make like a reset ? I tried to do it with a “for” function but I’m not at ease with it. Also I don’t really know how to read the ammount of steps I’ve done. Sorry for my bad english, I’m french :sweat_smile: .
If you can’t answer directly but you know where I could do my researches, I’ll take it.
Thanks you !

Moteur_PAP_boucle.ino (394 Bytes)

It is much easier for people to help if you include short programs in your Post - like this (See How to use the Forum )

#include<Stepper.h>
const int stepsPerRevolution = 400;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);// configuration brochagemoteur

void setup() {
    myStepper.setSpeed(50);// configuration de la vitesseà10 rpmne pas dépasser 250
    Serial.begin(9600); // initialisationdu moniteursérie
}

void loop() {
    for (stepCount>0; stepCount--; ){
        myStepper.step(stepCount);
    }
}

Also, indenting the code makes it much easier to read - use the AutoFormat tool.

To get the motor to move a certain number of steps and then stop you just need code like this in loop()

if (stepperHasRun == false) {
   myStepper.step(stepCount);
   stepperHasRun = true;
}

Define stepperHasRun as boolean at the top of the program and set it to false.

...R

Your for-loop looks strange to me. I like to use: for( i = start_value,; i < stop_value; step). It could be: for( int i = 0; i < step_count; i++){

Railroader:
Your for-loop looks strange to me.

I agree. But in this case I don't think there is any need for a FOR loop.

...R

@Robin2
I agree. Using stepper libraries, having those ready made functions accepting a specified number of steps, that’s easy.
I use my selfmade stepper functions that use for-loop.

Normally you don't have to stop the motor, you just tell the library "take x number of steps" and it'll do it for you. The Stepper library afaik will simply block (not return) until the steps are done.

The AccelStepper library allows you to run the stepping "in the background" - by telling it to what step position to go, and calling it frequently. It has a function to read back where it currently is as well.

@wvmarle
My I burst in and ask a question?
Interesting do let the stepping take place as a background task. Suppose I order a long stepping distance. How do I know when the stepper has reached the target and stopped? Else I migth order other actions, other steppers, in my little CNC to start too early.

The AccelStepper::isRunning() function tells you whether the stepper is running (not reached the point you told it to go to yet); the AccelStepper::currentPosition() function tells you at which step it is right now.

Complete class reference here.

It's far more advanced than the stepper library, which I looked at and rather quickly put aside for being 100% blocking. No use for anything but maybe a teaching demo.

Thanks.