简体   繁体   中英

Arduino Uno Stepper Motor Issues

I am working with a stepper motor hooked up to pins 9, 10, 11, and 12 on an Arduino Uno. In order to rotate the stepper motor, I wrote a helper method. This particular stepper motor rotates 1.8 degrees per step. The method is:

void rotateStepperBy(float deg) {
  int steps = deg / 1.8;
  motor.step(steps);
}

The method works fine for small degree measures, but behaves in unexpected ways (under rotating, rotating back and forth) if I give it larger degree measures like 45 and 90. Here's an example I was trying:

#include <Stepper.h>

Stepper motor(200, 9, 10, 11, 12);

void setup() {
  rotateStepperBy(360);
}

void loop() {
  rotateStepperBy(90);
  delay(10);
}

void rotateStepperBy(float deg) {
  int steps = deg / 1.8;
  motor.step(steps);
}

Does motor.step complete and then the rest of the program resumes or does there need to be a longer delay for bigger degree measurements to allow the motor to finish stepping?

Does motor.step complete and then the rest of the program resumes..

Yes, motor.step() is a blocking function :

This function is blocking; that is, it will wait until the motor has finished moving to pass control to the next line in your sketch.

But you probably have to set the speed in setup() , for example motor.setSpeed(30); .

Looking at the code for stepper it looks like step_delay stays undefined (or zero) until setSpeed() is called (ie it doesn't get a default value in the constructor..)

unsigned long step_delay; // delay between steps, in ms, based on speed

This value only changes in setSpeed() :

/*
 * Sets the speed in revs per minute
 */
void Stepper::setSpeed(long whatSpeed)
{
  this->step_delay = 60L * 1000L * 1000L / this->number_of_steps / whatSpeed;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM