-->
Page 1 of 1

crashing using millis() and while as a delay function

PostPosted: Thu Mar 23, 2017 7:39 am
by Mkling
Hi,

I have a problem and I don't really see what is happening.

I want to replace delay() with my own function and use millis().

Here is the code:
Code: Select allunsigned long returnmilli = 0;
void mydelay(unsigned long foo)
{
  returnmilli = millis()+foo;
  while(returnmilli>=millis())
  {
    delay(1);
  }
}


It's pretty simple, call the function with a number,
the function put the number + millis() into a variable, and does a while loop as long as the millis() is smaller than the variable.

and everything works good,

the problem arrives when I remove the delay(1); inside the while-loop (I did say that I didn't want to use delay - right).

So, I remove the delay() inside the while-loop, and the system crashes immediately when the function is called.

Code: Select all
Soft WDT reset

ctx: cont
sp: 3fff0980 end: 3fff0be0 offset: 01b0

>>>stack>>>
3fff0b30:  0000001c 00000000 3ffefafc 401004d8 
3fff0b40:  feefeffe 00000001 00000d8a 4020671a 
3fff0b50:  00000000 000000a6 0000000b 4020677c 
3fff0b60:  0000000f 00000011 3ffefafc 3ffefbb8 
3fff0b70:  3fffdad0 00000000 3ffefbb0 402067de 
3fff0b80:  3ffe8440 0000000f 3ffefafc 40207f38 
3fff0b90:  402010ae 0000001e 3ffefafc 40207f5c 
3fff0ba0:  3fffdad0 3ffefafc 3ffef9e4 4020666e 
3fff0bb0:  feefeffe feefeffe feefeffe 3ffefbb8 
3fff0bc0:  3fffdad0 00000000 3ffefbb0 40208634 
3fff0bd0:  feefeffe feefeffe 3ffefbc0 40100718 
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
ΓΌ


Booting....


I even tried to do some other stuff inside the wile (declare variables, serialprint's and so, but nope, it crashes).

anyone have any clues?

Re: crashing using millis() and while as a delay function

PostPosted: Mon Mar 27, 2017 1:21 pm
by jeffas
I suspect it's because you are using up all the processing power and not even allowing the watchdog timer in. I'm a little surprised that it crashes "immediately"; but then a human "immediately" is a lot longer than a computer "immediately".

Try putting a yield() call in, in place of the delay().

Re: crashing using millis() and while as a delay function

PostPosted: Mon Mar 27, 2017 2:50 pm
by schufti
arduino on the esp8266 has to return from the main loop every X ms to reset the watchdog.
Only some funtions e.g. delay(), yield() reset the wd, too.
So your delay could work for very small values of foo.