[SOLVED] Node with 2 Buttons, both debounced, always having the same status



  • Hey,

    I tried to programm a nano to have 2 relays and 2 buttons for controlling them. But every time, I change the status of one of the switches, the other is changed, too. Maybe it is something about the debouncing, but afaik you can create 2 instances of debounce.
    I minimized the sketch to avoid problems with further code, but still the same:

    // 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>
    
    #define CHILD_ID 4
    #define BUTTON_PIN  4  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID2 5
    #define BUTTON2_PIN  5  // Arduino Digital I/O pin for button/reed switch
    
    MySensor gw;
    Bounce debouncer = Bounce(); 
    Bounce debouncer2 = Bounce(); 
    int oldValue=-1;
    int oldValue2=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_LIGHT);
    MyMessage msg2(CHILD_ID2,V_LIGHT);
    
    void setup()  
    {  
      gw.begin();
    
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      pinMode(BUTTON2_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON2_PIN,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
      debouncer2.attach(BUTTON2_PIN);
      debouncer2.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_ID, S_LIGHT);  
      gw.present(CHILD_ID2, S_LIGHT);  
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      debouncer.update();
      debouncer2.update();
      // Get the update value
      int value = debouncer.read();
      int value2 = debouncer2.read();
     
      if (value != oldValue) {
         // Send in the new value
         gw.send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      if (value2 != oldValue2) {
         // Send in the new value
         gw.send(msg2.set(value2==HIGH ? 1 : 0));
         oldValue2 = value2;
      }
    } 
    

    I would be greatful for any kind of hint.



  • OK I solved the problem: there was a litte solder brigde between PIN 4 and 5. I didn't checked that carefully before because I bought it with already soldered headers.


  • Admin

    Ok 😉 Looked at the code earlier but couldn't find anything wrong.


Log in to reply
 

471
Online

6.9k
Users

7.8k
Topics

82.7k
Posts

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.