# Can't make a delay inside a task

**URL:** <https://forums.freertos.org/t/cant-make-a-delay-inside-a-task/9914>\
**Category:** Kernel\
**Created:** [May 29, 2020, 1:17pm UTC](https://forums.freertos.org/t/cant-make-a-delay-inside-a-task/9914 "2020-05-29T13:17:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![magdii](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@magdii](https://forums.freertos.org/u/magdii)\
**Post date:** [May 29, 2020, 1:17pm UTC](https://forums.freertos.org/t/cant-make-a-delay-inside-a-task/9914/1 "2020-05-29T13:17:43Z")

</div>

i am a new learner and i am using freeRTOS on ATmega32 to just flash 1 LED, I read many topics on this forum and other websites and my code is just the same but still doesn’t work correctly, it’s cause the led to stay on forever without flashing, can you help me?

> ```
> #define F_CPU 8000000UL
> #include <avr/io.h>
> #include <util/delay.h>
> 
> #include "FreeRTOS.h"
> #include "task.h"
> #include "croutine.h"
> #include "FreeRTOSConfig.h"
> #include "semphr.h"
> #include <avr/interrupt.h>
> 
> /* Define all the tasks */
> static void ledBlinkingtask1(void* pvParameters);
> 
> int main(void) {
> /* Call FreeRTOS APIs to create tasks, all tasks has the same priority "1" with the
> same stack size*/
> xTaskCreate( ledBlinkingtask1,"LED1",
> configMINIMAL_STACK_SIZE, NULL, 1, NULL );
> 	
> 
> // Start the RTOS kernel
> vTaskStartScheduler();
> 	
> /* Do nothing here and just run infinite loop */	
> while(1){}		
> return 0;
> }
> 
> static void ledBlinkingtask1(void* pvParameters){
> DDRB=1;
> while (1)
> {
> // LED on
> PORTB = 1;
> vTaskDelay(pdMS_TO_TICKS(500));
> 
> // LED off
> PORTB = 0;
> vTaskDelay(pdMS_TO_TICKS(500));	
> 
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [May 29, 2020, 4:22pm UTC](https://forums.freertos.org/t/cant-make-a-delay-inside-a-task/9914/2 "2020-05-29T16:22:38Z")

</div>

Are you sure the writes to the PORTB are running the LED on and off as you expect? You can try that by just using those lines of code from main().

Is the first task starting? You can know that by setting a break point in the code to see if it is ever hit.

If you stop the code on the debugger, where is it? What is it doing?

Is you tick interrupt working? You can know that by setting a break point in xTaskIncrementTick() in FreeRTOS/Source/tasks.c, or otherwise viewing the xTickCount variable in the debugger.

---

<div class="post-metadata">

**Author:** ![magdii](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@magdii](https://forums.freertos.org/u/magdii)\
**Post date:** [May 30, 2020, 4:27pm UTC](https://forums.freertos.org/t/cant-make-a-delay-inside-a-task/9914/3 "2020-05-30T16:27:08Z")

</div>

after working through your advice:  
portB works fine  
the first task starting, but the problem is in first  
vTaskDelay(pdMS\_TO\_TICKS(500));  
it just doesn’t work, when the debugger reaches this statement it stops and debugger button goes all grey out except for reset button that’s why it keeps on and never go to the off statement.  
vTaskDelay() is the problem I don’t know why I just used it as in the freertos book examples  
regarding the xTaskIncrementTick() …the debugger never reaches it too…it greys out before stopping at the break point

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [May 30, 2020, 6:11pm UTC](https://forums.freertos.org/t/cant-make-a-delay-inside-a-task/9914/4 "2020-05-30T18:11:24Z")

</div>

It is going to be tricky unless you can get the debugger working. Can you step into the vTaskDelay() function to see where it gets to? I suspect it will execute all the way to the point where it attempts to yield to another task. If not, it might just execute until the tick interrupt occurs. If we can determine which it will give us a clue as to what to look at next.

There is a description of how context switches operate on AVR parts here [https://www.freertos.org/implementation/a00019.html](https://www.freertos.org/implementation/a00019.html).

Also, have you done things like check for stack overflows? This page points to that and other useful debugging ideas: [https://www.freertos.org/FAQHelp.html](https://www.freertos.org/FAQHelp.html)
