-->
Page 1 of 2

What is the yield function for?

PostPosted: Fri May 20, 2016 12:31 pm
by ElCaron
Hi, I am using a Wemos D1 Mini with an infrared "obstacle sensor" to detect the mark on a Ferraris power meter. Since I have super low contrast with almost as much noise as signal, I want to read as often as possible to average. (Actually, I had to implement self-calibration code to keep it stable over more than a few hours.)

Now, first I forgot any delays and obviously, the network connection crashed immediately. Then I remembered I had to put delays, and got myself down to a delay of 2ms for stable operation. With 1ms, it fails.

Then I remembered the yield function. As far as I understood, this lets the ESP take over and do whats necessary. So I replaced my delay with the yield function (and later also added it in 4 other positions in my loop()), but then, my ESP is crashing again. Manages to answer 3 pings, then dies.

So, what am I misunderstanding? When is yield used? Is there another possibility to tell te ESP "Do what you have to, then let me do my thing again as soon as possible"?

Regarding versions: I set up the IDE with the board/libmanager, and those say my stuff is up to date. I haven't updated anything else. (I have not fully understood if the whole code of the ESP is replaced when uploading a sketch or if there is some firmware/bootloader I could update).

Re: What is the yield function for?

PostPosted: Fri May 20, 2016 3:26 pm
by picstart
I as you observed the esp8266 is an asynchronous device. Anything can happen at anytime. Mostly this is packets arriving over the air. The esp8266 buffers this but buffers must be accessed at a sufficient rate to avoid overflows.Yield allows another process to get some cpu time. Delay does the same only the timeout can be specified. Some things the esp8266 has to do are time consuming like signing on to routers. Call backs are available to avoid code blocking while a lengthy external process is run.

Re: What is the yield function for?

PostPosted: Sun May 22, 2016 2:38 am
by ElCaron
Sorry, but I don't see how this answers my question. I know that the ESP is an asynchronous device and that I have to give it CPU time. I wrote all that in my first posting.

What I don't understand is why replacing a single delay that worked with a yield did not work. In both cases I am giving the ESP CPU time. If yield doesn't cause it to take enough CPU time to do its thing, what use does it have?

Re: What is the yield function for?

PostPosted: Sun May 22, 2016 8:48 am
by schufti
Yield only takes the time internal stuff needs. Maybe with use of delay you also compensate for delay needed by yor external code. Sometimes people don't realise that things take some while and polling for 'ready state' would be apropriate before comencing and running into undefined states sometims leadingt to crashes. At last resort they start to insert delays ...