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. Troubleshooting
  3. Multiple Binary Switch Help

Multiple Binary Switch Help

Scheduled Pinned Locked Moved Troubleshooting
3 Posts 2 Posters 1.7k Views 1 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.
  • P Offline
    P Offline
    punter9
    wrote on last edited by
    #1

    Hey all I am learning here and running into some issues. I have 3 binary switches I would like to have read for me. I have written code and they all show up as I would like. The issues I am having is only pin D5 seems to do anything and it turns ON/OFF all 3 switches. Any help as to why this is would really be appreciated.

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define CHILD_ID1 3
    #define BUTTON_PIN1  3  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID2 4
    #define BUTTON_PIN2  4  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID3 5
    #define BUTTON_PIN3  5  // Arduino Digital I/O pin for button/reed switch
    
    
    
    MySensor gw;
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg1(CHILD_ID1,V_TRIPPED), msg2(CHILD_ID2,V_TRIPPED),msg3(CHILD_ID3,V_TRIPPED);
    
    
    
    void setup()  
    {  
      gw.begin();
    
     // Setup the button
      pinMode(BUTTON_PIN1,INPUT);
      pinMode(BUTTON_PIN2,INPUT);
      pinMode(BUTTON_PIN3,INPUT);
      
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN1,HIGH);
      digitalWrite(BUTTON_PIN2,HIGH);
      digitalWrite(BUTTON_PIN3,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN1);
      debouncer.interval(5);
      debouncer.attach(BUTTON_PIN2);
      debouncer.interval(5);
      debouncer.attach(BUTTON_PIN3);
      debouncer.interval(5);
    
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      gw.present(CHILD_ID1, S_DOOR);
      gw.present(CHILD_ID2, S_DOOR);
      gw.present(CHILD_ID3, S_DOOR);  
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
     
      if (value != oldValue) {
         // Send in the new value
         gw.send(msg1.set(value==HIGH ? 1 : 0));
         gw.send(msg2.set(value==HIGH ? 1 : 0));
         gw.send(msg3.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
    } 
    
    
    TheoLT 1 Reply Last reply
    0
    • P Offline
      P Offline
      punter9
      wrote on last edited by
      #2

      did this, works! on to adding a couple soil moisture sensors and a relay

      /**
       * The MySensors Arduino library handles the wireless radio link and protocol
       * between your home built sensors/actuators and HA controller of choice.
       * The sensors forms a self healing radio network with optional repeaters. Each
       * repeater and gateway builds a routing tables in EEPROM which keeps track of the
       * network topology allowing messages to be routed to nodes.
       *
       * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
       * Copyright (C) 2013-2015 Sensnology AB
       * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
       *
       * Documentation: http://www.mysensors.org
       * Support Forum: http://forum.mysensors.org
       *
       * This program is free software; you can redistribute it and/or
       * modify it under the terms of the GNU General Public License
       * version 2 as published by the Free Software Foundation.
       *
       *******************************
       *
       * DESCRIPTION
       *
       * Simple binary switch example 
       * Connect button or door/window reed switch between 
       * digitial I/O pin 3 (BUTTON_PIN below) and GND.
       * http://www.mysensors.org/build/binary
       */
      
      
      #include <MySensor.h>
      #include <SPI.h>
      #include <Bounce2.h>
      
      #define CHILD_ID1 3
      #define BUTTON_PIN1  3  // Arduino Digital I/O pin for button/reed switch
      #define CHILD_ID2 4
      #define BUTTON_PIN2  4  // Arduino Digital I/O pin for button/reed switch
      #define CHILD_ID3 5
      #define BUTTON_PIN3  5  // Arduino Digital I/O pin for button/reed switch
      
      
      
      MySensor gw;
      Bounce debouncer = Bounce(); 
      int oldValue1=-1;
      int oldValue2=-1;
      int oldValue3=-1;
      
      // Change to V_LIGHT if you use S_LIGHT in presentation below
      MyMessage msg1(CHILD_ID1,V_TRIPPED), msg2(CHILD_ID2,V_TRIPPED),msg3(CHILD_ID3,V_TRIPPED);
      
      
      
      void setup()  
      {  
        gw.begin();
      
       // Setup the button
        pinMode(BUTTON_PIN1,INPUT);
        pinMode(BUTTON_PIN2,INPUT);
        pinMode(BUTTON_PIN3,INPUT);
        
        // Activate internal pull-up
        digitalWrite(BUTTON_PIN1,HIGH);
        digitalWrite(BUTTON_PIN2,HIGH);
        digitalWrite(BUTTON_PIN3,HIGH);
        
        // After setting up the button, setup debouncer
        debouncer.attach(BUTTON_PIN1);
        debouncer.interval(5);
        debouncer.attach(BUTTON_PIN2);
        debouncer.interval(5);
        debouncer.attach(BUTTON_PIN3);
        debouncer.interval(5);
      
        // Register binary input sensor to gw (they will be created as child devices)
        // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
        // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
        gw.present(CHILD_ID1, S_DOOR);
        gw.present(CHILD_ID2, S_DOOR);
        gw.present(CHILD_ID3, S_DOOR);  
      }
      
      
      //  Check if digital input has changed and send in new value
      void loop() 
      {
        debouncer.update();
        // Get the update value
        int value1 = debouncer.read();
        int value2 = debouncer.read();
        int value3 = debouncer.read();
      
        value1 = digitalRead(BUTTON_PIN1);
        value2 = digitalRead(BUTTON_PIN2);
        value3 = digitalRead(BUTTON_PIN3);
       
        if (value1 != oldValue1) {
           // Send in the new value
           gw.send(msg1.set(value1==HIGH ? 1 : 0));
           oldValue1 = value1;
        }
        if (value2 != oldValue2) {
           // Send in the new value
           gw.send(msg2.set(value2==HIGH ? 1 : 0));
           oldValue2 = value2;
        }
        if (value3 != oldValue3) {
           // Send in the new value
           gw.send(msg3.set(value3==HIGH ? 1 : 0));
           oldValue3 = value3;
        }
      } 
      
      
      1 Reply Last reply
      0
      • P punter9

        Hey all I am learning here and running into some issues. I have 3 binary switches I would like to have read for me. I have written code and they all show up as I would like. The issues I am having is only pin D5 seems to do anything and it turns ON/OFF all 3 switches. Any help as to why this is would really be appreciated.

        #include <MySensor.h>
        #include <SPI.h>
        #include <Bounce2.h>
        
        #define CHILD_ID1 3
        #define BUTTON_PIN1  3  // Arduino Digital I/O pin for button/reed switch
        #define CHILD_ID2 4
        #define BUTTON_PIN2  4  // Arduino Digital I/O pin for button/reed switch
        #define CHILD_ID3 5
        #define BUTTON_PIN3  5  // Arduino Digital I/O pin for button/reed switch
        
        
        
        MySensor gw;
        Bounce debouncer = Bounce(); 
        int oldValue=-1;
        
        // Change to V_LIGHT if you use S_LIGHT in presentation below
        MyMessage msg1(CHILD_ID1,V_TRIPPED), msg2(CHILD_ID2,V_TRIPPED),msg3(CHILD_ID3,V_TRIPPED);
        
        
        
        void setup()  
        {  
          gw.begin();
        
         // Setup the button
          pinMode(BUTTON_PIN1,INPUT);
          pinMode(BUTTON_PIN2,INPUT);
          pinMode(BUTTON_PIN3,INPUT);
          
          // Activate internal pull-up
          digitalWrite(BUTTON_PIN1,HIGH);
          digitalWrite(BUTTON_PIN2,HIGH);
          digitalWrite(BUTTON_PIN3,HIGH);
          
          // After setting up the button, setup debouncer
          debouncer.attach(BUTTON_PIN1);
          debouncer.interval(5);
          debouncer.attach(BUTTON_PIN2);
          debouncer.interval(5);
          debouncer.attach(BUTTON_PIN3);
          debouncer.interval(5);
        
          // Register binary input sensor to gw (they will be created as child devices)
          // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
          // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
          gw.present(CHILD_ID1, S_DOOR);
          gw.present(CHILD_ID2, S_DOOR);
          gw.present(CHILD_ID3, S_DOOR);  
        }
        
        
        //  Check if digital input has changed and send in new value
        void loop() 
        {
          debouncer.update();
          // Get the update value
          int value = debouncer.read();
         
          if (value != oldValue) {
             // Send in the new value
             gw.send(msg1.set(value==HIGH ? 1 : 0));
             gw.send(msg2.set(value==HIGH ? 1 : 0));
             gw.send(msg3.set(value==HIGH ? 1 : 0));
             oldValue = value;
          }
        } 
        
        
        TheoLT Offline
        TheoLT Offline
        TheoL
        Contest Winner
        wrote on last edited by
        #3

        @punter9 You have to create a Debouncer for each switch. A debouncer will only debounce the assigned switch. Besides you have to check each switch separately. I adjusted your Sketch. It compiles but I haven't tested it. All of my breadboards are full with test circuits at the moment.

        Give this a try

        #include <MySensor.h>
        #include <SPI.h>
        #include <Bounce2.h>
        
        #define CHILD_ID1 3
        #define BUTTON_PIN1  3  // Arduino Digital I/O pin for button/reed switch
        #define CHILD_ID2 4
        #define BUTTON_PIN2  4  // Arduino Digital I/O pin for button/reed switch
        #define CHILD_ID3 5
        #define BUTTON_PIN3  5  // Arduino Digital I/O pin for button/reed switch
        
        
        
        MySensor gw;
        Bounce debouncer1 = Bounce(); 
        Bounce debouncer2 = Bounce(); // debouncer for the second switch
        Bounce debouncer3 = Bounce(); // debouncer for the third switch
        
        int oldValue1=-1;
        int oldValue2=-1; // second switch needs to have it's own old state
        int oldValue3=-1; // second switch needs to have it's own old state
        
        // Change to V_LIGHT if you use S_LIGHT in presentation below
        MyMessage msg1(CHILD_ID1,V_TRIPPED), msg2(CHILD_ID2,V_TRIPPED),msg3(CHILD_ID3,V_TRIPPED);
        
        
        
        void setup()  
        {  
          gw.begin();
        
         // Setup the button
          pinMode(BUTTON_PIN1,INPUT_PULLUP ); // You can assign pinmode and use pullup in one statement.
          pinMode(BUTTON_PIN2,INPUT_PULLUP);
          pinMode(BUTTON_PIN3,INPUT_PULLUP);
          
          // Activate internal pull-up
         // digitalWrite(BUTTON_PIN1,HIGH);
        //  digitalWrite(BUTTON_PIN2,HIGH);
        //  digitalWrite(BUTTON_PIN3,HIGH);
          
          // After setting up the button, setup debouncer1
          debouncer1.attach(BUTTON_PIN1);
          debouncer1.interval(5);
          debouncer2.attach(BUTTON_PIN2);
          debouncer2.interval(5);
          debouncer3.attach(BUTTON_PIN3);
          debouncer3.interval(5);
        
          // Register binary input sensor to gw (they will be created as child devices)
          // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
          // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
          gw.present(CHILD_ID1, S_DOOR);
          gw.present(CHILD_ID2, S_DOOR);
          gw.present(CHILD_ID3, S_DOOR);  
        }
        
        
        //  Check if digital input has changed and send in new value
        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
             gw.send(msg1.set(value==HIGH ? 1 : 0));
        //     gw.send(msg2.set(value==HIGH ? 1 : 0)); // this is where you turn the second switch in your controller
        //     gw.send(msg3.set(value==HIGH ? 1 : 0));  // this is where you turn the third switch in your controller
             oldValue1 = value;
          }
          
          // Check if the 2nd switch state has changed
          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
        //     gw.send(msg1.set(value==HIGH ? 1 : 0));
               gw.send(msg2.set(value==HIGH ? 1 : 0)); // this is where you turn the second switch in your controller
        //     gw.send(msg3.set(value==HIGH ? 1 : 0));  // this is where you turn the third switch in your controller
             oldValue2 = value;
          }
          
          // Check if the third switch state has changed
          debouncer3.update();
          // Get the update value
          value = debouncer3.read(); // no need to redeclare it (I removed int)
         
          if (value != oldValue3) {
             // Send in the new value
        //     gw.send(msg1.set(value==HIGH ? 1 : 0));
        //     gw.send(msg2.set(value==HIGH ? 1 : 0)); // this is where you turn the second switch in your controller
             gw.send(msg3.set(value==HIGH ? 1 : 0));  // this is where you turn the third switch in your controller
             oldValue3 = value;
          }
        
          
        } 
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        25

        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