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"); }
-
1We need Minimal, Complete, and Verifiable exampleKIIV– KIIV07/14/2016 18:30:50Commented Jul 14, 2016 at 18:30
-
ok. i've added one, but it is very common usagecurly brace– curly brace07/14/2016 18:43:19Commented Jul 14, 2016 at 18:43
2 Answers 2
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– curly brace07/14/2016 19:05:09Commented Jul 14, 2016 at 19:05 -
Method step contains delays, but there are no delays in onestep (at least not obvious ones).KIIV– KIIV07/14/2016 19:09:21Commented Jul 14, 2016 at 19:09
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.