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
  • Hi,

     

    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

    What SDK, and what are you delaying? For nRF5, you can use an app_timer instance for this. For NCS, you can use k_sleep().

     

    Kind regards,

    Håkon

  • i  am using nrf sdk vrsn 17.1.0 

    nrf52832 board softdevc s132.

    i  have added simple single shot and repeated timers in the code attached.

    i have two nrf 52832 boards and want to add a random timer.

    but i dont get how to add a random timer.

    i have to do that before timer used in line 357, i should add a  random ms value 

    such that one board when works operates with one random value delay while other board works with another random value  

  • Hi,

     

    You can setup a one-shot timer with a random value. Here's some pseudo code for you:

    uint16_t rand_value;
    uint8_t bytes_available = 0;
    
    (void)sd_rand_application_bytes_available_get(&bytes_available);
    if (bytes_available >= sizeof(rand_value))
    {
        err_code = sd_rand_application_vector_get(&rand_value, sizeof(rand_value));        
        APP_ERROR_CHECK(err_code);
    } else {
      /* implement your error handling if no rand bytes available */
    }
    
    err_code = app_timer_start(m_my_timer_instance, APP_TIMER_TICKS((rand_value % 1000) + 2000), NULL);
    ...

    You'll need to initialize the app timer instance in addition to the above logic.

      

    Kind regards,

    Håkon

  • pls tell what is this rand_value, bytes_available

    kindly assist me in logic understanding i am new to it.

    how to add instance 

    also where in my code shared above can i add this so before timer used in line 357

  • You're fetching the amount of random numbers available from the softdevice, and if that number is greater or equal to two 2bytes, you populate the "rand_value" with those random bytes.

    Ridhi said:
    also where in my code shared above can i add this so before timer used in line 357

    It seems that you have already several app_timer instances set up. I would recommend that you follow the same way of initializing and starting the timer, just that you change the functionality to "one shot" here.

    Please see the app_timer documentation if anything is unclear: https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/lib_timer.html?cp=8_1_3_53

     

    Kind regards,

    Håkon

  • Hakon i added:

    1). defined insatnce:  

    APP_TIMER_DEF(m_my_timer_instance);

    2). created a single shot timer in timers_init():

    err_code = app_timer_create(&m_my_timer_instance,
    APP_TIMER_MODE_SINGLE_SHOT,
    sensor_timer_handler);
    APP_ERROR_CHECK(err_code);

    3). now i am not getting how to use this shared by you:

    uint16_t rand_value;
    uint8_t bytes_available = 0;

    (void)sd_rand_application_bytes_available_get(&bytes_available);
    if (bytes_available >= sizeof(rand_value))
    {
    err_code = sd_rand_application_vector_get(&rand_value, sizeof(rand_value));
    APP_ERROR_CHECK(err_code);
    } else {
    /* implement your error handling if no rand bytes available */
    }

    err_code = app_timer_start(m_my_timer_instance, APP_TIMER_TICKS((rand_value % 1000) + 2000), NULL);

    as i dont know how to use it in handler, also i have to add this timer before timer used in line 357 of my code.

    pls hlp

Reply Children
Related