extern "C" {
#include "user_interface.h"
}
os_timer_t hotTimer;
os_timer_t coldTimer;
#define HOT_PIN D1
#define COLD_PIN D2
#define TIME_BOUNCE 20
volatile unsigned long counterHotWater, counterColdWater;
unsigned long hotTimeBounce, coldTimeBounce;
int hotInt, coldInt;
int avail;
void setup() {
Serial.begin(115200);
pinMode(HOT_PIN, INPUT_PULLUP);
pinMode(COLD_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(HOT_PIN), hotInterrupt, RISING);
attachInterrupt(digitalPinToInterrupt(COLD_PIN), coldInterrupt, RISING);
os_timer_setfn(&hotTimer, hotTimerCallback, NULL);
os_timer_setfn(&coldTimer, coldTimerCallback, NULL);
counterHotWater = 0;
counterColdWater = 0;
hotInt = 0;
coldInt = 0;
}
void loop() {
if (counterHotWater) {
Serial.printf("counterHotWater: %d\n", counterHotWater);
counterHotWater = 0;
}
if (counterColdWater) {
Serial.printf("counterColdWater: %d\n", counterColdWater);
counterColdWater = 0;
}
if (Serial.available()) {
avail = Serial.parseInt();
Serial.printf("Delay %d seconds\n", avail);
pause(avail*1000);
}
yield();
}
void pause(unsigned long p) {
while (p) {
delay(1);
p--;
}
}
void hotInterrupt() {
if (hotInt == 0) {
hotInt++;
hotTimeBounce = millis();
}
Serial.println("hotInterrupt");
os_timer_arm(&hotTimer, TIME_BOUNCE, true);
}
void coldInterrupt() {
if (coldInt == 0) {
coldInt++;
coldTimeBounce = millis();
}
Serial.println("coldInterrupt");
os_timer_arm(&coldTimer, TIME_BOUNCE, true);
}
void hotTimerCallback(void *pArg) {
Serial.println("hotTimerCallback");
if (!digitalRead(HOT_PIN)) {
hotTimeBounce = millis();
return;
}
if (digitalRead(HOT_PIN) && hotTimeBounce + TIME_BOUNCE > millis()) return;
os_timer_disarm (&hotTimer);
hotInt = 0;
counterHotWater++;
}
void coldTimerCallback(void *pArg) {
Serial.println("coldTimerCallback");
if (!digitalRead(COLD_PIN)) {
coldTimeBounce = millis();
return;
}
if (digitalRead(COLD_PIN) && coldTimeBounce + TIME_BOUNCE > millis()) return;
os_timer_disarm (&coldTimer);
coldInt = 0;
counterColdWater++;
}