Hi,
Here is the example of the code using an 8 delay board.
I used the Analog devices as much as possible because I intend to include 1-wire temp sensors as well.
On my (clone) Mini there are 8 analog pins but useable on the bread board because of the location. I will use them in the final perfboard.
// Example sketch showing how to control physical relays.
// This example will remember relay state even after power failure.
// Using an Array to define the pin's
#include <MySensor.h>
#include <SPI.h>
const int RELAY[] = {A0, A1, A2, A3, A4, 6, 7, 8}; // I/O pins for the relays
#define NUMBER_OF_RELAYS 8 // Total number of attached relays
#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
MySensor gw;
void setup()
{
// Initialize library and add callback for incoming messages
gw.begin(incomingMessage, AUTO, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Relay", "1.0");
// Fetch relay status
for (int sensor=1, pin=0; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(RELAY[pin], OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(RELAY[pin], gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
}
}
void loop()
{
// Alway process incoming messages whenever possible
gw.process();
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT) {
// Change relay state
digitalWrite(RELAY[message.sensor-1], message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
gw.saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
"