Adding delay causes setup to loop forever

Here is a working setup function. Loop is just void loop() {}

void setup()
{
    static int j = 0;
    Serial.begin(115200);
    Serial.println("");
    delay(5000);
    unsigned long mi;
    mi = millis();
    Serial.printf("\n%d %ld ms\n",j++,mi);
    Serial.println("Continuing");

It produces the following:


0 5064 ms
Continuing

If I change it to look like:

#include <Arduino.h>
void setup()
{
    static int j = 0;
    Serial.begin(115200);
    delay(5000);
    Serial.println("");
    unsigned long mi;
    mi = millis();
    Serial.printf("\n%d %ld ms\n",j++,mi);
    Serial.println("Continuing");
}

void loop()
{
}

It produces the following, forever.


0 5064 ms
Continuing


0 5064 ms
Continuing


0 5064 ms
Continuing


Any idea why?
Thanks,
Jim.

Hi Jim,

The fact that your code is using Serial.printf() tells us that you are not using a "regular" Arduino like Uno/Mega. Is it an ESP8266 or ESP32 perhaps?

Sorry I should have mentioned that. Yes it is a 8266, more specifically a WEMOD D1 mini Pro.
Using Arduino version 1.8.16. I tried version 2 but I had problems and backed off.

Also note that commenting out the printf statement didn't improve anything. I just printed "Continuing" over and over.

What's the difference between the two sketches - besides the fact that the first one you posted is not complete?
If I try the second one, it works as expected.

Edit: Ok, I see, you exchanged two lines. But that makes no difference for me.

I'm not that familiar with ESP stuff, but is it possible that the MCU resets?

Quick search for getting reset info: ESP8266 getResetReason().
Although it's quite an old post, but there has to be something similar available.

unable to reproduce the described problem with the posted code on an esp32 ezebc

Tested on a WeMos D1 mini (no Pro), driver "LOLIN(WEMOS) D1 mini (clone)", IDE 1.8.19: everything is fine, so unable to reproduce the issue.

What exactly is the board driver you're using? Are you sure you set the IDE to "LOLIN(WEMOS) D1 mini Pro"?

Just in case, try to completely remove any trace of your 1.8.16 IDE (I hope you haven't installed it from Microsft Store...) and make a new clean install: get the zip "portable" version, copy its contents into any local directory (e.g. "C:\Arduino") and use it after adding the ESP boards drivers.

PS: there's no need for that "#include <Arduino.h>"...

No, I didn't expect it to, that's not what I was implying.

I agree the board is resetting. There's no other way that setup() would get run over and over like that. The question we need to be asking is why is it resetting.

With ESP boards, one reason can be that:

  1. In the background, the ESP is attempting to connect to your WiFi router, using the last SSID/password it has saved, even if your sketch doesn't call for that to happen.

  2. Connecting to WiFi can draw a lot of power. If the power supply or USB cable can't supply that power without suffering a voltage drop, that voltage drop will trigger an automatic "brown-out" reset.

So I would suggest trying different USB cables, different USB ports, different D1 minis if you have them, even a different laptop/PC.

try this code:'

#include <Arduino.h>
void setup()
{
    static int j = 0;
    Serial.begin(115200);
    yield();  // <<<<<<<-----------------
    delay(5000);
    Serial.println("");
    unsigned long mi;
    mi = millis();
    Serial.printf("\n%d %ld ms\n",j++,mi);
    Serial.println("Continuing");
}

void loop()
{
}

Not if you are using the default boot loader that cleverly clears the register before passing control to the user program!

What happens if you use delay(1); ?

That makes it more interesting. Every time I've seen an esp8266 reset there is a bunch of debug information that gets printed. Of course I haven't had all possible esp8266 errors. :slightly_smiling_face:

Thanks for all the ideas. I'm a bit handicapped in that I'm in a motor home in PA at a campground that has less than desirable internet access. I have two hotspots, an AT&T and T-Mobile plus the campground WiFi. I think the campground WiFi is a repeater of the AT&T cell system 'cause when my hotspot stops working the campground WiFi also does. Signals aren't very strong and even when I can run a speed test, I get around 2 Mb/s and lots of dropouts. Downloading anything is a chore.

I happened to have another 8266 dev board with me, I discovered, so I plugged it in and ran a few tests. It behaves as expected, such as others have reported so I only have to believe the D1 has a problem. I've also discovered that the problem only arises when the board is reset at the end of the upload. If I press the restart button it works OK and if I remove and replace power it seems to work OK. The flakey board is mounted on a PC board that I designed, had manufactured and populated. It's not very complex, including the D1 there is an ADS1115 ADC and associated resistors/diodes to condition the input. I can't imagine an I2C part causing the problem. It is connected to the standard clock and data pins.

Again, thanks to all.
Jim

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