1

having strange behavior with motor shield and steppers. when i'm trying to make onestep, motor just makes some high-frequent noise, but when i add Serial.println for debugging, it moves as intended.

what is going on here?

here is an example:

#include AFMotor.h //dunno how to put angle brackets here

AF_Stepper lrM(48, 1);

void rightStep() {  
  lrM.onestep(BACKWARD, INTERLEAVE);
}
void leftStep() {  
  lrM.onestep(FORWARD, INTERLEAVE);
}

void setup() {
  Serial.begin(9600);
  Serial.println("ready");
}

void loop() {
  rightStep();
//Serial.println("right");
}
1

My guess is Serial.println adds additional delay so stepper motor can react. Without it changes are way too fast (= high frequency noise) to do whole step.

Try to add delay(1); or more into the main loop.

  • i thought that if onestep function is blocking, it would be ok, but it appeared that i need delay(10) so that motor can run. thank you! – curly brace Jul 14 '16 at 19:05
  • Method step contains delays, but there are no delays in onestep (at least not obvious ones). – KIIV Jul 14 '16 at 19:09
0

When you have a Serial print statement running really quickly the output buffer will fill up and the print command will have to wait for there to be room; That causes the command to take a substantial amount of time to complete.

When the statement is missing, you loop probably runs a few hundred times faster than without it (depending on how long onestep takes), probably fast enough that the physical motor can not keep up.

Try adding a delay of a few milliseconds or some kind of rate limiting to the program and then see how much of a difference serial.println makes.

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.