0

I'm using Arduino to control a stepper motor. I need it to make one revolution and then stop. I'm new to this and haven't ran it yet. Will this do for me?

#include <Stepper.h>

const int stepsPerRevolution = 200;  

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  myStepper.setSpeed(60);
}

void loop() {
  myStepper.step(stepsPerRevolution);
}
2

No, that will not do what you want. That will continuously rotate the stepper round and round and round.

The clue as to why is in the name of the function you put the myStepper.step call: "loop". That function repeatedly loops around over and over again, so your step function will be called over and over again.

There is one piece of information lacking from your thinking at the moment, and that is when do you want the stepper to rotate? If it's just once when you turn the power on then it's as simple as doing everything (including calling myStepper.step) in setup() and leaving loop() empty. If not, then you will need to add appropriate logic to only run the step function when the required conditions are met.

  • what if I want it to make just 10 revolutions and then stop. – Vic May 27 '17 at 12:51
  • 1
    The same way as for 1 revolution but with 10x the steps? – Majenko May 27 '17 at 13:07
  • Yes, though another option is to put a for loop around the step command. For a few rotations just multiplying the count works, but the count argument appears to be limited to +32767 or -32768 (on an ATmega) – Chris Stratton May 27 '17 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.