Array Relay Button Actuator
-
That was it. After I amended sketch according to your advise all the random state changes disappeared. I rebooted node over 20 times in various button / relay states and it always started with relays in correct position. For anyone else interested below is amended sketch.
//MultiRelayButton Sketch, MySensors 1.6, toggle switch #define MY_RADIO_NRF24 #define MY_REPEATER_FEATURE #include <MySensor.h> #include <SPI.h> #include <Bounce2.h> #define RELAY_ON 0 // switch around for ACTIVE LOW / ACTIVE HIGH relay #define RELAY_OFF 1 // #define noRelays 2 //2-4 const int relayPin[] = {14,15}; // switch around pins to your desire const int buttonPin[] = {3,4}; // switch around pins to your desire class Relay // relay class, store all relevant data (equivalent to struct) { public: int buttonPin; // physical pin number of button int relayPin; // physical pin number of relay boolean relayState; // relay status (also stored in EEPROM) }; Relay Relays[noRelays]; Bounce debouncer[noRelays]; MyMessage msg[noRelays]; void setup(){ sendHeartbeat(); wait(100); // Initialize Relays with corresponding buttons for (int i = 0; i < noRelays; i++){ Relays[i].buttonPin = buttonPin[i]; // assign physical pins Relays[i].relayPin = relayPin[i]; msg[i].sensor = i; // initialize messages msg[i].type = V_LIGHT; pinMode(Relays[i].buttonPin, INPUT_PULLUP); wait(100); pinMode(Relays[i].relayPin, OUTPUT); Relays[i].relayState = loadState(i); // retrieve last values from EEPROM digitalWrite(Relays[i].relayPin, Relays[i].relayState? RELAY_ON:RELAY_OFF); // and set relays accordingly send(msg[i].set(Relays[i].relayState? true : false)); // make controller aware of last status wait(50); debouncer[i] = Bounce(); // initialize debouncer debouncer[i].attach(buttonPin[i]); debouncer[i].interval(20); wait(50); } } void presentation() { sendSketchInfo("MultiRelayButton", "0.9t"); wait(100); for (int i = 0; i < noRelays; i++) present(i, S_LIGHT); // present sensor to gateway wait(100); } void loop() { for (byte i = 0; i < noRelays; i++) { if (debouncer[i].update()) { Relays[i].relayState = !Relays[i].relayState; digitalWrite(Relays[i].relayPin, Relays[i].relayState?RELAY_ON:RELAY_OFF); send(msg[i].set(Relays[i].relayState? true : false)); // save sensor state in EEPROM (location == sensor number) saveState( i, Relays[i].relayState ); } } wait(20); } // process incoming message void receive(const MyMessage &message){ if (message.type == V_LIGHT){ if (message.sensor <noRelays){ // check if message is valid for relays..... previous line [[[ if (message.sensor <=noRelays){ ]]] Relays[message.sensor].relayState = message.getBool(); digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState? RELAY_ON:RELAY_OFF); // and set relays accordingly saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number) } } wait(20); } -
What is the simplest method to add two extra butons to this sketch not tied to the relay?
I have a four button device. Two will work with relays and two extra utilised in openhab rules -
Just in case someone else finds this thread and decides to use the sketch and runs in to the same issue I did.
I was using push buttons and the sketch above, when I pushed the button the relay turned on but as soon as I let go of the button the relay turned back off. I changed the sketch like this:
void loop() { for (byte i = 0; i < noRelays; i++) { if (debouncer[i].update()) { int value = debouncer[i].read(); if ( value == LOW) { Relays[i].relayState = !Relays[i].relayState; digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); send(msg[i].set(Relays[i].relayState ? true : false)); // save sensor state in EEPROM (location == sensor number) saveState( i, Relays[i].relayState ); } } } //wait(20); }Now the relay changes state as soon as I push the button and keeps the state until I push the button again.
Because my inputs for the push buttons are ground when pushed I used
if ( value == LOW)This way the relay would turn on immediately when I push the button and not wait until I let go of the button.
-
Just in case someone else finds this thread and decides to use the sketch and runs in to the same issue I did.
I was using push buttons and the sketch above, when I pushed the button the relay turned on but as soon as I let go of the button the relay turned back off. I changed the sketch like this:
void loop() { for (byte i = 0; i < noRelays; i++) { if (debouncer[i].update()) { int value = debouncer[i].read(); if ( value == LOW) { Relays[i].relayState = !Relays[i].relayState; digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); send(msg[i].set(Relays[i].relayState ? true : false)); // save sensor state in EEPROM (location == sensor number) saveState( i, Relays[i].relayState ); } } } //wait(20); }Now the relay changes state as soon as I push the button and keeps the state until I push the button again.
Because my inputs for the push buttons are ground when pushed I used
if ( value == LOW)This way the relay would turn on immediately when I push the button and not wait until I let go of the button.
@korttoma I used this sketch but my vera founds only 1 device and i have no idea why.
-
@korttoma I used this sketch but my vera founds only 1 device and i have no idea why.
@Стоян-Георгиев please post the complete sketch along with info about MySensors library version you are using.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login