I am trying to control stepper motor from my arduino mega, controlling through PWM based motor controller(step & direction) and simultaneously obtaining position data over serial seeing in serial monitor. I am using Accelstepper library and serial library. My bug when i am setting baud rate to 115200 the motor runs fine and it gives correct position data but with baudrate less than 115200 (like 9600, 38400 etc) motor slows down a lot almost zero. My application requires motor to move in 0.1deg steps resolution which my driver give but don't know about Accelstepper library. Don't have so much idea on controller timing. Can any one please help me fixing stepper speed constant irrespective of any baud rate. My code is here:
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(1, 9, 10);
int pos = 400;
int Enable = 11;
char halt;
void setup()
{
Serial.begin(38400);
stepper.setMaxSpeed(400);
stepper.setAcceleration(50);
stepper.setMinPulseWidth(1);
pinMode(Enable, OUTPUT);
}
void loop()
{
stepper.currentPosition(); // this function retuns current stepper position over serial.println()...written in accelstepper library
digitalWrite(Enable, LOW); // Enabling stepper
if (stepper.distanceToGo() == 0)
{
pos = -pos;
stepper.moveTo(pos);
}
stepper.run();
}
Thanks in advance. Any help is appreciated!!!
Best,
Satya
Hi Graynomad,
Thanks for the reply. But my understanding is the changing baud rate should not affect my motor speed. I want to serial Print my stepper position at every step increment. So i am calling stepper.currentPosition() in my main loop.
Can you please help me modifying my program, running stepper motor at same speed and communicate over serial to PC irrespective of any baud rate. It would be great if someone suggest me some material.
Hello Nick,
The function stepper.currentPosition(); returns the current position and am able to see over serial monitor. I am not discarding that information but the main problem is it slows down the motor speed at 9600 to 56700 baud rate but works fine at 115200 baud rate.
I want a constant stepper speed irrespective of any baud rate and don't want my serial write function to affect my main loop timing. Any idea modifying my program?
I got Accelstepper.h from this link: AccelStepper: AccelStepper.h Source File
So you have embedded code we can't see, I still think my first post might be close to the mark. You are calling serial.print() every loop(), that's maybe 100,000 times a second or more, how is the poor serial port going to keep up? And why do you need to know this information so often?
Graynomad, thank you so much for a quick reply but you have not fully understood my problem. Higher baud rate (115200) is not my problem..Lower baud rate is my problem, motor slows down at lower baud rate and I don't want that.
I have not modified the standard library apart from Serial.println(_currentPos); . Don't dout i have embedded code and hidden you.
Can you suggest me how can I modify my program to not slow down the motor at any baud rate?
Where should I write serial.print() function?
You appear to be calling serial.print() tens of 1000s of times a second (every time loop() is executed), the poor serial port is overwhelmed. Do you get that?
I have not modified the standard library apart from Serial.println(_currentPos);
That's may seem like a small one-line change but it has a lot of ramifications.
Try this
void loop() {
if ((millis() % 1000) == 0) stepper.currentPosition(); // this function retuns current stepper position over serial.println()...written in accelstepper library
That should only print the position every second (it may miss a beat every now and then). I don't know if that's the problem but it can't do any harm.
satyabrata:
Higher baud rate (115200) is not my problem..Lower baud rate is my problem, motor slows down at lower baud rate and I don't want that.
Your call to Serial.println is going to cause a delay. The library can handle (buffer) around 32 bytes, from memory, after which it blocks (waits). At high baud rates it can clear the buffer without blocking. At low baud rates it won't. Your observed behaviour is totally explained by that.