Skip to content

Commit cd435d1

Browse files
committed
Fixed bug #75
Fixed bug #75, added timeout to "takeForcedMeasurement" and a return value to check the result of the function
1 parent 2046f41 commit cd435d1

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Adafruit_BME280.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,20 +343,33 @@ uint32_t Adafruit_BME280::read24(byte reg) {
343343

344344
/*!
345345
* @brief Take a new measurement (only possible in forced mode)
346+
@returns true in case of success else false
346347
*/
347-
void Adafruit_BME280::takeForcedMeasurement() {
348+
bool Adafruit_BME280::takeForcedMeasurement() {
349+
bool return_value = false;
348350
// If we are in forced mode, the BME sensor goes back to sleep after each
349351
// measurement and we need to set it to forced mode once at this point, so
350352
// it will take the next measurement and then return to sleep again.
351353
// In normal mode simply does new measurements periodically.
352354
if (_measReg.mode == MODE_FORCED) {
355+
return_value = true;
353356
// set to forced mode, i.e. "take next measurement"
354357
write8(BME280_REGISTER_CONTROL, _measReg.get());
358+
// Store current time to measure the timeout
359+
uint32_t timeout_start = millis();
355360
// wait until measurement has been completed, otherwise we would read
356-
// the values from the last measurement
357-
while (read8(BME280_REGISTER_STATUS) & 0x08)
361+
// the values from the last measurement or the timeout occurred after 2 sec.
362+
363+
while (read8(BME280_REGISTER_STATUS) & 0x08) {
364+
// In case of a timeout, stop the while loop
365+
if((millis() - timeout_start) > 2000) {
366+
return_value = false;
367+
break;
368+
}
358369
delay(1);
370+
}
359371
}
372+
return return_value;
360373
}
361374

362375
/*!

Adafruit_BME280.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class Adafruit_BME280 {
225225
sensor_filter filter = FILTER_OFF,
226226
standby_duration duration = STANDBY_MS_0_5);
227227

228-
void takeForcedMeasurement();
228+
bool takeForcedMeasurement();
229229
float readTemperature(void);
230230
float readPressure(void);
231231
float readHumidity(void);

0 commit comments

Comments
 (0)