Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By ElCaron
#47722 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).
User avatar
By picstart
#47732 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.
User avatar
By ElCaron
#47794 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?
User avatar
By schufti
#47809 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 ...