Using Sleep RISING mode with momentary switch?



  • I'm trying to make a sensor for garden doorbell duties. So I built a battery powered sensor with a momentary switch wired to pin 3 using a pull down resistor.
    My intention is to make it detect when someone pushes the button (obviously) but not changing the state to off as the button is released, but using a timer.
    With that maneuver I want to be able to make a camera send me the video while that timer is active.
    Also, as the sensor is battery powered, It will also sleep for a longer time and wake on the pin interrupt.

    So I thought that it could be clean and nice only waking the sensor on the button pushed and only changing the amount of sleep.
    In my code I used the sleep function with the RISING mode, but I found that it triggers with the on, but also the off state of the switch.

    I made a nasty hack and put a delay to neglect the pulling up of the button, but is there any solution?

    There's my code:

    // Loop will iterate on changes on the BUTTON_PINs
    void loop() 
    {
      uint8_t value = digitalRead(PRIMARY_BUTTON_PIN);
      unsigned long sleepTime = value==HIGH ? SHORT_PAUSE : SLEEP_TIME;
      send(msg.set(value==HIGH ? 1 : 0));
      digitalWrite(FEEDBACK_LED_PIN, value);
    
    #ifdef MY_DEBUG
       Serial.println(value==HIGH ? "Button press detected!" : "Reset!");
       Serial.print("New values: "); Serial.print(value); Serial.print(" / "); Serial.println(sleepTime);
    #endif
    
      SendBatteryLevel();
    
      delay(5000);
      
      // Sleep until something happens with the sensor
      sleep(PRIMARY_BUTTON_PIN-2, RISING, sleepTime);
    }```

  • Mod

    @Sergio-Rius delay is actually the easiest way to do it 🙂 Either that, or build a hardware debouncer.

    See also https://www.mysensors.org/build/binary for how to use the bounce2 library to handle debouncing. That solution doesn't support interrupts though, so delay is probably a better solution for your node.


Log in to reply
 

Suggested Topics

12
Online

11.4k
Users

11.1k
Topics

112.7k
Posts