Hello, I am very new to Arduino. I am doing a project where I am moving two stepper motors NOT simultaneously (they take turns), where each moves a fixed distance, stops, and moves forward again until a max distance is reached, and then returns to the original position. I want to move at a constant speed, but I need to use Accelstepper library for a non-blocking movement, so that it can check for a limit switch to be pressed during the movement distance.
However, when I upload the script to the arduino, the motor will not move. It sounds like it’s buzzing, but nothing is happening. I’ve tried to change the script around, but the problem persists. And the motors do have enough power to move because another script I have moves the motors fine. Is there anything I’m doing wrong with the code I’ve written (I’ve taken the limit switch code and one motor out for simplicity)? And also, is it feasible to use the built in Stepper.h library instead to check for the switches during movement?
#include <Accelstepper.h>
int steps = 10000;
AccelStepper stepper1(1,3,4);
int X = 0;
int Xmax = steps*5;
void setup() {}
void loop() {
while (X < Xmax)
{
stepper1.move(steps);
stepper1.setSpeed(1000);
while (stepper1.distanceToGo() != 0)
{
stepper1.runSpeedToPosition();
}
delay(2000);
X = X + steps;
}
stepper1.move(-X);
stepper1.setSpeed(1000);
while (stepper1.distanceToGo() != 0)
{
stepper1.runSpeedToPosition();
}
X = 0;
}
The motors have been running using a different script based on the built in Stepper Library, which is why I believe it's a programming issue. Can the power demand change using a different library?
And also, would it be feasible to use the Stepper library or the Basic code you wrote for checking for limit switches?
Thank you for the code sample, I’ll try it when I get a chance. However, if I add a second motor, say, stepper2, and I want it to do the same thing as stepper1, but not simultaneously, where it only turns after stepper1 is finished, wouldn’t a loop like that force them to move at the same time?
mbr29:
Thank you for the code sample, I'll try it when I get a chance. However, if I add a second motor, say, stepper2, and I want it to do the same thing as stepper1, but not simultaneously, where it only turns after stepper1 is finished, wouldn't a loop like that force them to move at the same time?
First off, the purpose of my code was ONLY to ensure that you can get one motor to work with code that has some resemblance to your own code.
Second, the easiest way to answer many Arduino questions is to try something and see what happens. The Arduino system is great for learning by doing.
Third, there is a mistake in my code. I had (incorrectly) thought that runSpeedToPosition() would complete its move before moving on to the next line of code - i.e. block the Arduino until it finishes. However I don't think it does. The blocking functions are runToPosition() and runToNewPosition() and both of them use acceleration. Consequently you have two choices {A} replace runSpeedToPosition() with runToPosition() in the code in Reply #3 - in which case you will get the sequential behaviour that you want. OR {B} you need to use code something like this
char stepper1State = 'W'; // Paused Waiting Running Done
char stepper2State = 'P';
void loop() {
if (stepper1State == 'W') {
stepper1State = 'R';
stepper1.move(steps);
stepper1.setSpeed(1000);
}
else {
if (stepper1.distanceToGo() == 0) {
stepper1State = 'D';
stepper2State = 'W';
delay(2000); // if you want a delay between movements
}
}
if (stepper2State == 'W') {
stepper2State = 'R';
stepper1.move(steps);
stepper1.setSpeed(1000);
}
else {
if (stepper1.distanceToGo() == 0)
stepper2State = 'D';
stepper1State = 'W'; // if you want the sequence to repeat
delay(2000); // if you want a delay between movements
}
stepper1.runSpeedToPosition();
stepper2.runSpeedToPosition();
}
Normally you'd call stepper1.run() and stepper2.run() every time round loop, and
then call move() or moveTo() whenever you decide to start a movement, let loop() do the work,
no delay()'s, no whiles.