@Boots33 Thanks for the help. The main problem was how to set the time when the doors are opened. I have tried several attempts and everytime the timer is reset. Therefore the buzzer will never be triggered because the timer and the current time is always the same.
I have solved this problem. The working script is here below:
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#define CHILD_ID_Koel 31
#define CHILD_ID_Vries 32
#define BUTTON_PIN_Koel 7 // Arduino Digital I/O pin for button/reed switch
#define BUTTON_PIN_Vries 8 // Arduino Digital I/O pin for button/reed switch
#define INTERRUPT BUTTON_PIN_Koel -2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define INTERRUPT BUTTON_PIN_Vries -2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define buzzPin 2 // I/O-pin from buzzer connects here
unsigned long currentMillisKoel;
unsigned long currentMillisVries;
MySensor gw;
Bounce debouncerKoel = Bounce();
Bounce debouncerVries = Bounce();
int oldValueKoel = -1;
int oldValueVries = -1;
int KoelBuzz = 0;
int VriesBuzz = 0;
unsigned long DoorDelay = 5000UL;
unsigned long KoelTimer = 0UL;
unsigned long VriesTimer = 0UL;
MyMessage msgKoel(CHILD_ID_Koel, V_TRIPPED);
MyMessage msgVries(CHILD_ID_Vries, V_TRIPPED);
void setup()
{
gw.begin(NULL, 204, false, 0);
// Setup the button
pinMode(BUTTON_PIN_Koel, INPUT);
pinMode(BUTTON_PIN_Vries, INPUT);
//Buzz pin
pinMode(buzzPin, OUTPUT);
digitalWrite(buzzPin, HIGH);
// Activate internal pull-up
digitalWrite(BUTTON_PIN_Koel, HIGH);
digitalWrite(BUTTON_PIN_Vries, HIGH);
// After setting up the button, setup debouncer
debouncerKoel.attach(BUTTON_PIN_Koel);
debouncerVries.attach(BUTTON_PIN_Vries);
debouncerKoel.interval(5);
debouncerVries.interval(5);
// Register binary input sensor to gw (they will be created as child devices)
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
gw.present(CHILD_ID_Koel, S_DOOR);
gw.present(CHILD_ID_Vries, S_DOOR);
}
// Check if digital input has changed and send in new value
void loop()
{
// Fridge door script
debouncerKoel.update();
// Get the update value
int valueKoel = debouncerKoel.read();
if (valueKoel != oldValueKoel) {
// Send in the new value
gw.send(msgKoel.set(valueKoel == HIGH ? 0 : 1)); // change this value to if NC then 0 : 1; if NO then 1 : 0
oldValueKoel = valueKoel;
KoelBuzz = digitalRead(BUTTON_PIN_Koel);
Serial.println(KoelBuzz);
if (KoelBuzz == LOW) { //Door is open.
KoelTimer = millis(); // Set the time.
Serial.println(KoelTimer);
}
}
// check how long the doors are opened
unsigned long currentMillisKoel = millis();
if ((currentMillisKoel - KoelTimer) >= DoorDelay && KoelBuzz == LOW) {
// Serial.println("Counter start");
// Serial.println(KoelTimer);
// Serial.println(currentMillisKoel);
digitalWrite(buzzPin, KoelBuzz); // Tone ON
// Serial.println("Buzzer triggerd");
}
// Freezer door script
debouncerVries.update();
// Get the update value
int valueVries = debouncerVries.read();
if (valueVries != oldValueVries) {
// Send in the new value
gw.send(msgVries.set(valueVries == HIGH ? 0 : 1)); // change this value to if NC then 0 : 1; if NO then 1 : 0
oldValueVries = valueVries;
VriesBuzz = digitalRead(BUTTON_PIN_Vries);
Serial.println(VriesBuzz);
if (VriesBuzz == LOW) { //Door is open.
VriesTimer = millis(); // Set the time.
Serial.println(VriesTimer);
}
}
if (VriesBuzz && KoelBuzz == HIGH) {
digitalWrite(buzzPin, VriesBuzz); // Tone OFF
}
// check how long the doors are opened
unsigned long currentMillisVries = millis();
if ((currentMillisVries - VriesTimer) >= DoorDelay && VriesBuzz == LOW) {
// Serial.println("Counter start");
// Serial.println(VriesTimer);
// Serial.println(currentMillisVries);
digitalWrite(buzzPin, VriesBuzz); // Tone ON
// Serial.println("Buzzer triggerd");
}
// Sleep until interrupt comes in on motion sensor. Send update every two minute.
// gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
// Serial.println("sleep");
}