BME680 bsec library with Arduino IDE uses delay()?!?!? Not solved.

I clicked the wrong button, so I am making a new post. I also think I should have better described the problem.  My original post:

Use of delay() in Arduino code is universally considered bad practice. The better approach is to test a duration in millis.

Looking at the code in the provided libraries in BSEC_Arduino_Library, I note, in bsec.cpp, the function void Bsec::delay_us(uint32_t period, void *intfPtr). This function uses delay(). The use of delay() in bsec.cpp interferes with the timing of code in loop() if a call is made to the run() method.

Has anyone reimplemented bsec.cpp to avoid use of delay()? I plan on doing so, but do not want to "reinvent the wheel" if I can avoid that.

BSTRobinreplied

In bsec.cpp, the delay_us() function has been adapted to the millisecond delay to microsecond delay of the delay() function.

/**
@brief Task that delays for a us period of time
*/
void Bsec::delay_us(uint32_t period, void *intfPtr)
{
(void) intfPtr;
// Wait for a period amount of ms
// The system may simply idle, sleep or even perform background tasks
delay(period/1000);
}

Thus, hopefully my second attempt will be clearer.

BSTRobinThank you for your reply!

The code line delay(period/1000); is precisely what is, I beleive, causing the issue.  Per the Arduino documentation at https://www.arduino.cc/reference/en/language/functions/time/delay/

"...the use of delay() in a sketch has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt. "

It appears that any use of delay(), once execution passes to loop, especially outside of loop() will pause excution of loop().  If loop() contains deadlines, those deadlines may expire without the execution of that code segment at the time needed.  In my case specifically, adapting the BSEC example basic.ino code to my sketch completely disrupts the timing of loop().

Use of a timer interrupt with a 1mS resolution is a possible solution for my loop code, but delay() itself is paused while an interrupt service routine is executing.

SO! I'm thinking...

Thanks to all in advance!

3 replies