Motion must be active for 5 seconds to reduce incidental fan activation
This commit is contained in:
parent
0eeba80bff
commit
4dfc72ba47
2 changed files with 45 additions and 8 deletions
34
lib/Components/BinaryInput.cpp
Normal file
34
lib/Components/BinaryInput.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#ifndef BinaryInput_cpp
|
||||
#define BinaryInput_cpp
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class BinaryInput {
|
||||
int _pin;
|
||||
bool _value;
|
||||
unsigned long _lastStateChangeMillis;
|
||||
|
||||
public:
|
||||
BinaryInput(int pin) {
|
||||
_pin = pin;
|
||||
pinMode(pin, INPUT_PULLDOWN);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
auto value = digitalRead(_pin);
|
||||
if (value != _value)
|
||||
_lastStateChangeMillis = millis();
|
||||
|
||||
_value = value;
|
||||
}
|
||||
|
||||
bool isHigh() {
|
||||
return _value;
|
||||
}
|
||||
|
||||
unsigned long millisSinceLastStateChange() {
|
||||
return millis() - _lastStateChangeMillis;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,13 +1,14 @@
|
|||
#include <Arduino.h>
|
||||
#include "BinaryOutput.cpp"
|
||||
#include "BinaryInput.cpp"
|
||||
#include "PWMOutput.cpp"
|
||||
#include "EspNowLightControlClient.cpp"
|
||||
#include "LightControlPayload.cpp"
|
||||
#include "Timer.cpp"
|
||||
|
||||
#define MOTION_PIN 4
|
||||
#define MOTION_DURATION 60000 // 1 minute
|
||||
#define FAN_DURATION 300000 // 5 minutes
|
||||
#define FAN_ON_DELAY 5000
|
||||
#define BROADCAST_INTERVAL 500
|
||||
#define BROADCAST_COUNT 3
|
||||
|
||||
|
@ -15,6 +16,7 @@ EspNowLightControlClient _lightControl;
|
|||
void sendPayload();
|
||||
void resetBroadcast();
|
||||
bool _motionActive = false;
|
||||
BinaryInput _motionSensor(4);
|
||||
PWMOutput _fan(23, 25000, 1);
|
||||
|
||||
int _broadcastCount = 0;
|
||||
|
@ -55,24 +57,24 @@ void resetBroadcast() {
|
|||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
pinMode(MOTION_PIN, INPUT);
|
||||
|
||||
_lightControl.init();
|
||||
_broadcastTimer.reset(BROADCAST_INTERVAL);
|
||||
_fan.setValueFraction(0.5f);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
bool motionDetected = digitalRead(MOTION_PIN);
|
||||
|
||||
if (motionDetected) {
|
||||
if (motionDetected != _motionActive) {
|
||||
void loop() {
|
||||
if (_motionSensor.isHigh()) {
|
||||
if (_motionSensor.isHigh() != _motionActive) {
|
||||
Serial.println("Motion detected");
|
||||
_motionActive = true;
|
||||
sendPayload();
|
||||
resetBroadcast();
|
||||
}
|
||||
|
||||
if (_motionSensor.millisSinceLastStateChange() >= FAN_ON_DELAY && _motionActive) {
|
||||
_fan.on();
|
||||
}
|
||||
|
||||
_motionStopTimer.reset(MOTION_DURATION);
|
||||
_fanStopTimer.reset(FAN_DURATION);
|
||||
}
|
||||
|
@ -80,4 +82,5 @@ void loop() {
|
|||
_broadcastTimer.loop();
|
||||
_motionStopTimer.loop();
|
||||
_fanStopTimer.loop();
|
||||
_motionSensor.loop();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue