How long can I safely block loop()

This code will be used on both Boron and Argon connected to Particle Cloud.

I understand it is generally advisable to avoid blocking loop().

It is known that Particle Serial1 receive can be a bit buggy. I want to call a function that will send a Serial1 packet and await for a response with a timeout if no response arrives.

I would like to allow 50 ms before response timeout. The function that sends the first packet will be called from loop() or something based from loop, then inside that function, after the outbound packet, I want to wait up to 50ms then return, during which time, loop() would be blocked.

I think this is probably safe.

Does anyone expect this to cause problem with either wifi or cellular connectivity? I want to make sure I’m not about to introduce intermittent problems because of this.

I’m not running any other background threads except I am running a few software timers built into DeviceOS.

Thanks!

A delay of 50ms shouldn't cause a problem, but if you are concerned about blocking you could use a "non-blocking" delay.

inline void softDelay(uint32_t t) {
  for (uint32_t ms = millis(); millis() - ms < t; Particle.process());
}

Also, have you considered using the system thread?
https://docs.particle.io/reference/device-os/firmware/argon/#system-thread

By running the application thread and system thread separately, even if the application thread is blocked by delay() the system thread can still perform its duties.

2 Likes

This is a great way to implement a solution. Thank you. Very helpful. :clap:

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