random delay

I want to add a random delay in range 2000 to 3000 ms before my device perfrom any activity.

pls tell how can do this at earliest

usins some  timer or something else

Parents Reply Children
  • can u pls change that i my shred code ,i am realyy finding it bit difficult

  • Hakon it will be very much kind of you if you will assist at earliest.

  • Please stop updating the thread if you do not get an answer within minutes.

    When you input a thread on this forum, we assume that you read through the responses, evaluate what you should do, and provide a bit more effort to try to solve your own scenario. A response like this two minutes after I have given you advice:

    Ridhi said:
    can u pls change that i my shred code ,i am realyy finding it bit difficult

    Indicates no question, nor any effort from your side to explain what you want to do, nor what you want to achieve.

    It is very clear that you are new to C programming, and I would strongly recommend that you read up on this matter and get a bit more experience within the field of embedded programming. 

     

    Kind regards,

    Håkon 

  • Hakon i had put this query only after i had tried a few things:

    1). actually earlier i had put  rando delay like:

     nrf_delay_ms((rand()% 3000) + 2000);

    i then tested this and was able to get the required delay. I had an issue that random delay using nrf delay resulted in very high current of 6mA.

    2). Then i studied and found tht timer method is another feasible way..

    I tried :

    err_code = app_timer_start(instance, APP_TIMER_TICKS((rand() % 1000) + 2000), NULL);
    but this also did not help.

    3). i refrred ways that:

    RNG peripheral,

    crypto random method

    and soft device method

    i am some how unable to integrate the hndler thing of soft device.thats why i am asking help from you.

  • Hi,

     

    Ridhi said:
    rand()

    This is a stdlib function (unless you manually defined this function), which is implemented based on what your linked in standard libc (Keil's libc in your case) actually implements. In embedded applications, such functions are seldom targeting the actual hardware RNG unless you provide hooks into it.

    This is why you have to use the RNG, or the API that the softdevice exposes, which is the sd_rand_* API.

    As mentioned earlier, it is very crucial that you query this API after the BLE stack is initialized:

    Håkon Alseth said:
    For the sd_rand_* API to provide you with random bytes, you need to call this after the softdevice has been initialized (ie. after ble_stack_init()).

     I would recommend that you enter debug mode and see if the sd_rand_* API calls succeeds and if the input variable is populated with random numbers, then implement the rest step-by-step.

    Ridhi said:
    I had an issue that random delay using nrf delay resulted in very high current of 6mA.

    This is because nrf_delay uses the CPU for waiting, by essentially counting down from a very large number until it reaches 0.

    Ridhi said:
    err_code = app_timer_start(instance, APP_TIMER_TICKS((rand() % 1000) + 2000), NULL);
    but this also did not help.

    When implemented with a function that returns a random number, this starts the timer and you will get an interrupt after between 2-3 seconds. You must add logic inside the timer_handler that you set for the specific "instance", as well as the waiting logic in your main-loop, something like this:

    start_my_apptimer();

    while(my_timer_flag == false) {

      // reset the flag set in the timer_handler

      my_timer_flag = false;

      idle_state_handle();

    }

    /* continue execution */

     

    Kind regards,

    Håkon

Related