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
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
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
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
The warning you're seeing is due to the types not matching.
You can either cast the input, like so:
err_code = sd_rand_application_vector_get((uint8_t *)&rand_value, sizeof(rand_value));
or declare rand_value as a uint8_t array: uint8_t rand_value[2];
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()).
Kind regards,
Håkon
Hakon i tried cast input function you are telling also, still no random delay is showing with the timer. pls tell what is missing in the code i have shrd above
That is why i am now asking you how can i use the random timer.
Do not call the timer logic from the timer handler itself.
You should call that from your main thread.
Kind regards,
Håkon
Do not call the timer logic from the timer handler itself.
You should call that from your main thread.
Kind regards,
Håkon
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