Take a look at this, works pretty good for me
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
// Enable repeater functionality for this node
//#define MY_REPEATER_FEATURE
#include <SPI.h>
#include <MySensors.h>
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
#define RELAY_1 1 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 18 // Total number of attached relays
#define SLEEP_BETWEEN_ALL_ON 100
#define SLEEP_BETWEEN_ALL_OFF 100
void before()
{
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Then set relay pins in output mode
//-----------pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
//-----------digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
}
}
void setup()
{
// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(8);
// Optional set pulse length.
// mySwitch.setPulseLength(320);
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("RF 433Mhz", "1.0");
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
present(sensor, S_BINARY);
}
}
void loop()
{
}
void receive(const MyMessage &message)
{
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_STATUS)
{
// Switch on:
// The first parameter represents the setting of the first rotary switch.
// In this example it's switched to "1" or "A" or "I".
//
// The second parameter represents the setting of the second rotary switch.
// In this example it's switched to "4" or "D" or "IV".
// 1 = channel A
// 2 = channel B
// 3 = channel C
// 4 = channel D
switch (message.sensor-1+RELAY_1)
{
case 1:
if(message.getBool() == 1){mySwitch.switchOn(1, 1);}else{mySwitch.switchOff(1, 1);}
break;
case 2:
if(message.getBool() == 1){mySwitch.switchOn(1, 2);}else{mySwitch.switchOff(1, 2);}
break;
case 3:
if(message.getBool() == 1){mySwitch.switchOn(1, 3);}else{mySwitch.switchOff(1, 3);}
break;
case 4:
if(message.getBool() == 1){mySwitch.switchOn(1, 4);}else{mySwitch.switchOff(1, 4);}
break;
case 5:
if(message.getBool() == 1){mySwitch.switchOn(2, 1);}else{mySwitch.switchOff(2, 1);}
break;
case 6:
if(message.getBool() == 1){mySwitch.switchOn(2, 2);}else{mySwitch.switchOff(2, 2);}
break;
case 7:
if(message.getBool() == 1){mySwitch.switchOn(2, 3);}else{mySwitch.switchOff(2, 3);}
break;
case 8:
if(message.getBool() == 1){mySwitch.switchOn(2, 4);}else{mySwitch.switchOff(2, 4);}
break;
case 9:
if(message.getBool() == 1){mySwitch.switchOn(3, 1);}else{mySwitch.switchOff(3, 1);}
break;
case 10:
if(message.getBool() == 1){mySwitch.switchOn(3, 2);}else{mySwitch.switchOff(3, 2);}
break;
case 11:
if(message.getBool() == 1){mySwitch.switchOn(3, 3);}else{mySwitch.switchOff(3, 3);}
break;
case 12:
if(message.getBool() == 1){mySwitch.switchOn(3, 4);}else{mySwitch.switchOff(3, 4);}
break;
case 13:
if(message.getBool() == 1){mySwitch.switchOn(4, 1);}else{mySwitch.switchOff(4, 1);}
break;
case 14:
if(message.getBool() == 1){mySwitch.switchOn(4, 2);}else{mySwitch.switchOff(4, 2);}
break;
case 15:
if(message.getBool() == 1){mySwitch.switchOn(4, 3);}else{mySwitch.switchOff(4, 3);}
break;
case 16:
if(message.getBool() == 1){mySwitch.switchOn(4, 4);}else{mySwitch.switchOff(4, 4);}
break;
// ALL ON
case 17:
mySwitch.switchOff(1, 1);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(1, 2);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(1, 3);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(1, 4);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(2, 1);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(2, 2);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(2, 3);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(2, 4);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(3, 1);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(3, 2);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(3, 3);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(3, 4);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(4, 1);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(4, 2);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(4, 3);
delay(SLEEP_BETWEEN_ALL_OFF);
mySwitch.switchOff(4, 4);
break;
// ALL OFF
case 18:
mySwitch.switchOn(1, 1);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(1, 2);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(1, 3);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(1, 4);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(2, 1);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(2, 2);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(2, 3);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(2, 4);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(3, 1);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(3, 2);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(3, 3);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(3, 4);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(4, 1);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(4, 2);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(4, 3);
delay(SLEEP_BETWEEN_ALL_ON);
mySwitch.switchOn(4, 4);
break;
default:
Serial.println("Unknown Status");
break;
}
saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for binary switch: ");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}