# understanding looping with millis()

i posted an earlier version of this question in the motors section, but its moved on a little, is more of a programming issue now… and i’m still lost. so hopefully i will be excused for a little repetition.

i have 3 steppers that i want to run for a fixed number of steps, to pause for a fixed period, then run the steps again, and repeat, repeat, repeat. using delays (obviously) got me into all sorts of trouble so i’m following the recommendation to use millis(). i’ve tried reading and adapting code from examples and tutorials but i’m missing something.

in the first example i am getting looping behaviour, but the stepper only advances one step each iteration.
in the second example i’m trying something slightly different and it completes its number of steps once, but then doesn’t do it again.

can someone point out to me the thing/s that i’m missing?

just one step:

``````#include <AccelStepper.h>

AccelStepper stepper1(1, 2, 1);

// each "event" (stepper) gets their own tracking variable
unsigned long previousMillisstep1=0;

// intervals for each stepper
int intervalstep1 = 5000;

void setup()
{

pinMode(3, OUTPUT);       //MS3 - stepper1
digitalWrite(3, LOW);
pinMode(4, OUTPUT);       //MS2 - stepper1
digitalWrite(4, LOW);

pinMode(1, OUTPUT);       //direction
digitalWrite(1, LOW);
pinMode(2, OUTPUT);       //step
digitalWrite(2, HIGH);

stepper1.setMaxSpeed(895);
stepper1.setAcceleration(40);
stepper1.move(1000);

}
void loop() {

unsigned long currentMillis = millis();

if ((unsigned long)(currentMillis - previousMillisstep1) >= intervalstep1) {
digitalWrite(1, LOW);
stepper1.run();
previousMillisstep1 = currentMillis;
}

}
``````

just one movement:

``````#include <AccelStepper.h>

AccelStepper stepper1(1, 2, 1);

unsigned long currentTime;
unsigned long loopTime;

void setup()
{

pinMode(3, OUTPUT);       //MS3 - stepper1
digitalWrite(3, LOW);
pinMode(4, OUTPUT);       //MS2 - stepper1
pinMode(1, OUTPUT);       //direction
digitalWrite(1, LOW);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);

stepper1.setMaxSpeed(895);   //kepler 10-b
stepper1.setAcceleration(40);
stepper1.move(1000);

currentTime = millis();
loopTime = currentTime;

}

void loop()
{
currentTime = millis();
if (currentTime >= (loopTime + 8000))

stepper1.move(1000);
stepper1.run();

loopTime = currentTime;

// Other processing can be done here
}
``````

You just failed to enclose your stepper logic in braces {} in the second example. So it only ran the line after it.

thanks for the response

i enclosed my stepper logic like this (i think i understood you correctly) but now the motor doesn't spin at all...

``````void loop()
{
currentTime = millis();
if (currentTime >= (loopTime + 8000))

{
stepper1.move(1000);
stepper1.run();
}

loopTime = currentTime;

}
``````

You have no time stamp to compare with. Have you looked at the millis() examples? Also you need to move more lines inside the brackets. :)

i have, and i’ve tried to make sense of them… but i don’t think i’m getting it.

ok… so if i now have this, with more lines inside the brackets, where would the timestamp go? we’re talking about an original point of reference, right? i was under the impression that this was provided by this - currentTime = millis();
loopTime = currentTime;

``````#include <AccelStepper.h>

AccelStepper stepper1(1, 2, 1);

unsigned long currentTime;
unsigned long loopTime;

void setup()
{

pinMode(3, OUTPUT);       //MS3 - stepper1
digitalWrite(3, LOW);
pinMode(4, OUTPUT);       //MS2 - stepper1
pinMode(1, OUTPUT);       //direction
digitalWrite(1, LOW);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);

stepper1.setMaxSpeed(895);   //kepler 10-b
stepper1.setAcceleration(40);
stepper1.move(1000);

currentTime = millis();
loopTime = currentTime;

}

void loop()
{
currentTime = millis();
if (currentTime >= (loopTime + 8000))

{
stepper1.move(1000);
stepper1.run();

loopTime = currentTime;
}

}
``````
``````void loop()
{
currentTime = millis();
if (currentTime - loopTime >= 8000)

{
stepper1.move(1000);
stepper1.run();
loopTime = currentTime;
}
}
``````

loopTime is a strange name for a timestamp, but it is functioning as one here.