Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Hardware
  3. Multiple switch inputs

Multiple switch inputs

Scheduled Pinned Locked Moved Hardware
13 Posts 4 Posters 3.4k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • rejoe2R Offline
    rejoe2R Offline
    rejoe2
    wrote on last edited by
    #4

    Just concentrate on the looping over arrays logic, then it's not that hard to break the link between the buttons and the relays. Did this myself here, despite having a lot of other stuff done there maybe this helps ;-)

    Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

    1 Reply Last reply
    0
    • P Offline
      P Offline
      PDP8
      wrote on last edited by
      #5

      @rejoe2 said in Multiple switch inputs:

      Thanks, I will dive into the code then :-)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PDP8
        wrote on last edited by
        #6

        Well to make three inputs I've come to this using the code you mentioned...
        I've removed the invert function because I could get Domotics invert the
        monitoring. And I've added an initial read at startup. Because at powering-up
        the switches were not read. Any comments? Regards!

        
        // Enable debug prints to serial monitor
        #define MY_DEBUG
        
        // Enable serial gateway
        #define MY_GATEWAY_SERIAL
        
        #include <SPI.h>
        #include <MySensors.h>
        #include <Bounce2.h>
        
        // Define Sensor ID's
        #define KNOB_A_ID 1   // Id of the sensor child
        #define KNOB_B_ID 2   // Id of the sensor child
        #define KNOB_C_ID 3   // Id of the sensor child
        
        
        // Define buttons
        const int buttonPinA = 10;
        const int buttonPinB = 11;
        const int buttonPinC = 12;
        
        // Define Variables
        int oldValueA = 0;
        int oldValueB = 0;
        int oldValueC = 0;
        bool stateA = false;
        bool stateB = false;
        bool stateC = false;
        int trigger = 0;
        
        
        Bounce debouncerA = Bounce();
        Bounce debouncerB = Bounce();
        Bounce debouncerC = Bounce();
        
        MyMessage msgA(KNOB_A_ID, V_STATUS);
        MyMessage msgB(KNOB_B_ID, V_STATUS);
        MyMessage msgC(KNOB_C_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
        
        
          // 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);
        
        
        
            /*--------------------- Added these lines for toggle switch-------------------------*/
          oldValueA = digitalRead(buttonPinA);  // set oldValueA to the current status of the toggle switch
          oldValueB = digitalRead(buttonPinB);  // set oldValueB to the current status of the toggle switch
          oldValueC = digitalRead(buttonPinC);  // set oldValueC to the current status of the toggle switch  
         
        }
        
        void presentation()  {
          // Send the sketch version information to the gateway and Controller
          sendSketchInfo("2x bistable button", "1.1");
        
          // Register all sensors to gw (they will be created as child devices)
          present(KNOB_A_ID, S_LIGHT);
          present(KNOB_B_ID, S_LIGHT);
          present(KNOB_C_ID, S_LIGHT);  
        
        }
        
        
        
        void loop()
        {
          if (trigger == 0)
            {
              debouncerA.update(); // Get the startup valueA
              int valueA = debouncerA.read();
              send(msgA.set(valueA==HIGH ? 0 : 1));
        
              debouncerB.update(); // Get the startup valueB
              int valueB = debouncerB.read();
              send(msgB.set(valueB==HIGH ? 0 : 1));
        
              debouncerC.update(); // Get the startup valueC
              int valueC = debouncerC.read();
              send(msgC.set(valueC==HIGH ? 0 : 1));
        
              trigger = 1;
            }
        
        
        
          debouncerA.update();
          // Get the update valueA
          int valueA = debouncerA.read();
         
          if (valueA != oldValueA) {
             // Send in the new valueA
             send(msgA.set(valueA==HIGH ? 0 : 1));
             oldValueA = valueA;
          }
        
          debouncerB.update();
          // Get the update valueB
          int valueB = debouncerB.read();
         
          if (valueB != oldValueB) {
             // Send in the new valueB
             send(msgB.set(valueB==HIGH ? 0 : 1));
             oldValueB = valueB;
          }
        
          debouncerC.update();
          // Get the update valueC
          int valueC = debouncerC.read();
         
          if (valueC != oldValueC) {
             // Send in the new valueC
             send(msgC.set(valueC==HIGH ? 0 : 1));
             oldValueC = valueC;
          }  
        
        }
        
        
        1 Reply Last reply
        0
        • rejoe2R Offline
          rejoe2R Offline
          rejoe2
          wrote on last edited by
          #7

          Seems to be a good starting point, but some remarks:

          • Using PINs 10-12 might not be the best choice in case you want to add a radio later (my code was for RS485@HWSerial)
          • Doing this kind of task using arrays gives much more flexibility and easier reading imho

          So essential Part of the sketch could be reduced to something like (untested, changing PINs recommended):

          ...
          #include <Bounce2.h>
          
          #define FIRST_PIR_ID 1
          #define MAX_PIRS 3 
          const uint8_t pirPin[] = {10, 11,12};   //  switch around pins to your desire
          Bounce debouncer[MAX_PIRS];
          MyMessage pirMsg(0, V_TRIPPED);
          bool oldPir[MAX_PIRS] = {false};
          
          void before()
          {
          }
          
          void presentation() {
            // Send the sketch version information to the gateway and Controller
            sendSketchInfo(SN, SV);
            // Fetch the number of attached temperature sensors
          
            for (int i = 0; i < MAX_PIRS; i++) { //i < numSensors &&
              present(FIRST_PIR_ID + i, S_MOTION);
            }
          }
          
          void setup()
          {
            for (uint8_t i = 0; i < MAX_PIRS; i++) {
              debouncer[i] = Bounce();                        // initialize debouncer
              debouncer[i].attach(pirPin[i], INPUT_PULLUP);
              debouncer[i].interval(5);
              oldPir[i] =  debouncer[i].read();
            }
          }
          
          void loop() {
            
            bool pir[MAX_PIRS];
            for (uint8_t i = 0; i < MAX_PIRS; i++) {
              debouncer[i].update();
              pir[i] = debouncer[i].read();
              if (pir[i] != oldPir[i]) {
                send(pirMsg.setSensor(FIRST_PIR_ID + i).set( pir[i])); // Send tripped value to gw
                oldPir[i] = pir[i];
              }
            } 
          }```

          Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

          1 Reply Last reply
          0
          • P Offline
            P Offline
            PDP8
            wrote on last edited by
            #8

            Thanks for the comment, I will look at it.

            Another question, the inputs look exactly like an output in domoticz.
            I can even switch on the lamp with the mouse. Of course there is nothing happening
            because it is an input. But it is a bit confusing. Is this a Domoticz setting? Or can I change
            something in the MySensors code for this? Thanks!

            0_1532950762825_inputs and outputs.png

            rejoe2R 1 Reply Last reply
            0
            • P PDP8

              Thanks for the comment, I will look at it.

              Another question, the inputs look exactly like an output in domoticz.
              I can even switch on the lamp with the mouse. Of course there is nothing happening
              because it is an input. But it is a bit confusing. Is this a Domoticz setting? Or can I change
              something in the MySensors code for this? Thanks!

              0_1532950762825_inputs and outputs.png

              rejoe2R Offline
              rejoe2R Offline
              rejoe2
              wrote on last edited by
              #9

              @pdp8 Despite not beeing familiar with domoticz, this behaviour seems to be logic to me: You presentet a light device, so this typiclly is switchable from controller side.
              You might either change the handling within domiticz or present a different Child Type (my code uses motion).

              Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

              1 Reply Last reply
              0
              • P Offline
                P Offline
                PDP8
                wrote on last edited by
                #10

                Hmm, I've changed it to S_DOOR now. But I still can turn it on and off in Domoticz.
                I guess this is a Domoticz thing and has nothing to do with MySensors... Weird...

                T 1 Reply Last reply
                0
                • zrom69Z Offline
                  zrom69Z Offline
                  zrom69
                  wrote on last edited by
                  #11

                  it's normal with domoticz same motion you can do it off
                  you have to protect how it works you can not operate it anymore

                  1 Reply Last reply
                  0
                  • P PDP8

                    Hmm, I've changed it to S_DOOR now. But I still can turn it on and off in Domoticz.
                    I guess this is a Domoticz thing and has nothing to do with MySensors... Weird...

                    T Offline
                    T Offline
                    tsjoender
                    wrote on last edited by
                    #12

                    @pdp8 By default Domoticz will add a S_BINARY, S_DOOR, S_MOTION and S_DIMMER (and likely a few others) device in the form of a switchable lamp. You can add it to the active devices and then change the type to be a different device.

                    For example when you create a S_MOTION device, it initially gets added as a lamp in Domoticz, but then you can later change the switch type to be a motion sensor. This changes the icon displayed in Domoticz and also stops you from changing the state.

                    1 Reply Last reply
                    0
                    • zrom69Z Offline
                      zrom69Z Offline
                      zrom69
                      wrote on last edited by
                      #13

                      yes you can change
                      you go on switch press edit after a window that opens
                      you go to Switch Type and you find motion
                      safeguard

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      23

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.1k

                      Posts


                      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular