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

ble button function

When i intially burn the code on dk, my dk should be in system off mode ie all functionality should be off. Then i want to press a button for 5 seconds with which the device blinks an led pattern. and if uptil  not 5 seconds button not pressed then nothing occurs and device remains in sleep mode

Parents
  • Hello,

    Then i want to press a button for 5 seconds with which the device blinks an led pattern. and if uptil  not 5 seconds button not pressed then nothing occurs and device remains in sleep mode

    Unfortunately there is no way to delay the wakeup by 5 seconds before waking up. If you configure the device to wake on an external event through the GPIOTE PORT event, or similar, it will start the full wakeup immediately when this happens. So, if you want to see that the button is still pressed after 5 seconds you will have to add a check for this that runs as the first thing upon wakeup from SYSTEM_OFF.
    Alternatively, you will need some external circuitry that delays the wakeup signal with 5 seconds, if it is crucial that the device does not wake up for the first 5 seconds.

    You can see SYSTEM_OFF sleep mode demonstrated in most of the ble_peripheral examples in the SDK, such as the Nordic UART peripheral example's sleep_mode_enter function. The bsp_btn_ble module is also used to extract the startup event, i.e seeing which button is still pressed upon startup. You could do the same and have a timer start upon startup, to see if the button is held for 5 seconds. If it is not, the device may return to sleep.

    Best regards,
    Karl

  • yes Karl u are getting exactly the way i want.

    i have seen the Nordic UART peripheral example's sleep_mode_enter function. Can u pls assist me in a code whereby i can check this:

    initially the device is in sleep mode as when the code is flashed. Then i press a button for 5 secs to see some led pattern or advertising should start and in case if button is not held for 5 secs then it should still remain in sleep mode and if beyond 5 seconds it should nt register anything

  • Hello,

    Please always use the Insert -> Code option when sharing code here in DevZone.

    Ridhi said:
    I want a that when i flash the code my device should be in system off mode,

    When you have implemented the check for the 5 seconds button press the device should always go into S

    Ridhi said:
    i am using nrf sdk 17.1.0 examples/ble_peripherals/template nrf dk nrf52832. and s132. 

    Thank you for specifying.

    Ridhi said:
    Then when i press  a button for 5 secs then it should start advertising. and if not pressed for 5 secs it should remian in sleep mode.  i have used the BSP_LONG_PUSH_TIMEOUT_MS in bsp_config to 5000 ms but it is not working. pls assist am attaching the code as

    This is one way you could go about implementing this:
    In the timers_init you should create and start a 5 second timer that will call sleep_mode_enter when it expires.
    You should then have the 5 second button long push stop this timer, and start the advertising. The button_led_init should therefore happen before the timers_init, so that the button press always leads the sleep timer.
    You will also need to check whether the extracted startup event in the button_leds_init was the 5 second button, and if so you need to call the bsp_button_event_handler manually with the extracted startup event. You should also consider what the correct handling for if the button is released before 5 seconds has passed, but then immediately pressed again. Should this restart the sleep timer, for example?
    Also, what is the timer_timeout_handler function?

    Please try this, and let me know if you achieve the desired functionality.

    Best regards,
    Karl

  • Hi Karl,

    I have nrf53832 dev kit sft devc s132 and sdk 17.1.0

    i have a code where initially the system is off

    then i press button 1 on dev kit for 5 secs then the led 1 glows.

    also, if multiply by 2 in this value 65534*100 i can press button for 10 secs and then the led 1 glows and works as desired.BOTH SENSOR - system off.rar

    My issue is that from where this 65534 value is comming which sets button press to be 5 secs. Like my issue is that now i want tht i should press button for say 3 secs then what will i use?

  • Ridhi said:

    i have a code where initially the system is off

    then i press button 1 on dev kit for 5 secs then the led 1 glows.

    Great, I am glad to hear that you have successfully implemented the functionality you desired!
    For clarity I also would like to emphasize that the DK is actually powered on for the 5 seconds you are pressing the button as well, so it technically wakes immediately upon the button press.

    Ridhi said:
    My issue is that from where this 65534 value is comming which sets button press to be 5 secs. Like my issue is that now i want tht i should press button for say 3 secs then what will i use?

    I can not open your project unless you share it as a .zip file.
    I am not sure where the value you are referencing is coming from either, but if you are using the TIMER or APP_TIMER there are macros and functions to convert from milliseconds to timer ticks for you, so you do not have to input the raw tick values yourself. I recommend that you use these macros or functions instead of inputting the raw tick values directly.

    Best regards,
    Karl

  • Please specify which section of the code you are referring to in your previous comment, so I know what you would like me to take a look at. Please be as specific as possible.

Reply Children
  • in main()

    while(bsp_button_is_pressed(BSP_BOARD_BUTTON_0) == true)
        {
    				if (button_tick >= 65534*100) //need to identify for 3 secs button pres
    			
    					{
    						
    						button_tick = 0;
    						bsp_indication_set(BSP_INDICATE_USER_STATE_0);
    						NRF_LOG_INFO("BeforeFivesec"); 
    						sleepFlag = 0;
    						break;
    				}
    					button_tick++;
    	  }	

  • Where did you acquire this button tick number from?
    To me it seem like this is just wasted CPU cycles counted for 5 seconds, is this something that will happen often in your device so that it will negatively impact your battery life?
    I would instead recommend that you implement this as a timer that is started upon wakeup, and then go to SYSTEM_ON sleep while you wait for the timer to expire. Upon expiration you can check if the button is still held, and if you have enabled the RELEASE event for the button you will be woken up before the timer has expired if the button is released before 5 seconds has passed - this will then mean that you need to go back to SYSTEM_OFF sleep.

    This approach will save you 5 seconds of active CPU time every time this scenario happens.

    Best regards,
    Karl

Related