Preface
Currently, I'm making an application on a Arduino 33 Nano BLE Sense. I'm trying to measure the current humidity and send it via BLE to another device. The target is to send a precise value with a high frequency, 8-10 values every second.
For measuring the humidity I connected an external sensor, the P14 Rapid Capacitive Humidity Sensor (it's faster and more accurate than the onboard humidity sensor) with a NE555 Timer IC in Monostable Mode.
To read the output pulse (frequency: ca. 500Hz) I'm using pulseIn()
.
I managed to calculate the humidity correctly and also the BLE data transfer worked.
Problem
My (minified) sketch:
#include <ArduinoBLE.h>
void setup() {
// some setup, takes around 10s
}
void loop() {
BLE.poll(); // polling for new events, takes ca. 10 µs
value = pulseIn(6, HIGH); // read the high pulse, takes around 500-4000 µs
// calculating the average value, takes < 1 µs
if (/* this if is executed every 1 second for testing purposes, targeted is every 100ms*/) {
// humidity is calculated, takes ca. 20 µs
characteristic.writeValue(/* calculated value */); // takes ca. 50 µs
}
}
When I merged the features, the problems began. I couldn't connect to the Arduino anymore.
When I try to connect to it (with nrf Connect App), this happens. When I manage to connect to it before it stops working, I will be disconnected with a GATT_CONN_TIMEOUT
exception.
When the Arduino is powered on, the setup is called and it's connectable while the setup is running. When pulseIn()
is called for the first time it stops working.
I already tried calling pulseIn()
only every 10s or only once, it stops working.
And yes, commenting out the pulseIn()
makes BLE work again.
Possible Solution
Next thing I would try is to delete the pulseIn()
and use interrupts for measuring the time.
My question is if there is a better way to fix this issue?