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