How to set timeout to a function when waiting for the result?

Hi,

I have connected a barometer BMP_280 to Arduino. I am trying to write a program that will turn on a LED whenever it realizes my sensor is missing.

For testing it, I am unplugging the feed wire from the 3.3v pin to see if it realizes that it is missing, but what happens is that the program gets frozen (due to the function readPressure and readTemperature, that reads the information from the sensor) until I plug it back and continues to read values.

So i guess i need to set a timeout to wait a certain amount of time until the reading functions give me or not a value, and after the timeout, just skip it and turn on a LED. But I cant succeed.

I tried to do multitasking using MultiTaskLib and separated the blinking LED from the reading thing but it all gets frozen anyway after unplugging.

any recomendations? I am using Arduino Uno and this is my code:

#include <MultiTaskLib.h>
#include <Adafruit_BMP280.h>


/* -------------------------------------------------------
   | Define barometer sensor BMP280 necessary parameters |
   -------------------------------------------------------

   PIN configuration:
   ----------------------
   | PIN  |  SENSOR PIN |
   ----------------------
   | A4   |  SDA        |
   | A5   |  SCL        |
   | 3.3V |  VCC        |
   | GND  |  GND        |
   ----------------------
*/

#define BMP280_ADDRESS (0x76)                         //I2C interface: device address is 0x76 when SDO is connected to GND (or not connected). 0x77 else
Adafruit_BMP280 bmp;

#define LED_PIN 7

MultiTask multitask(2); //2 tasks: 1. for reading sensor information and 2. for controlling its LED


void setup() {

  Serial.begin(57600);

  //begin BMP_280 connection to interface I2C (0x76 -> BMP280_ADDRESS)
  bmp.begin(BMP280_ADDRESS);

  //LED setup
  pinMode(LED_PIN, OUTPUT);


  //execute sensor reading continuously and trigger LED alert in case of callback
  multitask.AddContinuous( 
    100, 
    []() {read_bmp280();}
    );

  multitask.AddAlternantContinuous(
    1000,  
    []() {digitalWrite(LED_PIN, HIGH);},
    []() {digitalWrite(LED_PIN, LOW);}
    );
}


void loop() {  

  multitask.Update();

}


float read_bmp280() {  
  float pressure = bmp.readPressure();
  float temperature = bmp.readTemperature();
  
  Serial.println(pressure);
  Serial.println(temperature);

  return pressure, temperature;
}


/*
   Check our barometer sensor status and act in consequence
*/
void alert_missing_baro() {                  
  digitalWrite(LED_PIN, HIGH);
  digitalWrite(LED_PIN, LOW);
}

check the bmp library for any function that indicates status, use it before asking for a temperature read. You may depend on that since the library functions are likely where it is freezing.

1 Like

So, you are expecting a device to be able to be plugged in and pulled out of a live circuit and you are expecting the device to properly work, right?

just expecting the system to be able to handle it, recognize that that sensor was removed from it and then notify with a LED. If it would be possible..

It can sometimes work but in this case it's I2C. 'getstatus()' method only checks to see if an I2C object exists in code, not on the physical wire. So it is probably the I2C bus access via the I2C I/O that is freezing.

Actually your code does that, since the LED indicates the removal by stopping flashing... :slight_smile:

The issue is that the whole code gets frozen, so it is actually not a good ending. If this arduino will be controlling a little RC car, and it looses a sensor then the whole car would be crashed XD :frowning:

You are expecting to have the device inserted and removed as part of normal operations?

Of course the code would freeze when the device is removed. You'll need to edit the library and add times outs. I bet the code author did not write the code expecting to have the device removed and added back to the circuit while being powered

Hmmm... yes, when you lean over and remove the temp sensor from an RC car?

It is just to handle sensor problems. In case the sensor will be unfortunatelly missing, i dont want the whole system to die, just to learn it, and continue somehow..

Why do you expect it to die? Are you having issues?

well it is not a normal situation but it can happen that the connection sensor <-> arduino will be failing

1 Like

the code after the freezing piece of code stops as well

But then, how would the rest of the system respond? Does it need temperatures to run, or??

OK so before doing a reading do an I2C scan, if the sensor is present then do a reading, if not then don't do a reading. Simple.

1 Like

Have you tried 'getStatus' yet?

Yes it will need since actually i am planning to use arduino as an airdata computer that will support drones, so all this information about pressure, temp, etc is necessary

yes i receive some bytes about it, but once disconnected not anymore (of course) so actually i dont know what to do with it

how can a I2C scan be done?

like this?

if (bmp.getStatus() ) {
// read temps
...
1 Like