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);
}