This new one had the relays correct, but the buttons did not work. I compared the old with the new and below now works.
Thanks again a million for all the help
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#define RELAY_ON 0
#define RELAY_OFF 1
//
MySensor gw;
const int relayPin[] = {7, 8, A0, A1};
const int buttonPin[sizeof(relayPin) / sizeof(relayPin[0])] = {3, 4, 5, 6};
byte oldValue[sizeof(relayPin) / sizeof(relayPin[0])];
boolean relayState[sizeof(relayPin) / sizeof(relayPin[0])];
Bounce debouncer[sizeof(relayPin) / sizeof(relayPin[0])];
MyMessage msg[sizeof(relayPin) / sizeof(relayPin[0])];//(sensor,type);
void setup()
{
Serial.begin(115200);
gw.begin(incomingMessage, AUTO, false);
//
// or you can try:
// gw.begin(incomingMessage, <Your_Node_ID>, false); // where Your_Node_ID is a number from 1 to 254
//
delay(250);
gw.sendSketchInfo("MultiRelayButton", "0.9b");// <<<<<<<<<<<<<<<<<<< I forgot this, here
//
delay(250);
for (int i = 0; i < sizeof(relayPin) / sizeof(relayPin[0]); i++)
{
msg[i].sensor = i;
msg[i].type = V_LIGHT;
debouncer[i] = Bounce();
debouncer[i].attach(buttonPin[i]);
debouncer[i].interval(5);
pinMode(buttonPin[i], INPUT_PULLUP);
digitalWrite(relayPin[i], LOW);
pinMode(relayPin[i], OUTPUT);
gw.present(i, S_LIGHT); // <<<<<<<<<<<<<<<<<<<<<<< I fixed this too...
delay(250);
}
//retreive from EEPROM last states
for (int i = 0; i < sizeof(relayPin) / sizeof(relayPin[0]); i++)
{
relayState[i] = gw.loadState(i);
digitalWrite(relayPin[i], relayState[i]? RELAY_ON : RELAY_OFF);
gw.send(msg[i].set(relayState[i]? true : false), true);
delay(250);
}
}
//
void loop()
{
gw.process();
for (byte i = 0; i < sizeof(relayPin) / sizeof(relayPin[0]); i++)
{
debouncer[i].update();
byte value = debouncer[i].read();
if (value != oldValue[i] && value == 0)
{
relayState[i] = !relayState[i];
digitalWrite(relayPin[i], relayState[i]);
gw.send(msg[i].set(relayState[i]? true : false), true);
}
oldValue[i] = value;
}
}
//
void incomingMessage(const MyMessage &message)
{
if (message.isAck())
{
Serial.println(F("This is an ack from gateway"));
}
for (byte i = 0; i< sizeof(relayPin) / sizeof(relayPin[0]); i++)
{
if (message.sensor == i)
{
if (message.type == V_LIGHT)
{
relayState[i] = message.getBool();
digitalWrite(relayPin[i], relayState[i]? RELAY_ON : RELAY_OFF);
gw.saveState(relayPin[i], relayState[i]);
}
}
}
}