#define MY_DEBUG
#define MY_GATEWAY_SERIAL
// Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
#if F_CPU == 8000000L
#define MY_BAUD_RATE 38400
#endif
// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
#define MY_INCLUSION_BUTTON_FEATURE
// Inverses behavior of inclusion button (if using external pullup)
//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
#define MY_INCLUSION_MODE_BUTTON_PIN 3
#include <SPI.h>
#include <MySensors.h>
#include <Bounce2.h>
// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay
#define noLightSwitch 10 //2-11
int relayPin[] = {2,3,4,5,6,7,8,9,10,11}; // switch around pins to your desire
int buttonPin[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9}; // switch around pins to your desire
Bounce debouncer[10];
MyMessage msg[10];
void before() {
//sendHeartbeat();
wait(100);
// Initialize Relays with corresponding buttons
for (int i = 0; i < noLightSwitch; i++){
// Setup the button.
pinMode(buttonPin[i], INPUT_PULLUP);
wait(100);
// Then set relay pins in output mode
pinMode(relayPin[i], OUTPUT);
wait(100);
digitalWrite(relayPin[i], RELAY_OFF);
msg[i].sensor = i; // initialize messages
msg[i].type = V_LIGHT;
//send(msg[i].set(loadState(i))); // make controller aware of last status
//wait(100);
// setup debouncer.
debouncer[i] = Bounce(); // initialize debouncer
debouncer[i].attach(buttonPin[i]);
debouncer[i].interval(20);
}
}
void setup() {
// Setup locally attached sensors
delay(100);
presentation();
}
boolean inputState;
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Relay", "1.0");
// Register all sensors to gw (they will be created as child devices)
for (int i = 0; i < noLightSwitch; i++){
present(buttonPin[i], S_LIGHT);
inputState =! digitalRead(buttonPin[i]);
send(msg[i].set(inputState));
}
wait(100);
}
void loop() {
// Send locally attached sensor data here
for (int i = 0; i < noLightSwitch; i++){
if (debouncer[i].update()) {
inputState =! digitalRead(buttonPin[i]);
send(msg[i].set(inputState));
}
wait(100);
}
}
void receive(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT) {
for (int i = 0; i < noLightSwitch; i++) {
if (i == message.sensor){
digitalWrite(relayPin[i], RELAY_ON);
wait(100);
digitalWrite(relayPin[i], RELAY_OFF);
wait(100);
inputState =! digitalRead(buttonPin[i]);
send(msg[i].set(inputState));
}
}
}
}