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  

  • 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

  • Could you please explain what is the issue that you are seeing?

     

    Kind regards,

    Håkon

  • 1). in line 158: i have added

    APP_TIMER_DEF(m_my_timer_instance);

    2). in line 573: i have added

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

    3). i aim to add a random delay before the repeated timer of line 374 starts.

    as suggested by you: in line 357 i have added

    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);
     

    4). i am getting error as shown in pic

    5). i am not able to add the random delay

Reply
  • 1). in line 158: i have added

    APP_TIMER_DEF(m_my_timer_instance);

    2). in line 573: i have added

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

    3). i aim to add a random delay before the repeated timer of line 374 starts.

    as suggested by you: in line 357 i have added

    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);
     

    4). i am getting error as shown in pic

    5). i am not able to add the random delay

Children
Related