Problem with simple delay loop

I am using stepper motors with EasyDriver boards and to prevent the chip on the board from overheating and using up power when not needed, I want to move the motor and then wait 5 seconds and if nothing else happens, put the driver to sleep. I don't want to use the delay command, because I want to be able to send another command to move the motor if needed. Pulling the SleepPin low will shut off the power to my motor.

My code looks like this,

long interval = 5000;
  boolean flag = true;
  unsigned long time1 = millis();    // get current time
  unsigned long time2 = time1;                       
  while (flag == true) {
    time1 = millis(); 
    if (time1 - time2 > interval) 
       flag = false;                 // should take 5 seconds to get through loop
  }
  digitalWrite(SleepPin, LOW);
}

I'm not sure why this isn't working. After the interval time of 5 seconds, the flag should go false, the code drop out of the while loop and the sleep pin pulled low. What am I missing?

did you use pinMode (SleepPin, OUTPUT); also?

I don't want to use the delay command,

yet that is effectively what this does, just sitting in the while()

while (flag == true) {
    time1 = millis(); 
    if (time1 - time2 > interval) 
       flag = false;                 // should take 5 seconds to get through loop
  }

What you want is blink-without-delay, where the time comparison is just part of loop - check the elapsed time, if not long enough you let other stuff occur.

Yes, I use the pinMode command. I just discovered that the digitalWrite(SleepPin, LOW); command isn't working. To test it, I replaced all the timing code with a simple delay(5000) and it still didn't work. I'm troubleshooting now.

Bad pin maybe? Wrong pin connected maybe?

If you want to ensure that nothing happens for X millisecs you should write code that resets the timer when something does happen - something like this

if (somethingHappens) {
   startTime = millis();
}
if (millis() - startTime > xmillis) {
    // do what is needed
}

This approach does not block. The time can only reach the xmillis value if nothing happens

Of course there should be no need to depower a stepper driver to prevent it overheating. You should use a stepper driver that can comfortably manage the current required by your motor. See stepper motor basics

...R

It's definitely a programming issue. I added a few patches to exercise the analog pin and it works fine. I have to take a closer look at my homing routine.

Robin2, thanks for the code snippet. I may need it some day. The driver chips heat up to 60 degrees C if I don't put them in sleep mode. While they can handle this temperature, I'd rather not keep them warmed up because it will probably shorten their life and if my tester is plugged in and not being used, they will sit at 60C forever. I believe that these chips can handle 125C, but why stress them. I'm using 12V steppers and a 12V supply and the steppers themselves remain cool.

ScottMurchison: Robin2, thanks for the code snippet. I may need it some day.

I proposed it because I thought / think it will meet your current need ?

...R