Using Millis() instead of delay not working

Hey There,

I have a stepper motor that is turning a drive pulley. Attached to the drive pulley is an encoder wheel (wooden wheel with tooth pitch that matches the drive pulley pitch). The wooden wheel rotates in an optical encoder/photo eye. The program you see below is made to make the motor spin until it has reached every other tooth face (doesn’t pause motor when signal is HIGH but pauses when signal is LOW after being HIGH) . The program works perfectly right now, however, the delay that you see near the bottom of the code must be removed and replaced with a millis function. Reason being is that there are other segments of code that need to be added that must run simultaneously, and the delay will interfere. All the ways I have tried using the millis in place of that delay have not worked and the motor has continuously spun without the pause I seek.

In short, I need to replace delay with millis in order to temporarily pause motor function.
Please help! Thanks


#include <Stepper.h>
const int stepsPerRev = 10;
int encoderIn = 44;
int statusLED = 13;
int DetectState = 0;
int LastDetectState = 0;
//unsigned long previousMillis = 0;
const long interval = 2000;
const int period = 1500;
unsigned long previousmillis = 0;
unsigned long currentMillis = 0;

Stepper myStepper(stepsPerRev, 48, 46);

void setup() {
  pinMode(encoderIn, INPUT);
  pinMode(statusLED, OUTPUT);
void loop() {

  currentMillis = millis();
   previousmillis = millis();

DetectState = digitalRead(encoderIn);
digitalWrite (statusLED,DetectState);
// myStepper.step(stepsPerRev);

if (DetectState == HIGH )
 LastDetectState = DetectState;

if (DetectState != LastDetectState)
   LastDetectState = DetectState;

Sorry, I don't understand what is the purpose of the existing delay() so I can't suggest an alternative. Maybe you can explain your requirement more clearly?

In the meantime have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.


Hi there,

Sorry for not being clear enough.

The purpose of the existing delay right now is to pause the motors movement for a half second when the encoder tells it to.

The rest of the code works great but im trying to remove the delay (because it blocks other things) and replace it with a pause using millis.

Hope that clear it up.

Thanks for the reply.

You can set a static flag at the start of loop. Get rid of your current/previous millis assignments.

In your last if, test the flag. If not flag, set the flag and currentMillis = millis, else if millis - currentMillis >= 500 , clear flag and set LastDetectState = DetectState.

Optionally you can include a flag test then in your first if statement if you wish to skip that during your waiting period.

I’ll show you my code after you show me yours.

Pseudocode :

if (DetectState != LastDetectState)
  //save the value of millis() as startTime
  //set a boolean named currentlyTiming to true but only run the stepper code when the boolean is false

//every time through loop()
if (currentlyTiming is true)
  //check whether the period has elased (currentTime - startTime)
  //if not then keep going round loop()
  set currentlyTiming to false