[SOLVED] very wierd sleep behaviour



  • this is my loop function:
    #define MY_LED 19
    #define BUTTON_PIN 2
    bool led = false;
    void loop()
    {
    sleep(1000);
    digitalWrite(MY_LED, led);
    led = !led;
    sleep(digitalPinToInterrupt(BUTTON_PIN), FALLING, 10000);
    }

    this should turn the led on/of in 11s intervals or after button press. But it turn led on/off in 1s intervals !? if I comment the call to first sleep, then it works as expected (turns the led on/off in 10s intervals). So is that possible ? Is this a bug ? I'm using 2.1.1 MySensors library.



  • if I change
    sleep(1000);
    to
    sleep(digitalPinToInterrupt(BUTTON_PIN), FALLING, 1000);
    then works ok ... this must be a bug ...


  • Mod

    Use wait instead of sleep in loop, use sleep at the end of loop. But why are you not just using sleeping for 11s at the end of loop? Some people used like wait (500) before the sleep call to avoid random interrupts.
    Maybe you could set the pull up resistor on pin 2 just to avoid floating pin randomly triggering interrupt



  • do You mean that the sleep function may be called only once at the end of the loop function ? Could You point me to the docs where it is stated ?


  • Mod

    Just look at the many examples around, sleep is used at the end of loop function because it puts everything in power saving mode. In your case it depends if you want to have a 1s delay after button is pressed or not and have just the sleep function at the end doing the timing



  • examples are not the documentation. The question is, why the first sleep function causes the other one to stop sleeping immediately ? It shouldn't do that. The BUTTON_PIN has of course the pullup, so it is not triggering the actual interrupt.


  • Mod

    @rozpruwacz recently an issue with waking from interrupts was fixed in the mySensors library code.
    Try with the development version of the library (2.2.0 beta) and report back if that fixes your issue.



  • YES ! beta version has no more that issue, thaks ! 🙂


  • Mod

    @rozpruwacz great to hear!
    Now continue the fun 😆


Log in to reply
 

Suggested Topics

  • 1
  • 6
  • 5
  • 3
  • 1
  • 1

11
Online

11.4k
Users

11.1k
Topics

112.7k
Posts