ESP32-WROOM-32 keep rebooting

Hi all,
First time here and a noob.
I started a very simple program as below:

void mydelay(long t){
  unsigned long startMillis = millis();
  unsigned long currentMillis = millis();
  while((currentMillis - startMillis) < t){
    currentMillis = millis();
  }
}

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.println("good");
  mydelay(500);
}

It keep rebooting with the following message:

18:04:52.666 -> good
18:04:53.182 -> good
18:04:53.647 -> E (10098) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
18:04:53.693 -> E (10098) task_wdt:  - IDLE0 (CPU 0)
18:04:53.693 -> E (10098) task_wdt: Tasks currently running:
18:04:53.693 -> E (10098) task_wdt: CPU 0: loopTask
18:04:53.693 -> E (10098) task_wdt: Aborting.
18:04:53.693 -> abort() was called at PC 0x400e0dd7 on core 0
18:04:53.693 -> 
18:04:53.693 -> ELF file SHA256: 0000000000000000
18:04:53.693 -> 
18:04:53.693 -> Backtrace: 0x4008443b:0x3ffbe490 0x40084699:0x3ffbe4b0 0x400e0dd7:0x3ffbe4d0 0x400830fe:0x3ffbe4f0 0x40080d49:0x3ffb1f50 0x400d0ae4:0x3ffb1f70 0x400d0b23:0x3ffb1f90 0x400d12c5:0x3ffb1fb0 0x40085651:0x3ffb1fd0
18:04:53.693 -> 
18:04:53.693 -> Rebooting...
18:04:53.693 -> ets Jun  8 2016 00:22:57
18:04:53.740 -> 
18:04:53.740 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
18:04:53.740 -> configsip: 0, SPIWP:0xee
18:04:53.740 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
18:04:53.740 -> mode:DIO, clock div:1
18:04:53.740 -> load:0x3fff0018,len:4
18:04:53.740 -> load:0x3fff001c,len:1216
18:04:53.740 -> ho 0 tail 12 room 4
18:04:53.740 -> load:0x40078000,len:10944
18:04:53.740 -> load:0x40080400,len:6388
18:04:53.740 -> entry 0x400806b4
18:04:53.834 -> good

If however I do not use the mydelay() function but just delay(), it works ok.
Can someone point me out the right direction?
billche

Don't use such awful code?

You are living in a multitasking environment, if you have nothing to do, yield.

You are hammering the value of millis into a variable for 500 ms,
without feeding the watchdog, which comes to bite you.

I think the watchdog gets fed on each call of loop,
but you don't leave that function for 500 ms.

Using the built-in delay function, feeds the watchdog,
or uses other means to hinder its triggering.

Your example here could be written as one if statement inside loop.

if 500 ms have expired, then print "good"

Get rid of your function and replace with delay(500)
This is the working function that is already defined

You may need to give it more amps of whatever V it runs on.

Thanks everyone respond to me.
I knew the code is awful otherwise I won't called myself noob, in fact the awful code is widely discussed on the internet and I just copied to try. The reason I post it here is to try to get help of why it caused a reboot, awful code won't cause reboot, right?

Wrong, it will.

Did you not understand reply #2 ?

It does this by preventing the return to the loop function which will automatically kick or reset the watchdog timer.

1 Like

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