Hello Everyone, I hope someone can help me. I want to display two chronometers, The first one is on when we open water, means a impulsion is detected til 1 minute after there is no detection. the second one only is incrementing when we open water, the moment we close the water is stops, and then stops completly after 1 minute where there is no detection. Here is what I came up with but it's not giving exactly what I want. Can someone please help me.
#include <ArduinoBLE.h>
const int hallsensor = 2;
const int tsampling = 1;
const unsigned long maxIdleTime = 60000; // Durée maximale sans impulsion (1 minute)
volatile int NbTopsFan = 0;
float Q = 0.0;
float Calc = 0.0;
unsigned long startTimeTotal = 0;
unsigned long startTimeWaterRunning = 0;
unsigned long lastImpulseTime = 0;
bool isWaterRunning = false;
bool isIdle = false;
BLEService dataService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Random 128-bit UUID for the service
BLECharacteristic dataCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead, 20); // Random 128-bit UUID for the characteristic
void rpm() {
NbTopsFan++;}
void printDigits(unsigned long digits) {
if (digits < 10) {
Serial.print("0");
}
Serial.print(digits);
}
void setup() {
Serial.begin(9600);
pinMode(hallsensor, INPUT);
attachInterrupt(digitalPinToInterrupt(hallsensor), rpm, RISING);
startTimeTotal = millis();
if (!BLE.begin()) {
Serial.println("Erreur BLE");
while (1);
}
BLE.setLocalName("NanoBLE");
BLE.setAdvertisedService(dataService);
dataService.addCharacteristic(dataCharacteristic);
BLE.addService(dataService);
BLE.advertise();
Serial.println("En attente de connexion BLE...");
}
void loop() {
BLEDevice central = BLE.central();
NbTopsFan = 0;
delay(tsampling * 1000);
Q = NbTopsFan / (20.0 * tsampling);
Calc = (Q / 60) + Calc;
String To_App = String(Q, 3) + "," + String(Calc, 3);
Serial.println(To_App);
dataCharacteristic.writeValue(To_App.c_str(), To_App.length());
unsigned long currentTime = millis();
unsigned long elapsedTotal = currentTime - startTimeTotal;
if (Q > 0) {
// Mise à jour de la durée totale de la douche
unsigned long secondsTotal = (elapsedTotal / 1000) % 60;
unsigned long minutesTotal = (elapsedTotal / (1000 * 60)) % 60;
unsigned long hoursTotal = (elapsedTotal / (1000 * 60 * 60)) % 24;
Serial.print("Durée de la douche : ");
printDigits(hoursTotal);
Serial.print(":");
printDigits(minutesTotal);
Serial.print(":");
printDigits(secondsTotal);
Serial.println();
}
if (isWaterRunning) {
// Mise à jour de la durée d'écoulement de l'eau
unsigned long elapsedWaterRunning = currentTime - startTimeWaterRunning;
unsigned long secondsWaterRunning = (elapsedWaterRunning / 1000) % 60;
unsigned long minutesWaterRunning = (elapsedWaterRunning / (1000 * 60)) % 60;
unsigned long hoursWaterRunning = (elapsedWaterRunning / (1000 * 60 * 60)) % 24;
Serial.print("Durée d'écoulement d'eau : ");
printDigits(hoursWaterRunning);
Serial.print(":");
printDigits(minutesWaterRunning);
Serial.print(":");
printDigits(secondsWaterRunning);
Serial.println();
// Vérifier si aucune impulsion n'a été détectée pendant la durée maximale
if (elapsedWaterRunning >= maxIdleTime) {
isWaterRunning = false;
isIdle = true;
}
} else {
// Vérifier si une impulsion a été détectée après une période d'inactivité
if (isIdle && Q > 0) {
isWaterRunning = true;
startTimeWaterRunning = currentTime;
isIdle = false;
}
}
// Vérifier si aucune impulsion n'a été détectée pendant la durée maximale
if (elapsedTotal >= maxIdleTime) {
isWaterRunning = false;
isIdle = false;
}
}