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

Reply Children
Related