This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Bug in behavior coming in and out of sleep mode

I'm currently building a device that has the following functionality:

1.) wake from sleep mode and wait 5 seconds for a potential button press

2.) if the button is pressed write the current time received from an RTC over twi to flash memory after the button press is received

3.) wait for the button to be released

4.)write the current time (of when button is released) again from RTC received over twi to flash memory

5.) send the flash memory with logged times over ble

6.)go to sleep

When the button is pressed and held I want the device to both wake from sleep mode and write the current time, then begin waiting for the button release.  When the device is woken up (button not in pressed state when program leaves sleep mode) then the button is pressed when the device isn't in sleep mode the program works as intended.  But when I hold the button down to wake the device (ie the program starts with the button already pressed) and continue holding the button down the device writes the starting time, but then restarts (after line 24), writes the starting time again, then waits for the button release. This effectively writes: start time - start time - end time instead of start time - end time.

This problem seems to be due to the program starting with the button in the pressed state, but I'm not sure why.  Here is the function that handles the button press events.  On device startup this function automatically runs after everything is initialized.

void buttonPress()
{
  const bool startTime = 0;
  const bool endTime = 1;
  nrf_gpio_cfg_output(LED); //configures the led pin as an output pin
  nrf_gpio_cfg_input(Button, NRF_GPIO_PIN_PULLUP); //configures the button pin as an input pin. nrf_gpio_cfg_input takes argument for a pin and an enumerator. 
  APP_TIMER_DEF(mSleepTimer);                                      
  app_timer_create(&mSleepTimer, APP_TIMER_MODE_SINGLE_SHOT, mSleepTimer_timeout_handler); //mSleepTimer handler can be passed by ref like this since the third parameter is already the typedef of an app_timer_timeout_handler_t function

  
   //led pin uses inverse logic, give it 0 and will turn on, give 1 and will turn off
   nrf_gpio_pin_set(LED); //turns off the led
  

   while (true) //enter this loop which will act as an event handler for a button press if a button press is received, or timeout and go into sleep mode if DoingNothingTooLong (15 seconds) time is reached
   {
      app_timer_start(mSleepTimer,DoingNothingTooLong, NULL); //timer to put into sleep mode for case of device woken up but no button press is received

      if(nrf_gpio_pin_read(Button) == 0) //reads the button state. 0 means the button is being pressed.  Also stops the timer so that it doesn't enter sleep mode 
      {
        app_timer_stop(mSleepTimer);
        writeTime(startTime);
        nrf_delay_ms(150); //delay to allow for time to be written to memory
        nrf_gpio_pin_clear(LED); //turn on the LED to indicate that start time was successfully written
        
        while(nrf_gpio_pin_read(Button) == 0) //while the button is being pressed loop. stay here until button is released
        {};
        
        writeTime(endTime);
        nrf_delay_ms(150); //delay to allow for time to be written to memory
        nrf_gpio_pin_set(LED); //turn off the LED to indicate that end time was successfully written
        break;
      } 
   } 

}

If more code would help I can add more but I felt it would overly complicate things since this function is where the problem seems to lie and the write function works well.  I want the program to write the time then enter the while loop until the button is pressed, but when woken up with the button in a pressed state it seems to restart after line 24 since the led which indicates a successful write turns on, then the device resets, then the led that indicates a successful write comes back on.  I think I am not understanding something related to waking from a sleep state, so any ideas of what could be going on would be very helpful, thanks!

Related