well.. i guess not..... It seems that its working for a short while. after that 1 of the relays does not respond anymore, when i set on relay on, both of the relays are going on? any help where my sketch is wrong?
Floris
// Example sketch showing how to control physical relays.
// This example will remember relay state even after power failure.
#include <MySensor.h>
#include <SPI.h>
#include <DistanceGP2Y0A21YK.h>
#define CHILD_ID_DISTANCE 100
DistanceGP2Y0A21YK Dist;
#define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 2 // Total number of attached relays
#define RELAY_ON 0 // GPIO value to write to turn on attached relay
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay
MySensor gw;
int numSensors=0;
boolean receivedConfig = true;
boolean metric = true;
int distance;
int lastDistance=0;
int loopCounter=0;
MyMessage msg(1,V_LIGHT);
MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);
void setup()
{
Dist.begin(0);
// 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("RelayDistanceRepeaterFloris", "1.0");
// Fetch relay status
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)
gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
boolean savedState = gw.loadState(sensor);
digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
gw.send(msg.set(savedState? 1 : 0));
}
}
void loop()
{
// Alway process incoming messages whenever possible
gw.process();
static unsigned long lastTime = millis();
if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds
{
distance = Dist.getDistanceCentimeter();
if (distance != lastDistance)
{
lastDistance = distance;
gw.send(msgdist.set(distance));
}
lastTime = millis();
}
}
void incomingMessage(const MyMessage &message) {
if (message.type==V_LIGHT) {
boolean relayState = message.getBool();
digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF);
gw.saveState(message.sensor, relayState);
gw.send(msg.set(relayState? 1 : 0));
}
}