Re: Make millis() accurate regardless of code (or alternative to it)

Hi to everybody,
I've gone through the thread and I haven't been able to find a solution to my problem. I've implemented, with some modifications, a project taken from Random Nerd Tutorials aimed to control with Alexa the electric roller shutter, at home. The project works fine but what drives me mad is the following:
I can voice activate two relays without any issue but I also need to keep them excited for just 30 seconds. Consequently I added, in the void loop() the following piece of code

void loop() {

// fauxmoESP uses an async TCP server but a sync UDP server
// Therefore, we have to manually poll for UDP packets

  fauxmo.handle();
  
   // check if delay has timed out after Roll Up Time

  static unsigned long last = millis();
    Serial.println(last);
    delay(500);
  if (millis() - last > RollUpTime) {   // RollUpTime = 42000
    last = millis();
    Serial.println(millis());
    delay(500);
    digitalWrite(RELAY_PIN_1, HIGH);
    digitalWrite(RELAY_PIN_2, HIGH);
  }
}

Now, I had to put 42 seconds as Roll Up Time in order to achieve 30 and, as a matter of fact, the 30 seconds is not repetitive. It varies by 3-4 seconds around its target value.
I'm sure I'm overlooking something simple, stupid, I'd say, because of my lack of experience.
Can anybody help me? I don't want to revert to using Delay(). I'd like a more elegant solution
Many thanks in advance

Works for me, accurate to about 20 milliseconds:
11:04:30.137 -> 42200
11:05:12.123 -> 84201
11:05:54.136 -> 126202
11:06:36.117 -> 168203
11:07:18.123 -> 210204
11:08:00.102 -> 252206

Looks like you should have had ">= RollUpTime" in place of "> RollUpTime".

My guess would be a faulty resonator running somewhat faster than 16 MHz. To get 42000 milliseconds done in 30 seconds you're running around 22.4 MHz!

Many thanks for the prompt reply.
Actually I'm using an ESP8266-01S module, fairly cheap. It could be the xtal but, then, why it is not repetitive?. As I wrote, the time interval isn't always 30 sec. Sometimes it's 27, other times it's 33.
I'm using an accurate electronic chronograph to measure the time the two relays are on and I may be slow or fast by a sec, not more.
To clear the table from the doubts on the xtal I will repeat the experiment on an ESP32 WROOM module that looks a bit better but the question mark about the lack of repetitiveness remains. Could it be the fauxmo.handle()? I know that on the ESP8266-01S the CPU resources are divided between the WiFi and the rest. Might it influence, somehow, the mills count?

I run a quick check replacing > with >= and, actually, it seems to have gained in repetitiveness. What still remains weird is the time, now it is 35 sec instead of 42. I've to run it on a different module, ESP32 based, as I wrote

With an ESP8266, why use millis()?

See ESP8266 API Hardware Timers, RTC time, and the other timer related mentions.

@Fabri54, do not hijack. Thread split.

My apologies but I'm not sure I understand the comment. Is it because I'm using an ESP8266?

If your reply was meant for me...
https://www.google.com/search?q=hijack+thread

Yes Coding Badly, and I got the message. The title of the thread, and many of the posts, were perfectly describing my problem and I jumped into it.
My apologies.

Fabri54:
It could be the xtal but, then, why it is not repetitive?.

IMHO your problem ist in fauxmo.handle().

I'm afraid you're right. The fauxmo.handle() and the call back fauxmo.onSetState, in the setup section, are interacting in a way that is a bit obscure to me.
I've to find another way to set a timer for resetting the relays once they have been activated by a voice command. I'm even considering an HW based solution (NE555 triggered by the microcontroller)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.