Can anyone help me, I am using the accelstepper.h library to, accelerate a stepper motor to a given speed for x number of steps, stop, delay for x seconds and repeat on a continuous cycle. I have got it to accelerate, rotate for x number of steps and stop but cannot get it to delay and repeat. It sounds very simple to do but being new to this library I require some help.
You need to post your code - and in code tags please so it looks like this
. The code button is the scroll with the <> on top of it.
Without your code it is impossible to help.
Also tell us what stepper motor driver board you are using.
...R
The problem is, as i see it, that the accelstepper library presumes a state machine since the call to the stepper.run() function must be as frequent as possible. So i recommend you learning about state machines since this is very valuable knowledge.
As a first step we design a state diagram (attached) As i can see your machine has only 2 states. One where the motor runs and one where we are waiting.
The next step is identifying the conditions that define the transitions.
- The steps reached transition is defined by stepper.distanceToGo() == 0
- The delay time transition is best defined by using the "blink without delay" method from the examples. That is comparing a variable to the millis() function. Of course there are other ways like using a timer library or using my State machine librarywich has built in timing functions
Once this is done and you are convinced that the decstription is consistent with the problem you can start to code. This might look something like this (not tested):
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
int state;//0==running; 1==waiting
unsigned long timer;
const unsigned long delayTime = 1000;//1s wait
const long distance = 2000;//number of steps to move at a time
void setup(){
// Change these to suit your stepper if you want
stepper.setMaxSpeed(100);
stepper.setAcceleration(20);
stepper.move(distance);//start by moving
}//setup()
void loop(){
if(state == 0){//initial state == running
if(stepper.distanceToGo()==0){//steps reached
state = 1;//enter waiting state
timer = millis()+delayTime;
}//if(steps reached)
}else{//state == waiting
if(millis()>=timer){//check if delay time is reached
state = 0;//switch to running state
stepper.move(distance);//initiate move
}//if(delayTime)
}//statecheck
stepper.run();
}//loop()
#include <AccelStepper.h>
int motorSpeed = 4400;
float motorAccel = 11000;
int motorDirPin = 9;
int motorStepPin = 10;
AccelStepper motor(1, motorStepPin, motorDirPin); // pin 9 = step, pin 8 = direction
void setup()
{
}
void loop()
{
motor.setCurrentPosition(0);
motor.setMaxSpeed(motorSpeed);
motor.setAcceleration(motorAccel);
motor.move(8000);
while (motor.run());
motor.run();
}
I posted my code as requested, thank you for the example code, not sure if this will change anything you have said now you can see my code.
While your code is short it is confusing a few things.
You don't need a WHILE loop. The iteration of loop() itself is sufficient for repeated calls to motor.run().
That in turn will illustrate the fact that you should not be setting the position of the motor in every iteration of loop() because then it never gets a chance to finish.
For a start try putting everything except motor.run() into setup() AND do not include the WHILE loop.
Next you need to think about how you should separate the business of setting the motor positions from the business of making the motor move. As @nilton61 has said you need to manage the state of your program - is the motor working through a sequence, or has it finished. The function motor.currentPosition() will tel you where it is and when that is the same as where you wanted it to go you will know it has finished that movement. Then you can set up the next movement.
You may get some value from the Thread planning and implementing a program
...R
Thank you all for the advise, it all sounds logical, I will let you know how I get on.