monostable button with relay



  • Hi Guys,
    i m pulling my hair over this odd issue: everything seems to look ok apart from when i press button A it switch on relay connected to pin 7 where as I should switch on relay connected to pin 6 and when i press button B it switch on relay connected to 8 instead of relay connected to pin 7. What i m doing wrong here? Any help appreciated.

    following sketch came from here: https://forum.mysensors.org/topic/4847/multi-button-relay-sketch/37
    By: pepson 1 Nov 2016, 16:36 - Switch monostable:

    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Set LOW transmit power level as default, if you have an amplified NRF-module and
    // power your radio separately with a good regulator you can turn up PA level.
    // #define MY_RF24_PA_LEVEL RF24_PA_LOW
    
    #define MY_RF24_PA_LEVEL RF24_PA_MAX
    //#define MY_DEBUG_VERBOSE_RF24
    // RF channel for the sensor net, 0-127
    
    #define RF24_CHANNEL     125
    //RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
    #define RF24_DATARATE 	   RF24_250KBPS
    
    // Enabled repeater feature for this node
    //#define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    //PiHome Node ID
    #define MY_NODE_ID 40
    
    // Define 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
    
    // Define Sensor ID's
    #define SSR_A_ID 1   // Id of the sensor child
    #define SSR_B_ID 2   // Id of the sensor child
    #define SSR_C_ID 3   // Id of the sensor child
    #define SSR_D_ID 4   // Id of the sensor child
    
    // Define buttons and relays
    const int buttonPinA = 2;
    const int buttonPinB = 3;
    const int buttonPinC = 4;
    const int buttonPinD = 5;
    
    const int relayPinA = 6;
    const int relayPinB = 7;
    const int relayPinC = 8;
    
    // Define Variables
    int oldValueA = 0;
    int oldValueB = 0;
    int oldValueC = 0;
    int oldValueD = 0;
    
    bool stateA = false;
    bool stateB = false;
    bool stateC = false;
    bool stateD = false;
    
    int trigger = 0;
    
    Bounce debouncerA = Bounce();
    Bounce debouncerB = Bounce();
    Bounce debouncerC = Bounce();
    Bounce debouncerD = Bounce();
    
    MyMessage msgA(SSR_A_ID, V_STATUS);
    MyMessage msgB(SSR_B_ID, V_STATUS);
    MyMessage msgC(SSR_C_ID, V_STATUS);
    MyMessage msgD(SSR_D_ID, V_STATUS);
    
    void setup()
    {
    	pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
    	pinMode(buttonPinB, INPUT_PULLUP); // Setup the button Activate internal pull-up
    	pinMode(buttonPinC, INPUT_PULLUP); // Setup the button Activate internal pull-up
    	pinMode(buttonPinD, INPUT_PULLUP); // Setup the button Activate internal pull-up
    	
    	// After setting up the buttons, setup debouncer
    	debouncerA.attach(buttonPinA);
    	debouncerA.interval(5);
    	debouncerB.attach(buttonPinB);
    	debouncerB.interval(5);
    	debouncerC.attach(buttonPinC);
    	debouncerC.interval(5);
    	debouncerD.attach(buttonPinD);
    	debouncerD.interval(5);
    	
    	// Make sure relays are off when starting up
    	digitalWrite(relayPinA, RELAY_OFF);
    	digitalWrite(relayPinB, RELAY_OFF);
    	digitalWrite(relayPinC, RELAY_OFF);
    	
    	// Then set relay pins in output mode
    	pinMode(relayPinA, OUTPUT);
    	pinMode(relayPinB, OUTPUT);
    	pinMode(relayPinC, OUTPUT);
    
    }
    
    void presentation()  {
    	// Send the sketch version information to the gateway and Controller
    	//sendSketchInfo("2xRelay with monostable", "1.0");
    	
    	sendSketchInfo("Button Console", "1.3");
    	// Register all sensors to gw (they will be created as child devices)
    	present(SSR_A_ID, S_LIGHT);
    	present(SSR_B_ID, S_LIGHT);
    	present(SSR_C_ID, S_LIGHT);
    	present(SSR_D_ID, S_LIGHT);
    }
    
    //Example on how to asynchronously check for new messages from gw
    void loop()
    {
    	if (trigger == 0){
    		send(msgA.set(false)); // Send off state for relayA to ensure controller knows the switch is off
    		send(msgB.set(false)); // Send off state for relayB to ensure controller knows the switch is off
    		send(msgC.set(false)); // Send off state for relayB to ensure controller knows the switch is off
    		send(msgD.set(false)); // Send off state for relayB to ensure controller knows the switch is off
    		trigger = 1;
    	}
    	//Button One
    	debouncerA.update();
    	// Get the update value
    	int valueA = debouncerA.read();
    	if (valueA != oldValueA && valueA == 0) {
    		send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
    	}
    	oldValueA = valueA;
    	
    	//Button Two
    	debouncerB.update();
    	// Get the update value
    	int valueB = debouncerB.read();
    	if (valueB != oldValueB && valueB == 0) {
    		send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
    	}
    	oldValueB = valueB;
    	
    	//Button Three
    	debouncerC.update();
    	// Get the update value
    	int valueC = debouncerC.read();
    	if (valueC != oldValueC && valueC == 0) {
    		send(msgC.set(stateC ? false : true), true); // Send new state and request ack back
    	}
    	oldValueC = valueC; 
    	
    	//Button Four
    	debouncerD.update();
    	// Get the update value
    	int valueD = debouncerD.read();
    	if (valueD != oldValueD && valueD == 0) {
    		send(msgD.set(stateD ? false : true), true); // Send new state and request ack back
    	}
    	oldValueD = valueD;
    }
    
    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 (message.sensor) {
    		case 1:
    			stateA = message.getBool();
    			digitalWrite(message.sensor + 4, stateA ? RELAY_ON : RELAY_OFF);
    			break;
    		case 2:
    			stateB = message.getBool();
    			digitalWrite(message.sensor + 4, stateB ? RELAY_ON : RELAY_OFF);
    			break;
    		case 3:
    			stateC = message.getBool();
    			digitalWrite(message.sensor + 4, stateC ? RELAY_ON : RELAY_OFF);
    			break;
    		}
       
    /*      // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.println(message.sensor);
        Serial.print("from node:");
        Serial.println(message.sender);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
    	*/
    	}
    }
    


  • @pihome
    I am not able to fully trace the sketch but it seems that in receive method you are adding 4 to sensor id. That would mean that three message received would activate pin 5, 6 and 7. But your relays are connected to pins 6, 7 and 8. Please change message.sensor + 4 to message.sensor +5 and try to run it



  • thank you, that worked 👍


Log in to reply
 

Suggested Topics

25
Online

11.2k
Users

11.1k
Topics

112.5k
Posts