# Millis() not working

**URL:** <https://forum.arduino.cc/t/millis-not-working/1393544>\
**Category:** General Guidance\
**Created:** [July 4, 2025, 6:13am UTC](https://forum.arduino.cc/t/millis-not-working/1393544 "2025-07-04T06:13:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Declan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/declan/32/1126393_2.png) [@Declan](https://forum.arduino.cc/u/Declan)\
**Post date:** [July 4, 2025, 6:13am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/1 "2025-07-04T06:13:16Z")

</div>

I have a very simple sketch that runs a stepper motor.  
I am using millis(); to time how long the stepper motor runs to a given position.  
But, this just returns 0 in the Serial Monitor and I cannot understand why?

```arduino
#include <AccelStepper.h>

#define enable23 13
#define STEP_PIN_23 11
#define DIR_PIN_23 10

#define enable17 12
#define STEP_PIN_17 4
#define DIR_PIN_17 5

AccelStepper stepper23(1, STEP_PIN_23, DIR_PIN_23);
AccelStepper stepper17(1, STEP_PIN_17, DIR_PIN_17);

unsigned long timeEnd;
unsigned long timeBegin;

void setup() {
  Serial.begin(115200);
  pinMode(enable23, OUTPUT);
  digitalWrite(enable23, LOW);
  stepper17.setMaxSpeed(3000);
  stepper23.setMaxSpeed(3000);
}

void loop() {
  
  stepper17.setCurrentPosition(0);
  stepper17.moveTo(3520);
  stepper17.setMaxSpeed(2000);
  stepper17.setSpeed(-1750);
  timeBegin = millis();
  do {
    stepper17.runSpeedToPosition();
  } while (stepper17.distanceToGo() != 3520);
  timeEnd = millis();

  // stepper23.setCurrentPosition(0);
  // stepper23.moveTo(10536);
  // stepper23.setMaxSpeed(2000);
  // stepper23.setSpeed(-1200);
  // // stepper23.runSpeed();
  // do {
  // stepper23.runSpeedToPosition();
  // } while (stepper23.distanceToGo() != 10536);

  Serial.print("Start: ");
  Serial.println(timeBegin);
  Serial.print("End: ");
  Serial.println(timeEnd);

  unsigned long duration = timeEnd - timeBegin;
  Serial.print("Duration: ");
  Serial.println(duration);
}

```

Serial Monitor:

Start: 0

End: 0

Duration: 0

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 4, 2025, 6:16am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/2 "2025-07-04T06:16:06Z")

</div>

What do you see when you print timeBegin and timeEnd ?

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [July 4, 2025, 6:18am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/3 "2025-07-04T06:18:41Z")

</div>

Off the top of my head, I'll wager that if you create a counter that counts the number of times your code executes that while loop, you'll find it does it once.

```arduino
while (stepper17.distanceToGo() != 3520);

```

I have doubts that this does what you think it does.

> ## [◆](#a748665c3962e66fbc0e9373eb14c69c1)distanceToGo()
> 
> long AccelStepper::distanceToGo ( )
> 
> The distance from the current position to the target position.

---

<div class="post-metadata">

**Author:** ![Budvar10](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/budvar10/32/215868_2.png) [@Budvar10](https://forum.arduino.cc/u/Budvar10)\
**Post date:** [July 4, 2025, 6:21am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/4 "2025-07-04T06:21:45Z")

</div>

> [@Declan](#):
>
> `unsigned long duration = timeEnd - timeBegin;`

This is initialization of local variable and it is performed just once.  
Variables timeEnd, timeBegin are global, zeroed by default.  
Try this:

```arduino
unsigned long duration;
  duration = timeEnd - timeBegin;

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 4, 2025, 6:23am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/5 "2025-07-04T06:23:13Z")

</div>

You tell the stepper to move from 0 to 3520. How many times will distanceToGo not equal 3250 during that move ?

It looks like you should be testing whether distanceToGo is not equal to 0 rather than 3520

---

<div class="post-metadata">

**Author:** ![Declan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/declan/32/1126393_2.png) [@Declan](https://forum.arduino.cc/u/Declan)\
**Post date:** [July 4, 2025, 6:24am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/6 "2025-07-04T06:24:49Z")

</div>

Start: 0

End: 0

Duration: 0

---

<div class="post-metadata">

**Author:** ![Declan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/declan/32/1126393_2.png) [@Declan](https://forum.arduino.cc/u/Declan)\
**Post date:** [July 4, 2025, 6:27am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/7 "2025-07-04T06:27:48Z")

</div>

The steppers run perfectly

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [July 4, 2025, 6:29am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/8 "2025-07-04T06:29:34Z")

</div>

Which has nothing at all to do with what I wrote.

---

<div class="post-metadata">

**Author:** ![Budvar10](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/budvar10/32/215868_2.png) [@Budvar10](https://forum.arduino.cc/u/Budvar10)\
**Post date:** [July 4, 2025, 6:35am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/9 "2025-07-04T06:35:40Z")

</div>

> [@Declan](#):
>
> `timeBegin = millis()`

I don't see any obvious reason why it is not updating.  
Try the sketch just for millis().

---

<div class="post-metadata">

**Author:** ![Declan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/declan/32/1126393_2.png) [@Declan](https://forum.arduino.cc/u/Declan)\
**Post date:** [July 4, 2025, 6:40am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/10 "2025-07-04T06:40:26Z")

</div>

> [@Budvar10](#):
>
> ```arduino
> unsigned long duration;
> duration = timeEnd - timeBegin;
> 
> ```

Same result, just zeroes

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 4, 2025, 6:42am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/11 "2025-07-04T06:42:29Z")

</div>

Have you tried counting how many times that do/while loop executes as previously suggested ?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 4, 2025, 6:46am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/12 "2025-07-04T06:46:52Z")

</div>

> [@Declan](#):
>
> ```arduino
> do {
> stepper17.runSpeedToPosition();
> } while (stepper17.distanceToGo() != 3520);
> 
> ```

the [doc](https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#a344f58fef8cc34ac5aa75ba4b665d21c) for `runToPosition()`states

> _Moves the motor (with acceleration/deceleration) to the target position and blocks until it is at position. Dont use this in event loops, since it blocks_

`distanceToGo()` will be 0 at the end...

---

<div class="post-metadata">

**Author:** ![Declan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/declan/32/1126393_2.png) [@Declan](https://forum.arduino.cc/u/Declan)\
**Post date:** [July 4, 2025, 6:46am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/13 "2025-07-04T06:46:57Z")

</div>

Only once

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 4, 2025, 11:11am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/15 "2025-07-04T11:11:19Z")

</div>

> [@Declan](#):
>
> ```arduino
> timeBegin = millis();
> do {
> stepper17.runSpeedToPosition();
> toGoDistanceCounter++:
> } while (stepper17.distanceToGo() != 3520);
> timeEnd = millis();
> 
> ```

> [@van\_der\_decken](#):
>
> that if you create a counter that counts the number of times your code executes that while loop,...'

```arduino
  int toGoDistanceCounter = 0;

  timeBegin = millis();
  do 
  {
    stepper17.runSpeedToPosition();
    toGoDistanceCounter++:
  } 
  while (toGoDistanceCounter != 3520);
  timeEnd = millis();
  Serial.print(timeEnd - timeBegin); //should show non-zero value

```

The `stepper17.runSpeedToPosition();` function must be executing for 3520 times for stepper to arrive at its final postion. So, you need a counter to count how many times the function has been exected as said in #3.

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [July 4, 2025, 11:29am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/16 "2025-07-04T11:29:48Z")

</div>

> [@Declan](#):
>
> `while (stepper17.distanceToGo() != 3520);`

will exit the loop immediately, because the motor hasn’t moved yet when this is first tested(i.e. distanceToGo is still 3520).

> [@Declan](#):
>
> `while (stepper17.distanceToGo() != 0);`

Suggest you try this, with your original code otherwise as posted.

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [July 4, 2025, 11:36am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/17 "2025-07-04T11:36:19Z")

</div>

What Arduino is this running on?

There’s something fishy about those zero returns from millis(). Never seen that on my Nanos. The variables are globals, are written once each when you call millis(), and should have valid non-zero values when you do the initialization of duration. My comment before was with regards to the likelihood that they are the same, thus returning zero for a delta value, but the two timestamps should not be zero.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [July 4, 2025, 11:58am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/18 "2025-07-04T11:58:58Z")

</div>

Try this code and you will see the problem.

```arduino
  do {
    stepper17.runSpeedToPosition();
    Serial.println(stepper17.distanceToGo());
  } while (stepper17.distanceToGo() != 3520);

```

The do..while executes once, presumably because the accelstepper library had not yet updated the value of distanceToGo, so that it is still equal to 3520.  
After that, the loop starts over, and the stepper moves until distanceToGo() is zero, at which point the do..while loop will never exit.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 4, 2025, 5:50pm UTC](https://forum.arduino.cc/t/millis-not-working/1393544/19 "2025-07-04T17:50:40Z")

</div>

> [@GolamMostafa](#):
>
> So, you need a counter to count how many times the function has been exected as said in #3.

I don’t think you need anything else than

```arduino
stepper17.runSpeedToPosition();

```

It will block us you reached the destination

Also it’s a misconception if you think that the stepper would step every time you ask to run. Speed and acceleration come into play and the step is taken only when the step is due.

---

<div class="post-metadata">

**Author:** ![MicroBahner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/microbahner/32/238923_2.png) [@MicroBahner](https://forum.arduino.cc/u/MicroBahner)\
**Post date:** [July 4, 2025, 8:31pm UTC](https://forum.arduino.cc/t/millis-not-working/1393544/20 "2025-07-04T20:31:09Z")

</div>

> [@camsysca](#):
>
> There’s something fishy about those zero returns from millis(). Never seen that on my Nanos.

It's ok, because these 'zeros' are only outputted once. And because there is no delay from start, not even 1 millisecond has elapsed when millis() is called the first time.

> [@Declan](#):
>
> The steppers run perfectly

The main reason, that your code doesn't work, is the incorrect usage of `.distanceToGo()`. As already stated, you must check for 0, and not for 3520.

What happens with your code is:

- when `stepper17.runSpeedToPosition();`is called for the first time after setup, no step is generated, because millis is still 0.
- because no step is generated, the `distanceToGo()` is still 3250, and the while loop is exited immediately.
- Your serial prints are printed with all values of 0
- loop() is started again. Now millis() is \> 0 and a step is generated with the first call of `runSpeedToPosition()`. Now distanceToGo is \< 3250, and your while loop will never end. So no further printing.
- Because the while loop calls `runSpeedToPosition()` again and again, your stepper does its 3520 steps, but the while loop will not end after that - it loops endless.

You can prevent the special behaviour of Accelstepper in the first call of loop ( with millis() == 0 ) if you add a short delay at the end of setup ( a `delay(1)` is sufficent ). Now your while will loop endless from the beginning, and you will never get an output to the serial monitor.

Hope this helps understanding what is happening.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [July 5, 2025, 1:33am UTC](https://forum.arduino.cc/t/millis-not-working/1393544/21 "2025-07-05T01:33:19Z")

</div>

> [@Declan](#):
>
> a very simple sketch

I noticed you moved from an [interrupt driven stepper to `AccelStepper.h`](https://forum.arduino.cc/t/buggy-stepper-code/1384012/7). Have you tried `Servo.h`? Have you tried making your own step pulses and acceleration?

```arduino
// slowly accelerate to desired motor speed - no library

const int dirPin = 2,
          stepPin = 3;

// int usDelay, // oops... made a mistake (see post #22)
long usDelay, // pause between half-step
     ramp = 50; // rate of acceleration // keeping this type long

unsigned long timer,
         timeout = 1000,
         multiplier = 16; // start value multiplier, maximum = 16

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  digitalWrite(dirPin, HIGH);

  for (usDelay = timeout * multiplier; usDelay > timeout; usDelay -= ramp) { // decrease step delay
    onestep(); // step the motor
  }
}

void loop() {
  if (micros() - timer > timeout) { // use timeout value for constant rotation speed
    timer = micros(); // reset timer to wait for next pulse
    onestep();
  }
}

void onestep() {
  digitalWrite(stepPin, LOW);
  delayMicroseconds(usDelay);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(usDelay);
}

```

> **diagram.json for wokwi**
>
> ```arduino
> {
> "version": 1,
> "author": "foreignpigdog x",
> "editor": "wokwi",
> "parts": [
> { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -10.1, "attrs": {} },
> {
> "type": "wokwi-stepper-motor",
> "id": "stepper1",
> "top": -172.81,
> "left": 185.82,
> "attrs": { "size": "8", "arrow": "white" }
> },
> { "type": "wokwi-a4988", "id": "drv1", "top": -91.2, "left": 110.4, "attrs": {} },
> { "type": "wokwi-vcc", "id": "vcc1", "top": -133.64, "left": 163.2, "attrs": {} },
> { "type": "wokwi-gnd", "id": "gnd1", "top": -9.6, "left": 172.2, "attrs": {} }
> ],
> "connections": [
> ["vcc1:VCC", "drv1:VMOT", "red", ["v0"] ],
> ["gnd1:GND", "drv1:GND.2", "black", ["v0"] ],
> ["drv1:GND.2", "drv1:ENABLE", "black", ["h19.35", "v-19.12", "h-86.4", "v9.6"] ],
> ["drv1:2B", "stepper1:A+", "green", ["h0"] ],
> ["drv1:2A", "stepper1:A-", "green", ["h0"] ],
> ["drv1:1A", "stepper1:B-", "green", ["h0"] ],
> ["drv1:1B", "stepper1:B+", "green", ["h0"] ],
> ["gnd1:GND", "drv1:GND.1", "black", ["v0"] ],
> ["vcc1:VCC", "drv1:VDD", "red", ["v0"] ],
> ["nano:2", "drv1:DIR", "orange", ["v0"] ],
> ["nano:3", "drv1:STEP", "white", ["v0"] ],
> ["drv1:RESET", "drv1:SLEEP", "blue", ["h-19.2", "v9.6"] ]
> ],
> "dependencies": {}
> }
> 
> ```

[Next page](https://forum.arduino.cc/t/millis-not-working/1393544.md?page=2)
