I am using the AccelStepper library to control stepper motors (AccelStepper is built into MeOrion Library), and was wondering how I can use the millis function to wait an interval of time. I have tried to follow examples on this forum, and have come up with the following code. The problem is that once I run this code, it waits the interval initially, then runs continuously, without regard for the distance it was told to move. If I add the previous= current part, the motors don't move at all. I was wondering what needs to be done to get the motors moving properly. Thanks.
//Testing motor control with millis() function
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <MeOrion.h>
MeStepper stepper_1(1);
unsigned long previous= 0;
unsigned long interval=10000;
void setup() {
// put your setup code here, to run once:
stepper_1.setMaxSpeed(100);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long current = millis();
if( current-previous>= interval)
{
stepper_1.move(100);
stepper_1.setSpeed(100);
stepper_1.runSpeedToPosition();
previous=current;
}
}
yousimxing:
I have tried to follow examples on this forum, and have come up with the following code. The problem is that once I run this code, it waits the interval initially, then runs continuously, without regard for the distance it was told to move.
I suspect the problem is that you are not allowing for the time taken for the motor to move.
Try changing this line
previous=current;
to
previous = millis();
so that the timing is measured from when the motor stops