@jeylites
to turn relay off automatically after two minutes (and send the updated "off status" to controller... try something like this untested code:
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#define NUMBER_OF_REEDS 3
const int relayPin = 3;
const int reedPin[NUMBER_OF_REEDS] = {4, 5, 6};
int reedState[NUMBER_OF_REEDS];
Bounce debouncer[NUMBER_OF_REEDS] = Bounce();
unsigned long relayTimer;
MySensor gw;
MyMessage msg[NUMBER_OF_REEDS];
MyMessage relayMessage(0,V_LIGHT);
void setup()
{
gw.begin(incomingMessage, AUTO, true);
gw.sendSketchInfo("Test", "1.0a");
gw.present(0, S_LIGHT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, gw.loadState(0));
for (int i = 0; i < NUMBER_OF_REEDS; i++)
{
debouncer[i].attach(reedPin[i]);
debouncer[i].interval(5);
pinMode(reedPin[i], INPUT_PULLUP);
gw.present(i, S_DOOR);
gw.wait(250);
}
}
void loop()
{
gw.process();
for (int i = 0; i < NUMBER_OF_REEDS; i++)
{
debouncer[i].update();
int value = debouncer[i].read();
if (value != reedState[i])
{
gw.send(msg[i+1].set(value), false); // device number mismatch here because of the array
reedState[i] = value;
Serial.print("updating state for switch: ");
Serial.print(reedPin[i]);
Serial.print(" state: ");
Serial.println(reedState[i]);
}
}
if (millis() - relayTimer > 2 * 60 * 1000UL && digitalRead(relayPin)) // two minute timer
{
digitalWrite(relayPin, LOW);
gw.saveState(0, false);
gw.send(relayMessage.set(false), true);
}
}
void incomingMessage(const MyMessage &message)
{
if (message.type == V_LIGHT)
{
int newState = message.getBool();
digitalWrite(relayPin, newState);
gw.saveState(0, newState);
if (newState)
{
relayTimer = millis();
}
Serial.print(F("Incoming change for relay, New state: "));
Serial.println(newState);
}
}