# Delay between setting each variable

**URL:** <https://forums.freertos.org/t/delay-between-setting-each-variable/18276>\
**Category:** Kernel\
**Created:** [October 3, 2023, 6:43pm UTC](https://forums.freertos.org/t/delay-between-setting-each-variable/18276 "2023-10-03T18:43:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohinidr](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@rohinidr](https://forums.freertos.org/u/rohinidr)\
**Post date:** [October 3, 2023, 6:43pm UTC](https://forums.freertos.org/t/delay-between-setting-each-variable/18276/1 "2023-10-03T18:43:11Z")

</div>

Hi

I have a case where I need to set the delay . The delay varies between 5-15 seconds . I am using STM32 . below is my code . I want to modify these variables when these delays expire . But I do not wan to block the task. How do I achieve this in FreeRTOS.

Any help appreciated

```auto
 void test_sequence(void) {
    if (complete) {
delay(5sec);
        enable = true;
        delay(10sec);
        remote_setpoint = 500;
delay(15sec);
        remote_setpoint_confirm = CONFIRM;
    }

```

Thanks

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [October 3, 2023, 7:32pm UTC](https://forums.freertos.org/t/delay-between-setting-each-variable/18276/2 "2023-10-03T19:32:36Z")

</div>

I would use a timer callback to set those variables at the point in the future.

See: [FreeRTOS - RTOS software timer functionality and features description](https://freertos.org/RTOS-software-timer.html)

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [October 3, 2023, 8:11pm UTC](https://forums.freertos.org/t/delay-between-setting-each-variable/18276/3 "2023-10-03T20:11:25Z")

</div>

Or use [RTOS - xTaskCheckForTimeOut()](https://www.freertos.org/xTaskCheckForTimeOut.html) (see the example code) to poll for timeouts if your task runs without waiting for something (forever).

---

<div class="post-metadata">

**Author:** ![rohinidr](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@rohinidr](https://forums.freertos.org/u/rohinidr)\
**Post date:** [October 3, 2023, 9:34pm UTC](https://forums.freertos.org/t/delay-between-setting-each-variable/18276/4 "2023-10-03T21:34:49Z")

</div>

Thanks Richard for responding. The software timers are static right ? Are you suggesting me to create 3 timers in this case (for 5,10 and 15 sec).

All I want to do is when 5 seconds expires set 1 st variable and when 10 secs expires set 2 nd variable and when 15 sec expires set 3rd variable

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [October 4, 2023, 12:38am UTC](https://forums.freertos.org/t/delay-between-setting-each-variable/18276/5 "2023-10-04T00:38:19Z")

</div>

When you create a timer, you connect a subroutine that will be called when the timer expires to that timer. That timer can be used and reused as needed. You can change the timeout period for the timer (and make it auto-restart for periodic activation if desired).

In your case you could either create 3 separate timers, each that calls a function that sets the appropriate variable, or you could create 1 timer that calls a function that keeps track of what “step” it is on, setting the appropriate variable for that step, and then restarting the timer for when the next step should occur. Then when the condition occurs that you want to trigger them your start the timer(s) and things will happen when they expire.

---

<div class="post-metadata">

**Author:** ![moninom1](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/moninom1/32/4343_2.png) [@moninom1](https://forums.freertos.org/u/moninom1)\
**Post date:** [October 5, 2023, 5:15am UTC](https://forums.freertos.org/t/delay-between-setting-each-variable/18276/6 "2023-10-05T05:15:00Z")

</div>

Hi @rohinidr

Adding to what @richard-damon mentioned, To do it in a single function you can use auto-reload timers which gets periodic callback execution and set the auto reload timer period according to your use case which seems to be 5seconds here.

Please check [more details here](https://freertos.org/One-shot-Vs-auto-reload-real-time-software-timers.html).
