@@ -343,20 +343,32 @@ uint32_t Adafruit_BME280::read24(byte reg) {
343
343
344
344
/* !
345
345
* @brief Take a new measurement (only possible in forced mode)
346
+ @returns true in case of success else false
346
347
*/
347
- void Adafruit_BME280::takeForcedMeasurement () {
348
+ bool Adafruit_BME280::takeForcedMeasurement (void ) {
349
+ bool return_value = false ;
348
350
// If we are in forced mode, the BME sensor goes back to sleep after each
349
351
// measurement and we need to set it to forced mode once at this point, so
350
352
// it will take the next measurement and then return to sleep again.
351
353
// In normal mode simply does new measurements periodically.
352
354
if (_measReg.mode == MODE_FORCED) {
355
+ return_value = true ;
353
356
// set to forced mode, i.e. "take next measurement"
354
357
write8 (BME280_REGISTER_CONTROL, _measReg.get ());
355
- // wait until measurement has been completed, otherwise we would read
356
- // the values from the last measurement
357
- while (read8 (BME280_REGISTER_STATUS) & 0x08 )
358
+ // Store current time to measure the timeout
359
+ uint32_t timeout_start = millis ();
360
+ // wait until measurement has been completed, otherwise we would read the
361
+ // the values from the last measurement or the timeout occurred after 2 sec.
362
+ while (read8 (BME280_REGISTER_STATUS) & 0x08 ) {
363
+ // In case of a timeout, stop the while loop
364
+ if ((millis () - timeout_start) > 2000 ) {
365
+ return_value = false ;
366
+ break ;
367
+ }
358
368
delay (1 );
369
+ }
359
370
}
371
+ return return_value;
360
372
}
361
373
362
374
/* !
0 commit comments