# FreeRTOS hang issue

**URL:** <https://forums.freertos.org/t/freertos-hang-issue/21911>\
**Category:** Community Media\
**Tags:** tutorial, monitoring\
**Created:** [November 19, 2024, 4:21pm UTC](https://forums.freertos.org/t/freertos-hang-issue/21911 "2024-11-19T16:21:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronyio](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/ronyio/32/5138_2.png) [@ronyio](https://forums.freertos.org/u/ronyio)\
**Post date:** [November 19, 2024, 4:21pm UTC](https://forums.freertos.org/t/freertos-hang-issue/21911/1 "2024-11-19T16:21:44Z")

</div>

Hi There,  
I have two task. task2 works fine, but when task1 trigger then CPU got hang.  
How to fix it?

CPU: Arduino UNO (ATmega328P)  
Code is following:

```auto
#include <Arduino_FreeRTOS.h>

/ *******************xTask for**************************** /
void scanButton(void *pvParameters)  
{
  int itr=0;
  bool readBtnStatus;
  
  pinMode(11, INPUT);
  pinMode(0, OUTPUT);

  while(1)
  {
    readBtnStatus = digitalRead(11);   
    vTaskDelay( 100);

    if(readBtnStatus)
    {
      
      if(itr >= 2)
      {
        digitalWrite(0, HIGH);
        itr = 0;
      }

      else
      {
        digitalWrite(0, LOW);
        itr++;
      }
      
    }
 
    //vTaskDelay(10);
     
  }

}

/ *******************xTask for**************************** /
void controlLoad(void *pvParameters)  
{
  bool lowswitch = 0;
  bool highswitch = 0;
  bool passswitch = 0;
  
  pinMode(1,INPUT); //low switch
  pinMode(2,INPUT); //pass switch
  pinMode(4,INPUT); //high switch

  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  

  while(1)
  {
    lowswitch = digitalRead(1);
    passswitch = digitalRead(2);
    highswitch = digitalRead(4);

    if (lowswitch == 1 && highswitch == 0 && passswitch == 0)
    {
      digitalWrite(10,LOW);
      digitalWrite(9,HIGH);
    }

    else if (lowswitch == 0 && highswitch == 1 && passswitch == 0)
    {
      digitalWrite(10,HIGH);
      digitalWrite(9,LOW);
    }

    else if (lowswitch == 0 && highswitch == 0 && passswitch == 1)
    {
      digitalWrite(10,HIGH);
      vTaskDelay( 150 );
      digitalWrite(10,LOW);
      vTaskDelay( 150 );
      digitalWrite(9,LOW);
    }

    else if (lowswitch == 1 && highswitch == 0 && passswitch == 1)
    {
      digitalWrite(9,LOW);
      digitalWrite(10,HIGH);
      vTaskDelay( 150 );
      digitalWrite(10,LOW);
      digitalWrite(9,HIGH);
      vTaskDelay( 150 );
    }

    else if (lowswitch == 0 && highswitch == 1 && passswitch == 1)
    {
      
      digitalWrite(10,LOW);
      digitalWrite(9,HIGH);
      vTaskDelay( 150 );
      digitalWrite(9,LOW);
      digitalWrite(10,HIGH);
      vTaskDelay( 150 );
    }

    else if (lowswitch == 1 && highswitch == 1)
    {
      digitalWrite(10,HIGH);
      digitalWrite(9,HIGH);
    }

    
    else
    {
      digitalWrite(10,LOW);
      digitalWrite(9,LOW);
    }
    
    vTaskDelay(10);
  }

}

/ *******************Setup**************************** /
void setup()
{
  Serial.begin(9600);

  xTaskCreate(scanButton, "task1", 512, NULL, 2, NULL );
  xTaskCreate(controlLoad, "task2", 512, NULL, 1, NULL );
}

void loop()
{

}

```

---

<div class="post-metadata">

**Author:** ![rohitmdn](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rohitmdn/32/5490_2.png) [@rohitmdn](https://forums.freertos.org/u/rohitmdn)\
**Post date:** [November 19, 2024, 5:38pm UTC](https://forums.freertos.org/t/freertos-hang-issue/21911/2 "2024-11-19T17:38:51Z")

</div>

Did you try stepping through your code with a debugger to know where exactly task 1 (scanButton) hangs ?  
As some quick fixes from a FreeRTOS point of view you could probably try:-

- Increasing the stack size of your tasks, can you check if this works ? Also, you can check how close you came to stack overflow by [this](https://www.freertos.org/Documentation/02-Kernel/04-API-references/03-Task-utilities/04-uxTaskGetStackHighWaterMark) FreeRTOS API, as well as can check [FreeRTOS stack usage and stack overflow checking](https://www.freertos.org/Documentation/02-Kernel/02-Kernel-features/09-Memory-management/02-Stack-usage-and-stack-overflow-checking?_ga=2.176866341.1243152115.1731827987-536414459.1721626045)
- You might want to define `configASSERT` for development.

> [@ronyio](#):
>
> `//vTaskDelay(10);`

Just curious, are you sure you want to keep this commented in the scanButton task?

---

<div class="post-metadata">

**Author:** ![karahulx](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/karahulx/32/4893_2.png) [@karahulx](https://forums.freertos.org/u/karahulx)\
**Post date:** [November 19, 2024, 5:57pm UTC](https://forums.freertos.org/t/freertos-hang-issue/21911/3 "2024-11-19T17:57:22Z")

</div>

In addition to what @rohitmdn suggested , you need to start the scheduler as well in the setup function.

```auto
void setup()
{
  Serial.begin(9600);

  xTaskCreate(scanButton, "task1", 512, NULL, 2, NULL );
  xTaskCreate(controlLoad, "task2", 512, NULL, 1, NULL );

  vTaskStartScheduler();
}

```

Is it a copy/paste error here?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 20, 2024, 5:43am UTC](https://forums.freertos.org/t/freertos-hang-issue/21911/4 "2024-11-20T05:43:00Z")

</div>

You do not need to call `vTaskStartScheduler` explicitly as Arduino setup code does that - [Arduino\_FreeRTOS\_Library/src/variantHooks.cpp at master · feilipu/Arduino\_FreeRTOS\_Library · GitHub](https://github.com/feilipu/Arduino_FreeRTOS_Library/blob/master/src/variantHooks.cpp#L58).

Here are few more suggestions -

1. Check the return value of `xTaskCreate` to ensure that tasks are successfully created.
2. Just blink 2 different LEDs in the 2 tasks and see if you can get that to work.
3. Once that works, start adding your code to narrow down which part of your code causes the application to hang.

---

<div class="post-metadata">

**Author:** ![ronyio](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/ronyio/32/5138_2.png) [@ronyio](https://forums.freertos.org/u/ronyio)\
**Post date:** [November 20, 2024, 2:31pm UTC](https://forums.freertos.org/t/freertos-hang-issue/21911/5 "2024-11-20T14:31:39Z")

</div>

I have tried 1 to 3. Task creation is fine.  
Can’t find any issue when two task run separately.

Is there any suggestion for me?

---

<div class="post-metadata">

**Author:** ![karahulx](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/karahulx/32/4893_2.png) [@karahulx](https://forums.freertos.org/u/karahulx)\
**Post date:** [November 20, 2024, 7:36pm UTC](https://forums.freertos.org/t/freertos-hang-issue/21911/6 "2024-11-20T19:36:05Z")

</div>

You can try reducing the stack size to 128 for the tasks as the ATmega328P only has 2kB of RAM. Did you enable stack overflow checking as mentioned by @rohitmdn to see that stack is sufficient even after the tasks are created?
