Simple binary switch example, trying to add second switch



  • Hello! Firs i'm newbie to arduino and my sensors so please don't be mad on me in know that is for you guys probably basic of arduino coding.
    so i tok the basic exsample of Simple binary switch example and try to add one more switch but i think i have some problem with bounce function because it work only one switch CHILD_ID_ST2 on the code below
    Thanks for any help! Probable master can solve that problem in couple of minutes 🙂 it bother me 2 days 🙂

    /**
     * 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
     */
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define CHILD_ID_ST1 3
    #define CHILD_ID_ST2 4
    #define ST1_PIN  4  // Arduino Digital I/O pin for button/reed switch
    #define ST2_PIN  3
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msgst1(CHILD_ID_ST1,V_TRIPPED);
    MyMessage msgst2(CHILD_ID_ST2,V_TRIPPED);
    void setup()  
    {  
      // Setup the button
      pinMode(ST1_PIN,INPUT);
      pinMode(ST2_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(ST1_PIN,HIGH);
      digitalWrite(ST2_PIN,HIGH);
      // After setting up the button, setup debouncer
      debouncer.attach(ST1_PIN);
      debouncer.attach(ST2_PIN);
      debouncer.interval(5);
    }
    
    void presentation() {
      // 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.
      present(CHILD_ID_ST1, S_DOOR); 
      present(CHILD_ID_ST2, 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
         send(msgst1.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      if (value != oldValue) {
         // Send in the new value
         send(msgst2.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
    }```

  • Mod

    @Mitja-Blazinsek the debouncer can only handle one button. You you'll need two debouncers. Change

    Bounce debouncer = Bounce(); 
    

    to

    Bounce debouncer = Bounce(); 
    Bounce debouncer2 = Bounce(); 
    

    and adjust the rest of the code to use both debouncers.



  • i try to change that but again i'm doing something wrong now it change it constantly both state when i pressed any button

    /**
     * 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
     */
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define CHILD_ID_ST1 3
    #define CHILD_ID_ST2 4
    #define ST1_PIN  4  // Arduino Digital I/O pin for button/reed switch
    #define ST2_PIN  3
    Bounce debouncer1 = Bounce(); 
    Bounce debouncer2 = Bounce();
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msgst1(CHILD_ID_ST1,V_TRIPPED);
    MyMessage msgst2(CHILD_ID_ST2,V_TRIPPED);
    void setup()  
    {  
      // Setup the button
      pinMode(ST1_PIN,INPUT);
      pinMode(ST2_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(ST1_PIN,HIGH);
      digitalWrite(ST2_PIN,HIGH);
      // After setting up the button, setup debouncer
      debouncer1.attach(ST1_PIN);
      debouncer2.attach(ST2_PIN);
      debouncer1.interval(5);
        debouncer2.interval(5);
    }
    
    void presentation() {
      // 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.
      present(CHILD_ID_ST1, S_DOOR); 
      present(CHILD_ID_ST2, S_DOOR);    
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      debouncer1.update();
      // Get the update value
      int value = debouncer1.read();
    
      if (value != oldValue) {
         // Send in the new value
         send(msgst1.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      debouncer2.update();
      // Get the update value
      int value2 = debouncer2.read();
        
      if (value2 != oldValue) {
         // Send in the new value
         send(msgst2.set(value2==HIGH ? 1 : 0));
         oldValue = value2;
      }
    }```

  • Hero Member

    @Mitja-Blazinsek You are using the oldValue variable for both switches. You need to declare and use a unique one for each switch.

    For example

    if (value2 != oldValue2) {
         // Send in the new value
         send(msgst2.set(value2==HIGH ? 1 : 0));
         oldValue2 = value2;
      }
    
    


  • thank you very much boots33 and mfalkvidd finaly wor also learn a lot on this example, its time now to try with relays i will add 2 relays that i can triger with switch or controler

    my working code for switch:

    /**
       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 updated  for 2 switches
       Connect button or door/window reed switch between
       digitial I/O pin 3 (BUTTON_PIN below) and GND.
       http://www.mysensors.org/build/binary
    */
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define CHILD_ID_ST1 3
    #define CHILD_ID_ST2 4
    #define ST1_PIN  3  // Arduino Digital I/O pin for button/reed switch
    #define ST2_PIN  4
    Bounce debouncer1 = Bounce();
    Bounce debouncer2 = Bounce();
    int oldValue = -1;
    int oldValue1 = -1;
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msgst1(CHILD_ID_ST1, V_TRIPPED);
    MyMessage msgst2(CHILD_ID_ST2, V_TRIPPED);
    void setup()
    {
      // Setup the button1
      pinMode(ST1_PIN, INPUT);
      debouncer1.attach(ST1_PIN);
      debouncer1.interval(5);
      digitalWrite(ST1_PIN, HIGH);
      // Setup the button2
      pinMode(ST2_PIN, INPUT);
      debouncer2.attach(ST2_PIN);
      debouncer2.interval(5);
      digitalWrite(ST2_PIN, HIGH);
    }
    
    void presentation() {
      // 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.
      present(CHILD_ID_ST1, S_DOOR);
      present(CHILD_ID_ST2, S_DOOR);
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop()
    {
      debouncer1.update();
      // Get the update value
      int value = debouncer1.read();
    
      if (value != oldValue) {
        // Send in the new value
        send(msgst1.set(value == HIGH ? 1 : 0));
        oldValue = value;
      }
    
      debouncer2.update();
      // Get the update value
      int value1 = debouncer2.read();
    
      if (value1 != oldValue1) {
        // Send in the new value
        send(msgst2.set(value1 == HIGH ? 1 : 0));
        oldValue1 = value1;
      }
    
    
    }
    

Log in to reply
 

Suggested Topics

  • 3
  • 6
  • 5
  • 2
  • 1
  • 8

2
Online

11.4k
Users

11.1k
Topics

112.7k
Posts