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. My Project
  3. Multi Binary Switches

Multi Binary Switches

Scheduled Pinned Locked Moved My Project
13 Posts 6 Posters 7.9k Views 2 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.
  • jeylitesJ Offline
    jeylitesJ Offline
    jeylites
    wrote on last edited by jeylites
    #1

    Hi, I'm trying to create an array of binary switches based on the 8Ch relay script, but I'm having issues setting it up. I don't have much knowledge in programming and I was wondering if anyone here could guide me. This is what I have done...

    // Simple binary switch example 
    // Connect button or door/window reed switch between 
    // digitial I/O pin 3 (BUTTON_PIN below) and GND.
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    const int BUTTON[] = {3, 4, 5};
    #define NUMBER_OF_BUTTONS 3
    //#define CHILD_ID 3
    //#define BUTTON_PIN  3  // 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 msg(V_TRIPPED);
    
    void setup()  
    {  
      gw.begin();
    // Fetch relay status
      for (int sensor=1, pin=0; sensor<=NUMBER_OF_BUTTONS;sensor++, pin++) {
        
     // Setup the button
      pinMode(BUTTON[pin],INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON[pin],HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON[pin]);
      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(sensor, 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(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
    } 
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      maltimus
      wrote on last edited by
      #2

      What is the issues you are having?

      jeylitesJ 1 Reply Last reply
      0
      • M maltimus

        What is the issues you are having?

        jeylitesJ Offline
        jeylitesJ Offline
        jeylites
        wrote on last edited by jeylites
        #3

        @maltimus

        I can't seem to get binary to work in an Array.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          raditv
          wrote on last edited by
          #4

          you have to make an array also for debouncer

          1 Reply Last reply
          0
          • B Offline
            B Offline
            BulldogLowell
            Contest Winner
            wrote on last edited by BulldogLowell
            #5

            @jeylites

            have you checked this thread?

            http://forum.mysensors.org/topic/994/multi-button-relay-switch/24

            jeylitesJ 1 Reply Last reply
            0
            • B BulldogLowell

              @jeylites

              have you checked this thread?

              http://forum.mysensors.org/topic/994/multi-button-relay-switch/24

              jeylitesJ Offline
              jeylitesJ Offline
              jeylites
              wrote on last edited by jeylites
              #6

              @BulldogLowell

              I'm trying to do Multi Binary switches without the relays. I tried using the Multi Button Relay sketch, but I'm afraid that if remove the relay function, it would affect the button setup.

              @Rachmat-Aditiya How do I go about doing that an array for debouncer?

              B 1 Reply Last reply
              0
              • jeylitesJ jeylites

                @BulldogLowell

                I'm trying to do Multi Binary switches without the relays. I tried using the Multi Button Relay sketch, but I'm afraid that if remove the relay function, it would affect the button setup.

                @Rachmat-Aditiya How do I go about doing that an array for debouncer?

                B Offline
                B Offline
                BulldogLowell
                Contest Winner
                wrote on last edited by
                #7

                @jeylites said:

                How do I go about doing that an array for debouncer?

                the example of debouncing an array of sensors was in the code in the examples... was my point.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mikeones
                  wrote on last edited by
                  #8

                  Here is one example with a few extras.

                  // Simple binary switch example 
                  // Connect button or door/window reed switch between 
                  // digitial I/O pin 3 (BUTTON_PIN below) and GND.
                  
                  #include <MySensor.h>
                  #include <SPI.h>
                  #include <Bounce2.h>
                  #include <Time.h>        //http://playground.arduino.cc/Code/Time
                  #include <TimeAlarms.h>  //http://playground.arduino.cc/Code/Time
                  
                  MySensor gw;
                  
                  #define RADIO_ID 12
                  #define noReeds 5
                  const int BUTTON_PIN[] = {14, 15, 16, 17, 18};            // Arduino Digital I/O pin for button/reed switch, A0 - A4
                  boolean reedState[] = {HIGH, HIGH, HIGH, HIGH, HIGH};
                  
                  
                  Bounce debouncer[noReeds];
                  MyMessage msg[noReeds];
                  
                  void setup(){
                    Serial.println("Starting setup" );
                    gw.begin(NULL, RADIO_ID, true);  //stattc RADIO_ID an enable repeater
                    Alarm.delay(250);
                    gw.sendSketchInfo("Alarm Pannel", "1.0");
                    Alarm.delay(250);
                    gw.requestTime(receiveTime);
                    // initialize Relays with corresponding buttons
                    for (int i = 0; i < noReeds; i++){
                      msg[i].sensor = i;			// initialize messages
                      msg[i].type = V_TRIPPED;
                      debouncer[i] = Bounce();		// initialize debouncer
                      debouncer[i].attach(BUTTON_PIN[i]);
                      debouncer[i].interval(5);
                      // Setup the button
                      pinMode(BUTTON_PIN[i],INPUT);
                      // Activate internal pull-up
                      digitalWrite(BUTTON_PIN[i],HIGH);
                      gw.present(i, S_DOOR);		// present sensor to gateway
                      Serial.print("setup for switch: ");
                      Serial.print(BUTTON_PIN[i]);
                      Serial.println(" complete" );
                      Alarm.timerRepeat(720, updateState); // update relay status every 2 hrs
                      Alarm.delay(250);
                    }
                    Serial.println("Setup complete" );
                  }
                  
                  
                  //  Check if digital input has changed and send in new value
                  void loop() 
                  {
                    gw.process();
                    for (int i = 0; i < noReeds; i++){
                      debouncer[i].update();
                      // Get the update value
                      int value = debouncer[i].read();
                      if (value != reedState[i]) {
                         // Send in the new value
                         gw.send(msg[i].set(value==HIGH ? 1 : 0), false);
                         reedState[i] = value;
                         Serial.print("updating state for swicth: ");
                         Serial.print(BUTTON_PIN[i]);
                         Serial.print(" state: ");
                         Serial.println(reedState[i]);
                      }
                    }
                  }
                  
                  void updateState(){
                    Serial.println("Start state info");
                    for (int i = 0; i < noReeds; i++)
                    {
                        Serial.print("sending update for switch: ");
                        Serial.println(BUTTON_PIN[i]);
                        gw.present(i, S_DOOR);
                        Alarm.delay(250);
                        //MyMessage msg(relayPin[pin],V_LIGHT);
                        gw.send(msg[i].set(reedState[i]), false); // Send last state from eprom to GW
                        Alarm.delay(250);
                    }
                  }
                  
                  // This is called when a new time value was received
                  void receiveTime(unsigned long time) {
                    setTime(time);
                  }```
                  jeylitesJ 1 Reply Last reply
                  0
                  • M mikeones

                    Here is one example with a few extras.

                    // Simple binary switch example 
                    // Connect button or door/window reed switch between 
                    // digitial I/O pin 3 (BUTTON_PIN below) and GND.
                    
                    #include <MySensor.h>
                    #include <SPI.h>
                    #include <Bounce2.h>
                    #include <Time.h>        //http://playground.arduino.cc/Code/Time
                    #include <TimeAlarms.h>  //http://playground.arduino.cc/Code/Time
                    
                    MySensor gw;
                    
                    #define RADIO_ID 12
                    #define noReeds 5
                    const int BUTTON_PIN[] = {14, 15, 16, 17, 18};            // Arduino Digital I/O pin for button/reed switch, A0 - A4
                    boolean reedState[] = {HIGH, HIGH, HIGH, HIGH, HIGH};
                    
                    
                    Bounce debouncer[noReeds];
                    MyMessage msg[noReeds];
                    
                    void setup(){
                      Serial.println("Starting setup" );
                      gw.begin(NULL, RADIO_ID, true);  //stattc RADIO_ID an enable repeater
                      Alarm.delay(250);
                      gw.sendSketchInfo("Alarm Pannel", "1.0");
                      Alarm.delay(250);
                      gw.requestTime(receiveTime);
                      // initialize Relays with corresponding buttons
                      for (int i = 0; i < noReeds; i++){
                        msg[i].sensor = i;			// initialize messages
                        msg[i].type = V_TRIPPED;
                        debouncer[i] = Bounce();		// initialize debouncer
                        debouncer[i].attach(BUTTON_PIN[i]);
                        debouncer[i].interval(5);
                        // Setup the button
                        pinMode(BUTTON_PIN[i],INPUT);
                        // Activate internal pull-up
                        digitalWrite(BUTTON_PIN[i],HIGH);
                        gw.present(i, S_DOOR);		// present sensor to gateway
                        Serial.print("setup for switch: ");
                        Serial.print(BUTTON_PIN[i]);
                        Serial.println(" complete" );
                        Alarm.timerRepeat(720, updateState); // update relay status every 2 hrs
                        Alarm.delay(250);
                      }
                      Serial.println("Setup complete" );
                    }
                    
                    
                    //  Check if digital input has changed and send in new value
                    void loop() 
                    {
                      gw.process();
                      for (int i = 0; i < noReeds; i++){
                        debouncer[i].update();
                        // Get the update value
                        int value = debouncer[i].read();
                        if (value != reedState[i]) {
                           // Send in the new value
                           gw.send(msg[i].set(value==HIGH ? 1 : 0), false);
                           reedState[i] = value;
                           Serial.print("updating state for swicth: ");
                           Serial.print(BUTTON_PIN[i]);
                           Serial.print(" state: ");
                           Serial.println(reedState[i]);
                        }
                      }
                    }
                    
                    void updateState(){
                      Serial.println("Start state info");
                      for (int i = 0; i < noReeds; i++)
                      {
                          Serial.print("sending update for switch: ");
                          Serial.println(BUTTON_PIN[i]);
                          gw.present(i, S_DOOR);
                          Alarm.delay(250);
                          //MyMessage msg(relayPin[pin],V_LIGHT);
                          gw.send(msg[i].set(reedState[i]), false); // Send last state from eprom to GW
                          Alarm.delay(250);
                      }
                    }
                    
                    // This is called when a new time value was received
                    void receiveTime(unsigned long time) {
                      setTime(time);
                    }```
                    jeylitesJ Offline
                    jeylitesJ Offline
                    jeylites
                    wrote on last edited by jeylites
                    #9

                    @mikeones Thank you, The sketch is very helpful and it works. Just out of curiosity, what is the function of Time & TimeAlarm ?

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mikeones
                      wrote on last edited by
                      #10

                      It forces a status update at a predefined interval.

                      Alarm.timerRepeat(720, updateState); // update relay status every 2 hrs
                      
                      jeylitesJ 1 Reply Last reply
                      0
                      • M mikeones

                        It forces a status update at a predefined interval.

                        Alarm.timerRepeat(720, updateState); // update relay status every 2 hrs
                        
                        jeylitesJ Offline
                        jeylitesJ Offline
                        jeylites
                        wrote on last edited by
                        #11

                        @mikeones Got it.

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          Cowboy1980
                          wrote on last edited by
                          #12

                          Much appreciated for the script but Ive been trying to get more reed switches running off the one nano, I added the extra pins to Button_Pin and changed noReeds and boonlean readstate to represent how many pins I was using. I have 12 showing up in vera and serial shows correctly the change in state of all 12 pins but in the Vera all 12 show but dont trip/show the state change. What am I missing? When I run the code youve written as is everything works fine but as soon as I even add 1 more pin Mios wont show the state change. Plz help!

                          jeylitesJ 1 Reply Last reply
                          0
                          • C Cowboy1980

                            Much appreciated for the script but Ive been trying to get more reed switches running off the one nano, I added the extra pins to Button_Pin and changed noReeds and boonlean readstate to represent how many pins I was using. I have 12 showing up in vera and serial shows correctly the change in state of all 12 pins but in the Vera all 12 show but dont trip/show the state change. What am I missing? When I run the code youve written as is everything works fine but as soon as I even add 1 more pin Mios wont show the state change. Plz help!

                            jeylitesJ Offline
                            jeylitesJ Offline
                            jeylites
                            wrote on last edited by
                            #13

                            @Cowboy1980 Can you post your script...

                            1 Reply Last reply
                            0

                            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
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            27

                            Online

                            12.0k

                            Users

                            11.2k

                            Topics

                            113.4k

                            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