time instead of iteration for turning off light
This commit is contained in:
@@ -6,7 +6,8 @@
|
|||||||
// Normal mode command
|
// Normal mode command
|
||||||
#define COMMAND "FDFCFBFA0800120000006400000004030201"
|
#define COMMAND "FDFCFBFA0800120000006400000004030201"
|
||||||
#define PRESENCE_DISTANCE 100
|
#define PRESENCE_DISTANCE 100
|
||||||
#define COUNT_STATUS_DIFFERENCE 5
|
#define DELAY_STATUS_CHECK 1000 // delay (in ms) between two checks of the presence
|
||||||
|
#define POWER_DELAY 20000 // delay (in ms) until turning off the screen
|
||||||
|
|
||||||
// Zigbee
|
// Zigbee
|
||||||
#define OCCUPANCY_SENSOR_ENDPOINT_NUMBER 1
|
#define OCCUPANCY_SENSOR_ENDPOINT_NUMBER 1
|
||||||
@@ -14,7 +15,7 @@
|
|||||||
ZigbeeOccupancySensor zbOccupancySensor = ZigbeeOccupancySensor(OCCUPANCY_SENSOR_ENDPOINT_NUMBER);
|
ZigbeeOccupancySensor zbOccupancySensor = ZigbeeOccupancySensor(OCCUPANCY_SENSOR_ENDPOINT_NUMBER);
|
||||||
|
|
||||||
bool isPresent = false;
|
bool isPresent = false;
|
||||||
int countDifferent = 0;
|
unsigned long lastMotionDetected = 0; // time when the last motion was detected
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
initSerial();
|
initSerial();
|
||||||
@@ -25,7 +26,7 @@ void setup() {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
readAndProcessSensorLines();
|
readAndProcessSensorLines();
|
||||||
delay(500);
|
delay(DELAY_STATUS_CHECK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void initSerial() {
|
void initSerial() {
|
||||||
@@ -95,25 +96,24 @@ void readAndProcessSensorLines() {
|
|||||||
|
|
||||||
// Check if the line contains the "Range" information
|
// Check if the line contains the "Range" information
|
||||||
if (line.startsWith("Range ")) {
|
if (line.startsWith("Range ")) {
|
||||||
|
|
||||||
// Extract the substring after "Range "
|
// Extract the substring after "Range "
|
||||||
String distanceStr = line.substring(6);
|
String distanceStr = line.substring(6);
|
||||||
int distance = distanceStr.toInt();
|
int distance = distanceStr.toInt();
|
||||||
bool currentStatus = distance <= PRESENCE_DISTANCE;
|
bool currentStatus = distance <= PRESENCE_DISTANCE;
|
||||||
|
|
||||||
if (currentStatus != isPresent) {
|
if (currentStatus == 1) {
|
||||||
countDifferent++;
|
lastMotionDetected = millis();
|
||||||
} else {
|
|
||||||
countDifferent = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// change the status, if the last n updates were different than the current status
|
if (currentStatus && !isPresent) {
|
||||||
if (countDifferent >= COUNT_STATUS_DIFFERENCE) {
|
isPresent = true;
|
||||||
isPresent = !isPresent;
|
|
||||||
bool res = sendZigbeeOnOffCommand(isPresent);
|
bool res = sendZigbeeOnOffCommand(isPresent);
|
||||||
Serial.print("Message sent: ");
|
Serial.println("Motion detected, turning screen light on");
|
||||||
Serial.print(isPresent);
|
} else if (!currentStatus && isPresent && (millis() - lastMotionDetected >= POWER_DELAY)) {
|
||||||
Serial.print(" - successful: ");
|
isPresent = false;
|
||||||
Serial.println(res);
|
bool res = sendZigbeeOnOffCommand(isPresent);
|
||||||
|
Serial.printf("No motion detected for %d ms, turning light off.\n", POWER_DELAY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user