Buzzer when door opened longer than X seconds
-
Hi All,
I have got the buzzer module today and I want to use it on my exist frigde door contact sensor. What I want to do is when one of the fridge door is open more than 30 seconds the buzzer will turn on. And when door is closed the buzz stops.
The full code 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 currentMillis = millis(); MySensor gw; Bounce debouncerKoel = Bounce(); Bounce debouncerVries = Bounce(); int oldValueKoel = -1; int oldValueVries = -1; int buzz = 0; unsigned long DoorDelay = 30000; unsigned long DoorMillis; 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() { 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; } 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; buzz = digitalRead(BUTTON_PIN_Vries); Serial.println(buzz); if (buzz == HIGH) { digitalWrite(buzzPin, buzz); // Tone OFF Serial.println("Buzzer off"); } if (buzz == LOW) { DoorMillis = millis(); Serial.println("Counter start"); Serial.println(DoorMillis); } if ((millis() - DoorMillis) > DoorDelay) { digitalWrite(buzzPin, buzz); // 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"); }
Below is part of the code where I tired to get the code work. I think I am doing something wrong.
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; buzz = digitalRead(BUTTON_PIN_Vries); Serial.println(buzz); if (buzz == HIGH) { digitalWrite(buzzPin, buzz); // Tone OFF Serial.println("Buzzer off"); } if (buzz == LOW) { DoorMillis = millis(); Serial.println("Counter start"); Serial.println(DoorMillis); } if ((millis() - DoorMillis) > DoorDelay) { digitalWrite(buzzPin, buzz); // Tone ON Serial.println("Buzzer triggerd"); }
Does anyone know how the code should look like
Thanks
-
Hi
I think your problem is where you have the code
if ((millis() - DoorMillis) > DoorDelay) { digitalWrite(buzzPin, buzz); // Tone ON Serial.println("Buzzer triggerd"); }
you have it nested inside the valueVries if statement which means it will only excecute when a change of state is detected. This means when the switch is first activated the timeout will not have expired and then it will not be retested until the switch state has changed, which will then have turned off.
you need to move that code to the main loop so it is continually tested and try something like
if ((millis() - DoorMillis) > DoorDelay && buzz == LOW) { digitalWrite(buzzPin, buzz); // Tone ON Serial.println("Buzzer triggerd"); }
-
@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"); }