Millis() and delay() replacment

Hi,

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.

I was wondering what needs to be done to get the motors moving properly.

Step 1: Actually post your code.

Sorry, Completely forgot!

//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;
}
}

Add a Serial.print() statement to the body of the if statement. Does it print once every 10 seconds? Or does it print more often than that?

Don’t forget the Serial.begin() call in setup().

MeStepper stepper_1(1);

And put the stepper on a different pin if you are going to put in some Serial prints like @PaulS suggested.

In general you should stay off of pins 0 and 1 unless you are completely out of other pins as those are the Serial pins.

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

...R