2025-04-13 14:59:01 -03:00
|
|
|
#include "alarm.h"
|
2025-05-17 00:28:37 -03:00
|
|
|
#include "Times.h"
|
2025-04-13 14:59:01 -03:00
|
|
|
|
|
|
|
AlarmStatus* status;
|
|
|
|
|
|
|
|
class DoorSensor {
|
|
|
|
private:
|
2025-04-20 18:02:17 -03:00
|
|
|
int DOOR_SENSOR_PIN = 14;
|
2025-04-13 14:59:01 -03:00
|
|
|
AlarmStatus* status;
|
2025-05-17 00:28:37 -03:00
|
|
|
int lastDoorState = 1; // Last instantaneous reading
|
|
|
|
unsigned long lastDebounceTime = 0; // Time when a change was first detected
|
2025-04-13 14:59:01 -03:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2025-04-20 18:02:17 -03:00
|
|
|
static const int DEFAULT_DOOR_SENSOR_PIN=14;
|
2025-04-13 14:59:01 -03:00
|
|
|
|
2025-04-13 22:52:33 -03:00
|
|
|
DoorSensor(AlarmStatus* statusObj, int doorSensorPin){
|
2025-04-13 14:59:01 -03:00
|
|
|
DOOR_SENSOR_PIN = doorSensorPin;
|
|
|
|
status = statusObj;
|
|
|
|
}
|
2025-04-13 22:52:33 -03:00
|
|
|
DoorSensor(AlarmStatus* statusObj){
|
|
|
|
DOOR_SENSOR_PIN = DEFAULT_DOOR_SENSOR_PIN;
|
|
|
|
status = statusObj;
|
|
|
|
}
|
2025-04-13 14:59:01 -03:00
|
|
|
void Init(){
|
|
|
|
pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
|
|
|
|
status->doorStatus = digitalRead(DOOR_SENSOR_PIN);
|
2025-05-17 00:28:37 -03:00
|
|
|
lastDoorState = status->doorStatus;
|
2025-04-13 14:59:01 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDoorOpen(){
|
|
|
|
return status->doorStatus == DOOR_OPEN;
|
|
|
|
}
|
|
|
|
bool IsDoorClosed(){
|
|
|
|
return status->doorStatus == DOOR_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleDoor(){
|
2025-05-17 00:28:37 -03:00
|
|
|
// Read the current state of the door sensor
|
|
|
|
int reading = digitalRead(DOOR_SENSOR_PIN);
|
2025-04-13 14:59:01 -03:00
|
|
|
|
2025-05-17 00:28:37 -03:00
|
|
|
// If the reading has changed from the previous instantaneous reading,
|
|
|
|
// reset the debounce timer.
|
|
|
|
if (reading != lastDoorState) {
|
|
|
|
lastDebounceTime = millis();
|
2025-04-13 14:59:01 -03:00
|
|
|
}
|
2025-05-17 00:28:37 -03:00
|
|
|
|
|
|
|
// If the new reading has remained stable longer than the debounce delay,
|
|
|
|
// then update the door state.
|
|
|
|
if ((millis() - lastDebounceTime) >= FromSeconds(1)) {
|
|
|
|
// If the reading is different from the last stable door state,
|
|
|
|
// update the status and mark that a change has occurred.
|
|
|
|
if (reading != status->doorStatus) {
|
|
|
|
status->doorStatus = reading;
|
|
|
|
status->doorChanged = true;
|
|
|
|
}
|
2025-04-13 14:59:01 -03:00
|
|
|
}
|
2025-05-17 00:28:37 -03:00
|
|
|
|
|
|
|
// Save the current reading for the next iteration.
|
|
|
|
lastDoorState = reading;
|
2025-04-13 14:59:01 -03:00
|
|
|
}
|
|
|
|
};
|