Software Timers and interrupts, Photon hangs

Hi all,

EDIT: May have the solution already. Just heard about startFromISR(); Going to try this first. Tried deleting this post for now but don’t know how.

Question.

  • I have a hardware setup with a Particle Photon (0.7.0) and 1 momentary push button on pin A4.
  • I have a non-complex application (see below) that checks if this push button is held down for 3000 ms, and if so, puts the device to sleep.

My problem is the following:
Sometimes (about 1 in 5 attempts), when I initiate the press (rising), the photon stops responding. What I mean by that is that no code is executed any more. Also, the pulsating blue LED on the photon immediately stops pulsating and just freezes at it’s current brightness. I’m puzzled why it hangs.

//setup() function invokes initPowerbutton()
//loop() is empty and I'd like to keep it so, so no checking whether the powerbutton is pressed long enough by millis()

    #define BTN_PIN         A4
    Timer buttonPressTimer(3000, onButtonPressTimerTick, true);

bool initPowerbutton(){
    pinMode(BTN_PIN, INPUT_PULLDOWN);
    return attachInterrupt(BTN_PIN, onButtonPress, RISING);
}

void onButtonPress(){
    attachInterrupt(BTN_PIN, onButtonReleasedEarly, FALLING);
    buttonPressTimer.start();
}

void onButtonReleasedEarly(){
    buttonPressTimer.reset();
    buttonPressTimer.stop();
    attachInterrupt(BTN_PIN, onButtonPress, RISING);
}

//3000 ms has passed and timer was not stopped
void onButtonPressTimerTick(){
    //call a member function that invokes System.sleep();
}

Let us know if your edit fixes your problem!