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

  • 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.

  • 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