esp32-hal-misc.c: unsigned long IRAM_ATTR millis() { return (unsigned long) (esp_timer_get_time() / 1000ULL); } Of course, that just begs the question .... why would you ever use millis() on an ESP32? The esp_timer_get_time() function returns a 64-bit integer, has microsecond-resolution, and w… Answer from gfvalvo on forum.arduino.cc
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Question about ESP32 millis() counter? - Programming - Arduino Forum
March 8, 2021 - Hello, I have this kind of question. Is there any limitation about max millis() counter? I mean does it matter if currenttime in millis counts up certain value and the whole loop stops? I'm using millis() in order to set one counter to 0. At the moment ESP32 plugged to serial monitor about 23hours ticking, the millis() was working fine.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Where is the definition of function millis() located used for ESP32 - Programming - Arduino Forum
November 21, 2021 - Hi everybody, recently somebody asked a question about the rollover of millis() on ESP32-boards. I tried to find the definition of the function millis() but had no luck so far. I found something in ...packages\arduino\hardware\mbed\1.1.6\cores\arduino\wiring.cpp unsigned long millis() { return t.read_ms(); } but I'm still unsure if this is the defintion used if I compile for the platform ESP32 So does somebody know where this definition is or a method to really find it? best regards S...
Discussions

Question about using millis() efficiently
As a general programming principle it's a good idea to spend a few bytes of memory to avoid reprocessing. What's more, loop() executes really fast. On a Uno I think an empty loop() will execute about 4x per millisecond. Less often of course depending on how much it does, but in simple sketches multiple calls to millis() will likely return the same value during an iteration of loop(). My usual practice is to start loop() with long msNow = millis(); and use msNow as the time throughout loop(). edit: oh wait, you said esp32, the figure I quoted was for a Uno. Yes, almost undoubtedly millis() will return the same value throughout your loop. More on reddit.com
🌐 r/arduino
9
7
March 1, 2024
Serious problem with millis()
Board: ESP32 Dev Module nodeMCU 32s Core Installation version: latest git IDE name: Arduino IDE Flash Frequency: 40Mhz PSRAM enabled: ?no? ?yes? Upload Speed: default speed Computer OS: Ubuntu · millis() is not working as expected, causing time out problems every 72 minutes in many functions of the core and user programs... More on github.com
🌐 github.com
16
February 6, 2019
esp32 - I am using millis () function but after one iteration it does not check the condition and enters the loop. What should I do? - Arduino Stack Exchange
I am using mq7 sensor where I have to toggle vcc between 5 to 1.5 V for 1 min and 1.5 min. For this I am using millis function.But after 1st iteration it skips the condition and enters the loop. Th... More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
How to test millis() rollover on ESP32
Hi. I have a sketch that deals with time intervals and I and would like to test if I have coded everything correctly. So I'd like to test the behavior when the number of milliseconds rolls over (when it exceeds `4294967295 and restarts from 0, which happens after about 49 days since startup). More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
November 21, 2021
🌐
Stack Exchange
arduino.stackexchange.com › questions › 83307 › esp32-millis-not-working-properly
esp8266 - ESP32 millis not working properly - Arduino Stack Exchange
Calling millis() multiple times will give different results, when between the calls at least one millisecond passed. When initializing sendDataPrevMillis to zero, the time will be counted from startup (power cycle or reset).
🌐
ESP32Cube
esp32cube.com › post › how-to-use-millis-instead-of-delay
How to use millis() instead of delay() | ESP32Cube
November 25, 2024 - In order to make a pause or wait for the task in Arduino, the most widely used function is delay(), but sometimes it was cause some problem, in this article it will discuss it, and shows how to use function millis() to replace it.
🌐
Prosperityconsulting
prosperityconsulting.com › arduino-esp32-use-millis-to-put-commands-in-sequence-without-delay
Arduino Esp32 use millis() to put commands in sequence. Without delay – Prosperity Consulting
October 26, 2022 - unsigned long then = 0; unsigned long intervals[4] = { 0, 2000, 4000, 6000 }; int current_interval_index = 0; void setup() { Serial.begin(115200); } void loop() { unsigned long now = millis(); if (now - then >= intervals[current_interval_index]) { switch (current_interval_index) { case 0:
🌐
Mechatronics LAB
mechatronicslab.net › home › lessons › esp32 arduino programming handbook › chapter 12: timers, delays & interrupts › non-blocking delays with millis() for esp32 arduino programming
Non-Blocking Delays with millis() for ESP32 Arduino Programming - Mechatronics LAB
The millis() function returns the number of milliseconds since your ESP32 started running. You can use this number like a stopwatch. Instead of pausing your code, you check how much time has passed and decide if it’s time to act. This is perfect for multitasking.
🌐
Reddit
reddit.com › r/arduino › question about using millis() efficiently
r/arduino on Reddit: Question about using millis() efficiently
March 1, 2024 -

If your code includes many function on the main loop which call millis() for various timing needs, is it better to have a global variable which updates once per cycle with the value of millis() or is calling millis() seperatly many times perfectly fine?

We are talking 10-12 millis() calls per cycle at the moment.

The MCU is esp32

Top answer
1 of 2
5
As a general programming principle it's a good idea to spend a few bytes of memory to avoid reprocessing. What's more, loop() executes really fast. On a Uno I think an empty loop() will execute about 4x per millisecond. Less often of course depending on how much it does, but in simple sketches multiple calls to millis() will likely return the same value during an iteration of loop(). My usual practice is to start loop() with long msNow = millis(); and use msNow as the time throughout loop(). edit: oh wait, you said esp32, the figure I quoted was for a Uno. Yes, almost undoubtedly millis() will return the same value throughout your loop.
2 of 2
1
is it better to have a global variable which updates once per cycle with the value of millis() or is calling millis() seperatly many times perfectly fine? ... The MCU is esp32 For the function resolution of ESP32 specifically, the Arduino function internally calls esp_timer_get_time() which has microsecond resolution, which the library cuts off to milliseconds. For what is "better" will depend on what you're using it for. If you need consistency so everything in the same loop has the same number you'll want to call it once. If you're looking for the current time at that moment, you'll probably want to call it each time. The function call isn't free, it has the cost of potentially having instruction cache misses, the data load, and actually doing the math, but each call takes on the order of a few hundred nanoseconds worst case. If you're doing more advanced programming, another important detail for timings will be the CPU speed. The ESP32 family has power management so you don't have to be running the CPU at full speed and can do dynamic scaling, just like your home PC isn't running at 5 GHz all the time when browsing the web or sitting idle versus full power when playing advanced games. Sure you can run both cores at 240 MHz or even 400 MHz for the ESP32-P4 CPUs, but you can also slow it down to 80 MHz, or even 40 MHz or lower with custom settings. Even though operations take the same number of cycles you can get an order of magnitude difference in time if you're looking for efficiency of power. And if you're going really advanced, you can attach a JTAG debugger to use a profiler to see the actual performance numbers.
Find elsewhere
🌐
GitHub
github.com › espressif › arduino-esp32 › issues › 2430
Serious problem with millis() · Issue #2430 · espressif/arduino-esp32
February 6, 2019 - So the while loop stops after only 100 _micro_seconds of waiting (the assumed duration of one iteration of the while loop), instead of the intended 1000 _milli_seconds!!!! In the above example the same thing happens in a real example. The last timeout, exactly after about 72 minutes of running lasted only 1932 ms instead of the wanted 3000 ms. Stream is in the heart of many programs.
Author   espressif
🌐
ESP32 Forum
esp32.com › viewtopic.php
get current time in millis() - ESP32 Forum
July 21, 2017 - This is how arduino gets millis https://github.com/espressif/arduino-es ... misc.c#L50 You can also call esp_log_timestamp() from esp_log.h which is the same timestamp used in the ESP_LOGx() functions. You can also use arduino in esp-idf https://github.com/espressif/arduino-es ... -component ... This might be a useful previous post: https://esp32.com/viewtopic.php?t=1468 If it were me, I'd probably use "gettimeofday".
🌐
GitHub
github.com › peufeu2 › ESP32FastMillis
GitHub - peufeu2/ESP32FastMillis: Faster millis() and micros() for ESP32 and AceRoutine · GitHub
Then, micros() is just two instructions to read the 32-bit micros value from the timer. It is also possible to read a 64 bit microseconds value. For milliseconds, TIMG0_T1 is configured to run at 2 kHz.
Author   peufeu2
🌐
ESP32 Forum
esp32.com › viewtopic.php
Trouble with multiple millis() timers - ESP32 Forum
void lock() //maybe beginLocking() would be more descriptive { time_now = millis(); digitalWrite (int_lights, HIGH); digitalWrite (relay_1, LOW); //Starts actuator power // start up the relay run timer two = time_now; } Now in STATE_LOCKING_DOWN, two will have a useful value:
Top answer
1 of 1
3

The millis()-based programming idiom you are using is intended to handle delays without ever blocking the program in a delay() call. Then you are completely defeating its purpose by calling delay(90000). After this 1.5 min delay, of course one minute will have elapsed, and the test currentMillis - previousMillis > 60000 will always be true.

If you want to embrace the non-blocking paradigm millis() allows, you should completely ban delay() from your vocabulary (as well as other blocking loops).

Edit: Answering the question in the comment “Can you suggest as to how can I modify the code for 1.5 minutes delay”. As suggested by JRobert in another comment, you should follow the “Blink Without Delay” Arduino example. There is only one caveat: this Arduino example keeps the LED on for one second and off for another second, while you want to toggle your system between two states with different durations.

The solution is to remember what state you are currently on, and write separate code paths for handling each state. As an example, the code below toggles the on-board LED, keeping it on for 100 ms and off for 900 ms. This is achieved by remembering the current state (in the boolean variable is_on below) and conditioning the code execution on the current state (e.g. if (is_on && ...)).

/*
 * Toggle the on-board LED on for 100 ms every second.
 */

const unsigned long time_on  = 100;  // on time  = 100 ms
const unsigned long time_off = 900;  // off time = 900 ms
unsigned long previousMillis;   // previous time the LED was toggled
bool is_on;                     // is the LED currently on?

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
    is_on = false;
}

void loop() {
    unsigned long currentMillis = millis();

    // If the LED has been on long enough, turn it off.
    if (is_on && currentMillis - previousMillis >= time_on) {
        digitalWrite(LED_BUILTIN, LOW);
        is_on = false;
        previousMillis = currentMillis;
    }

    // If the LED has been off long enough, turn it on.
    if (!is_on && currentMillis - previousMillis >= time_off) {
        digitalWrite(LED_BUILTIN, HIGH);
        is_on = true;
        previousMillis = currentMillis;
    }
}
🌐
ESP32 Forum
esp32.com › viewtopic.php
ESP-IDF, esp_timer, and how to replace millis() - ESP32 Forum
Sun Dec 15, 2019 6:48 pm I'm working on an application that I'm moving some code from the arduino environment. I'm attempting to make if ESP-IDF only and the code I'm changing uses millis(). It appears from the ESP docs and some searching here on the forums that esp_timer_get_time() would be the replacement.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How to test millis() rollover on ESP32 - Programming - Arduino Forum
November 21, 2021 - Hi. I have a sketch that deals with time intervals and I and would like to test if I have coded everything correctly. So I'd like to test the behavior when the number of milliseconds rolls over (when it exceeds `42949…
🌐
Wokwi
wokwi.com › projects › 445215408599813121
Millis Function (ESM) - Wokwi ESP32, STM32, Arduino ...
unsigned long myTime; void setup() { Serial.begin(9600); // speed of transmission } void loop() { Serial.print("Time: "); myTime = millis(); Serial.println(myTime); // prints time since program started // delay(2000); // wait a second so as not to send massive amounts of data } // Non-blocking Timing with millis() /* unsigned long previousMillis = 0; const long interval = 1000; // 1 seconds void setup() { Serial.begin(9600); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; Serial.print("Time: "); Serial.println(currentMillis); } } */ Simulation
🌐
ESP32 Forum
esp32.com › viewtopic.php
ESP32 Deep Sleep, using millis in Arduino IDE, and then trying to do something in between - ESP32 Forum
I added these variables before void setup: uint32_t previousTime1 = 0; uint32_t deepsleepInterval = readingsInterval + 100; Then I added my deepsleep code inside of this millis function ... Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. ESP8266EX and ESP32 are some of our products.
🌐
Reddit
reddit.com › r/esp32 › how to add microseconds dely inside the millis function
r/esp32 on Reddit: how to add microseconds dely inside the millis function
February 7, 2023 -

I want to make the pin high as soon as the code enters into the millis function and turn it off after some microseconds and again turn it on after some milliseconds. here I can create a microsecond delay using delayMcirosecond but how can create the delay in milliseconds as we can not use delay inside millis

Code: int Ad[] = {20, 7, 6, 9, 1, 7, 8, 1, 1, 1, 30, 8, 7}; float Af[] = {1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23}; int Ap[] = {15, 25, 50, 50, 50, 90, 50, 50, 70, 50, 50, 50, 50};

int xa = sizeof(Ad) / sizeof(int); float ya = sizeof(Af) / sizeof(float); int za = sizeof(Ap) / sizeof(int);

void setup() { // put your setup code here, to run once: Serial.begin(115200); pinMode(26,OUTPUT);

} void w_a(int Ad1[], int x , float Af1[] , int y, int Ap1[], int z) { unsigned long previousMillis = 0; int i; for (i = 0; i < x; i++) { int d = Ad1[i] * 1000; float f = round((1 / Af1[i]) * 1000); int p = Ap1[i];

unsigned long currentMillis = millis();
previousMillis = currentMillis;
while (currentMillis - previousMillis < d)
{
  digitalWrite(26, HIGH);
  delayMicroseconds(p);
  digitalWrite(26, LOW);
  delay(f);
  currentMillis = millis();
}
previousMillis = currentMillis;

} }

void channel1_1() { int j = 0; for ( j = 0; j < 2; j++) { w_a(Ad, xa, Af, xa, Ap, xa); // A } }

void loop() { channel1_1(); }

🌐
GitHub
github.com › EEPROM-RISET-AND-DEVELOPMENT-DIVISION › MILLIS_TUTORIAL › blob › main › tutorial_millis_on_esp32.ino
MILLIS_TUTORIAL/tutorial_millis_on_esp32.ino at main · EEPROM-RISET-AND-DEVELOPMENT-DIVISION/MILLIS_TUTORIAL
unsigned long waktumillis = millis(); · if (waktumillis - waktumoment >= 200) { waktumoment = waktumillis; · · bool tombolDitekan = !(GPIO.in & (1 << TOMBOL_GPIO)); // LOW = ditekan · //GPIO.in itu register 32-bit yang ...
Author   EEPROM-RISET-AND-DEVELOPMENT-DIVISION
🌐
Wokwi
wokwi.com › projects › 362719280356720641
Millis Function - Wokwi ESP32, STM32, Arduino Simulator
// check to see if it's time to blink the LED; that is, if the // difference between the current time and last time you blinked // the LED is bigger than the interval at which you want to // blink the LED. unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) ...