I have been using this sketch for about a week now and it is working fine for my needs.
// Simple binary switch example
// Connect button or door/window reed switch between
// digitial I/O pin 3 (ZONE_1 below) and GND.
#include <Sensor.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Bounce2.h>
#define ZONE_1 14 // Arduino Digital I/O pin for button/reed switch
#define NUMBER_OF_ZONES 6
Sensor gw;
Bounce debouncer_1 = Bounce();
Bounce debouncer_2 = Bounce();
Bounce debouncer_3 = Bounce();
Bounce debouncer_4 = Bounce();
Bounce debouncer_5 = Bounce();
Bounce debouncer_6 = Bounce();
int oldValue_1 =-1;
int oldValue_2 =-1;
int oldValue_3 =-1;
int oldValue_4 =-1;
int oldValue_5 =-1;
int oldValue_6 =-1;
void setup()
{
gw.begin();
for (int i=0; i<NUMBER_OF_ZONES;i++) {
// Setup the button
pinMode(ZONE_1+i,INPUT);
// Activate internal pull-up
digitalWrite(ZONE_1+i,HIGH);
// After setting up the button, setup debouncer
switch (1+i) {
case 1:
debouncer_1.attach(ZONE_1);
debouncer_1.interval(5);
break;
case 2:
debouncer_2.attach(ZONE_1+i);
debouncer_2.interval(5);
break;
case 3:
debouncer_3.attach(ZONE_1+i);
debouncer_3.interval(5);
break;
case 4:
debouncer_4.attach(ZONE_1+i);
debouncer_4.interval(5);
break;
case 5:
debouncer_5.attach(ZONE_1+i);
debouncer_5.interval(5);
break;
}
// 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 below.
gw.sendSensorPresentation(ZONE_1+i, S_DOOR);
delay(1000);
}
}
// Check if digital input has changed and send in new value
void loop()
{
for (int i=0; i<NUMBER_OF_ZONES;i++) {
int num = 1+i;
// Get the update value
switch (num) {
case 1:
{
debouncer_1.update();
int value_1 = debouncer_1.read();
if (value_1 != oldValue_1) {
// Send in the new value
gw.sendVariable(ZONE_1+i, V_TRIPPED, value_1==HIGH ? "1" : "0"); // Change to V_LIGHT if you use S_LIGHT in presentation above
oldValue_1 = value_1;
}
break;
}
case 2:
{
debouncer_2.update();
int value_2 = debouncer_2.read();
if (value_2 != oldValue_2) {
// Send in the new value
gw.sendVariable(ZONE_1+i, V_TRIPPED, value_2==HIGH ? "1" : "0"); // Change to V_LIGHT if you use S_LIGHT in presentation above
oldValue_2 = value_2;
}
break;
}
case 3:
{
debouncer_3.update();
int value_3 = debouncer_3.read();
if (value_3 != oldValue_3) {
// Send in the new value
gw.sendVariable(ZONE_1+i, V_TRIPPED, value_3==HIGH ? "1" : "0"); // Change to V_LIGHT if you use S_LIGHT in presentation above
oldValue_3 = value_3;
}
break;
}
case 4:
{
debouncer_4.update();
int value_4 = debouncer_4.read();
if (value_4 != oldValue_4) {
// Send in the new value
gw.sendVariable(ZONE_1+i, V_TRIPPED, value_4==HIGH ? "1" : "0"); // Change to V_LIGHT if you use S_LIGHT in presentation above
oldValue_4 = value_4;
}
break;
}
case 5:
{
debouncer_5.update();
int value_5 = debouncer_5.read();
if (value_5 != oldValue_5) {
// Send in the new value
gw.sendVariable(ZONE_1+i, V_TRIPPED, value_5==HIGH ? "1" : "0"); // Change to V_LIGHT if you use S_LIGHT in presentation above
oldValue_5 = value_5;
}
break;
}
case 6:
{
debouncer_6.update();
int value_6 = debouncer_6.read();
if (value_6 != oldValue_6) {
// Send in the new value
gw.sendVariable(ZONE_1+i, V_TRIPPED, value_6==HIGH ? "1" : "0"); // Change to V_LIGHT if you use S_LIGHT in presentation above
oldValue_6 = value_6;
}
break;
}
}
}
}