Multi Binary Switches



  • 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;
      }
    } 
    


  • What is the issues you are having?



  • @maltimus

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



  • you have to make an array also for debouncer


  • Contest Winner



  • @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?


  • Contest Winner

    @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.



  • 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);
    }```


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



  • It forces a status update at a predefined interval.

    Alarm.timerRepeat(720, updateState); // update relay status every 2 hrs
    


  • @mikeones Got it.



  • 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!



  • @Cowboy1980 Can you post your script...


Log in to reply
 

Suggested Topics

  • 1847
  • 81
  • 6
  • 4
  • 1
  • 3
  • 1
  • 59

139
Online

9.8k
Users

10.3k
Topics

106.7k
Posts