Maximum of relais and Binary buttons on Nano?



  • I try to get 5 relais and 2 binary buttons activated on my nano and have them controlled by Domoticz.
    What I found was that everything is working 2 bin buttons and 3 relais but adding another relais (4) I loose one bin button and had 4 relais. adding another relay I lost my last bin button. How can I get it in place 5 relais and 2 binary button?
    here you see my script:

    
    #define MY_RADIO_NRF24
    #define MY_DEBUG
    #define MY_REPEATER_FEATURE
    #include <MySensors.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define CHILD_ID1 3
    #define BUTTON_PIN1  4  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID2 4
    #define BUTTON_PIN2  5  // Arduino Digital I/O pin for button/reed switch
    
    #define RELAY_1  A0  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 5 // 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
    
    Bounce debouncer1 = Bounce();
    Bounce debouncer2 = Bounce(); // debouncer for the second switch
    
    
    
    int oldValue1 = -1;
    int oldValue2 = -1; // second switch needs to have it's own old state
    
    MyMessage msg1(CHILD_ID1, V_TRIPPED), msg2(CHILD_ID2, V_TRIPPED);
    
    void before()
    {
      for (int sensor = 1, pin = RELAY_1; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);
        // Set relay to last known state (using eeprom storage)
        digitalWrite(pin, loadState(sensor) ? RELAY_ON : RELAY_OFF);
      }
    }
    
    void presentation()
    {
    
      sendSketchInfo("Serre", "1.0");
    
      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)
        present(sensor, S_BINARY);
      }
    
      pinMode(BUTTON_PIN1, INPUT_PULLUP ); // You can assign pinmode and use pullup in one statement.
      pinMode(BUTTON_PIN2, INPUT_PULLUP);
    
    
      debouncer1.attach(BUTTON_PIN1);
      debouncer1.interval(5);
      debouncer2.attach(BUTTON_PIN2);
      debouncer2.interval(5);
    
      present(CHILD_ID1, S_DOOR);
      present(CHILD_ID2, S_DOOR);
    }
    
    
    void loop()
    {
      // Check if the first switch state has changed
      debouncer1.update();
      // Get the update value
      int value = debouncer1.read();
    
      if (value != oldValue1) {
        // Send in the new value
        send(msg1.set(value == HIGH ? 1 : 0));
        oldValue1 = value;
      }
    
      debouncer2.update();
      // Get the update value
      value = debouncer2.read(); // no need to redeclare it (I removed int)
    
      if (value != oldValue2) {
        // Send in the new value
        send(msg2.set(value == HIGH ? 1 : 0)); // this is where you turn the second switch in your controller
        oldValue2 = value;
      }
    
    
    }
    
    void receive(const MyMessage &message)
    {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type == V_STATUS) {
        // Change relay state
        digitalWrite(message.sensor - 1 + RELAY_1, message.getBool() ? RELAY_ON : RELAY_OFF);
        // Store state in eeprom
        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());
      }
    }
    
    
    


  • @Dick Most likely your problems are related to the use of analog out pins for switching on your relay. Maybe your problem is solved by addressing pin 14 as the digital equivalent to A0 (see https://forum.arduino.cc/index.php?topic=147582.0).

    In case you have to use different PINs, you could use an array to store info about the used pins, see https://forum.mysensors.org/topic/4847/multi-button-relay-sketch/33# as a (more complex) example.



  • @rejoe2 Thanks for the links and I cange my code. The 5 relais are visibl, the 2 binary switches as wel ut the relais are not switching??? any idea



  • @Dick said in Maximum of relais and Binary buttons on Nano?:

    @rejoe2 Thanks for the links and I changed my code. The 5 relais are visible, the 2 binary switches as well but the relais are not switching??? any idea

    here my code for now:

    #define MOTION_SENSOR 3
    #define CHILD_ID_MOTION 10
    #define RELAY1_PIN  2  // Arduino Digital I/O pin number for relay alarm serre (groen)
    #define BUTTON1_PIN  14  // Arduino Digital I/O pin number for button with relay
    #define CHILD_ID_1 1   // Id of the sensor child
    #define RELAY2_PIN  4  // Arduino Digital I/O pin number for relay garden light serre (grijs)
    #define BUTTON2_PIN  15  // Arduino Digital I/O pin number for button with relay
    #define CHILD_ID_2 2   // Id of the sensor child
    #define RELAY3_PIN  5  // Arduino Digital I/O pin number for relay gordijn serre open (grijs)
    #define BUTTON3_PIN  16  // Arduino Digital I/O pin number for button with relay
    #define CHILD_ID_3 3   // Id of the sensor child
    #define RELAY6_PIN  6  // Arduino Digital I/O pin number for relay gordijn serre dicht (groen)
    #define BUTTON6_PIN  17  // Arduino Digital I/O pin number for button with relay
    #define CHILD_ID_6 12   // Id of the sensor child
    #define RELAY7_PIN  18  // Arduino Digital I/O pin number for relay gordijn serre stop (geel)
    #define BUTTON7_PIN  8  // Arduino Digital I/O pin number for button with relay
    #define CHILD_ID_7 11   // Id of the sensor child
    #define BUTTON4_PIN  19  // Arduino Digital I/O pin number for button without relay
    #define CHILD_ID_4 4   // Id of the sensor child
    #define BUTTON5_PIN  8  // Arduino Digital I/O pin number for button without relay
    #define CHILD_ID_5 5   // Id of the sensor child
    #define RELAY_ON 0
    #define RELAY_OFF 1
    
    #include <MySensors.h>
    #define MY_RADIO_NRF24
    #define MY_DEBUG
    #include <SPI.h>
    #include <Bounce2.h>
    
    
    
    Bounce debouncer1 = Bounce();
    int oldValue1 = 0;
    bool state1;
    Bounce debouncer2 = Bounce();
    int oldValue2 = 0;
    bool state2;
    Bounce debouncer3 = Bounce();
    int oldValue3 = 0;
    bool state3;
    Bounce debouncer4 = Bounce();
    int oldValue4 = 0;
    bool state4;
    Bounce debouncer5 = Bounce();
    int oldValue5 = 0;
    bool state5;
    Bounce debouncer6 = Bounce();
    int oldValue6 = 0;
    bool state6;
    Bounce debouncer7 = Bounce();
    int oldValue7 = 0;
    bool state7;
    
    Bounce debouncerMotion = Bounce();
    
    
    MyMessage msg1(CHILD_ID_1, V_LIGHT);
    MyMessage msg2(CHILD_ID_2, V_LIGHT);
    MyMessage msg3(CHILD_ID_3, V_LIGHT);
    MyMessage msg4(CHILD_ID_4, V_LIGHT);
    MyMessage msg5(CHILD_ID_5, V_LIGHT);
    MyMessage msg6(CHILD_ID_6, V_LIGHT);
    MyMessage msg7(CHILD_ID_7, V_LIGHT);
    
    unsigned long SLEEP_TIME = 6000;
    unsigned long blockMotionTimer = 0;
    
    
    
    MyMessage msgRelay1(CHILD_ID_1, V_LIGHT), msgRelay2(CHILD_ID_2, V_LIGHT), msgRelay3(CHILD_ID_3, V_LIGHT), msgButton4(CHILD_ID_4, V_TRIPPED), msgButton5(CHILD_ID_5, V_TRIPPED), msgMotion(CHILD_ID_MOTION, V_TRIPPED), msgRelay6(CHILD_ID_6, V_LIGHT), msgRelay7(CHILD_ID_7, V_LIGHT);
    
    void presentation()
    {
    
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay & Button & Motion", "1.0");
    
      // Setup the button
      pinMode(BUTTON1_PIN, INPUT);  // Activate internal pull-up
      digitalWrite(BUTTON1_PIN, HIGH);
      pinMode(BUTTON2_PIN, INPUT);
      digitalWrite(BUTTON2_PIN, HIGH);
      pinMode(BUTTON3_PIN, INPUT);
      digitalWrite(BUTTON3_PIN, HIGH);
      pinMode(BUTTON4_PIN, INPUT);
      digitalWrite(BUTTON4_PIN, HIGH);
      pinMode(BUTTON5_PIN, INPUT);
      digitalWrite(BUTTON5_PIN, HIGH);
      pinMode(MOTION_SENSOR, INPUT_PULLUP);
      pinMode(BUTTON6_PIN, INPUT);
      digitalWrite(BUTTON6_PIN, HIGH);
      pinMode(BUTTON7_PIN, INPUT);
      digitalWrite(BUTTON7_PIN, HIGH);
    
      // After setting up the button, setup debouncer
      debouncer1.attach(BUTTON1_PIN);
      debouncer1.interval(5);
      debouncer2.attach(BUTTON2_PIN);
      debouncer2.interval(5);
      debouncer3.attach(BUTTON3_PIN);
      debouncer3.interval(5);
      debouncer4.attach(BUTTON4_PIN);
      debouncer4.interval(5);
      debouncer5.attach(BUTTON5_PIN);
      debouncer5.interval(5);
      debouncer6.attach(BUTTON6_PIN);
      debouncer6.interval(5);
      debouncer7.attach(BUTTON7_PIN);
      debouncer7.interval(5);
    
      debouncerMotion.attach(MOTION_SENSOR);
      debouncerMotion.interval(400);
    
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_1, S_LIGHT);
      digitalWrite(RELAY1_PIN, RELAY_OFF);
      pinMode(RELAY1_PIN, OUTPUT);
    
      present(CHILD_ID_2, S_LIGHT);
      digitalWrite(RELAY2_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY2_PIN, OUTPUT);
    
      present(CHILD_ID_3, S_LIGHT);
      digitalWrite(RELAY3_PIN, RELAY_OFF);
      pinMode(RELAY3_PIN, OUTPUT);
    
      present(CHILD_ID_6, S_LIGHT);
      digitalWrite(RELAY6_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY6_PIN, OUTPUT);
    
      present(CHILD_ID_7, S_LIGHT);
      digitalWrite(RELAY7_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY7_PIN, OUTPUT);
    
    
      
      present(CHILD_ID_4, S_DOOR);
      
      present(CHILD_ID_5, S_DOOR);
      
      present(CHILD_ID_MOTION, S_MOTION, "Motion needs presentation", true);
    
      
      // Set relay to last known state (using eeprom storage)
      state1 = loadState(CHILD_ID_1);
      digitalWrite(RELAY1_PIN, state1 ? RELAY_ON : RELAY_OFF);
      state2 = loadState(CHILD_ID_2);
      digitalWrite(RELAY2_PIN, state2 ? RELAY_ON : RELAY_OFF);
      state3 = loadState(CHILD_ID_3);
      digitalWrite(RELAY3_PIN, state1 ? RELAY_ON : RELAY_OFF);
      state6 = loadState(CHILD_ID_6);
      digitalWrite(RELAY6_PIN, state6 ? RELAY_ON : RELAY_OFF);
      state7 = loadState(CHILD_ID_7);
      digitalWrite(RELAY7_PIN, state7 ? RELAY_ON : RELAY_OFF);
      
     }
    
    
    void loop()
    {
    
      debouncer1.update();
      // Get the update value
      int value1 = debouncer1.read();
      if (value1 != oldValue1 && value1 == 0) {
        send(msgRelay1.set(state1 ? false : true), true); // Send new state and request ack back
      }
      oldValue1 = value1;
    
      debouncer2.update();
      // Get the update value
      int value2 = debouncer2.read();
      if (value2 != oldValue2 && value2 == 0) {
        send(msgRelay2.set(state2 ? false : true), true); // Send new state and request ack back
      }
      oldValue2 = value2;
    
      debouncer3.update();
      // Get the update value
      int value3 = debouncer3.read();
      if (value3 != oldValue3 && value3 == 0) {
        send(msgRelay3.set(state3 ? false : true), true); // Send new state and request ack back
      }
      oldValue3 = value3;
    
      debouncer4.update();
      // Get the update value
      int value4 = debouncer4.read();
      if (value4 != oldValue4)
      {
        //Send in the new value
    
        send(msgButton4.set(value4 == HIGH ? 1 : 0));
        oldValue4 = value4;
      }
    
      debouncer6.update();
      // Get the update value
      int value6 = debouncer6.read();
      if (value6 != oldValue6 && value6 == 0) {
        send(msgRelay6.set(state6 ? false : true), true); // Send new state and request ack back
      }
      oldValue6 = value6;
    
      debouncer7.update();
      // Get the update value
      int value7 = debouncer7.read();
      if (value7 != oldValue7 && value7 == 0) {
        send(msgRelay7.set(state7 ? false : true), true); // Send new state and request ack back
      }
      oldValue7 = value7;
      
    
      debouncer5.update();
      // Get the update value
      int value5 = debouncer5.read();
      if (value5 != oldValue5)
      {
    
        send(msgButton5.set(value5 == HIGH ? 1 : 0));
        oldValue5 = value5;
      }
    
      if (blockMotionTimer == 0) {
        if (debouncerMotion.update()) {
          int valueMotion = debouncerMotion.read();
          Serial.println( "PIR " + (String)valueMotion );
          send( msgMotion.set( valueMotion ), true ); // Also it might need inverted value
          blockMotionTimer = (millis() + SLEEP_TIME);
        }
      } else {
        debouncerMotion.update(); // dummy update to prevent false trigger after timer expires
        if (blockMotionTimer < millis()) {
          blockMotionTimer = 0;
        }
      }
    
    }
    
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
        Serial.println("This is an ack from gateway");
      }
    
    
    
    
    if (message.type == V_LIGHT) {
        switch (message.sensor) {
          case CHILD_ID_1:
            // Change relay state
            state1 = message.getBool();
            digitalWrite(RELAY1_PIN, state1 ? RELAY_ON : RELAY_OFF);
            // Store state in eeprom
            saveState(CHILD_ID_1, state1);
            break;
          case CHILD_ID_2:
            state2 = message.getBool();
            digitalWrite(RELAY2_PIN, state2 ? RELAY_ON : RELAY_OFF);
            saveState(CHILD_ID_2, state2);
            break;
          case CHILD_ID_3:
            state3 = message.getBool();
            digitalWrite(RELAY3_PIN, state3 ? RELAY_ON : RELAY_OFF);
            saveState(CHILD_ID_3, state3);
            break;
          case CHILD_ID_6:
            state6 = message.getBool();
            digitalWrite(RELAY6_PIN, state6 ? RELAY_ON : RELAY_OFF);
            saveState(CHILD_ID_6, state6);
            break;
          case CHILD_ID_7:
            state7 = message.getBool();
            digitalWrite(RELAY7_PIN, state7 ? RELAY_ON : RELAY_OFF);
            saveState(CHILD_ID_7, state7);
            break;
          default: 
            Serial.print("Incoming sensor unknown");
        }
      }
    }```


  • @Dick Please review your code wrt to the usage of arrays. To me, this kind of code is really unreadable, use for-loops is much more straight forward. There are at least some points that seem not working for me:

    • A5+A6 are pure analog pins and may not be used as digital in/outputs
    • the things you are doing in case of a keypress are not clear to me. You are not switching anything, but sending a state[x], which seems never to be updated (EDIT: this part seems to be ok)

    Just in case you plan to have an equal number of relays and buttons, you may just use the sketch in the link above and adopt the values for the in- and outputs using just digital-usabel pins and you are done...



  • @rejoe2 I take a good look at my code and put comments in it. I play around. again thanks for the advise



  • @Dick Still try to get it working. What I have untill now, 5 working relais, where 2 can be activated with a button. Only the Binary Button will not work and is not visible in Domoticz, For this Binary Button I used the default script (https://www.mysensors.org/build/binary). If I can get this button work (better is 2 Binary Buttons, my project is finished. Who can help me!


Log in to reply
 

Suggested Topics

  • 4
  • 9
  • 2
  • 3
  • 5
  • 933

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts