# Managing multiple tasks using Arduino and FreeRTOS

**URL:** <https://forums.freertos.org/t/managing-multiple-tasks-using-arduino-and-freertos/17646>\
**Category:** Kernel\
**Created:** [June 15, 2023, 3:42am UTC](https://forums.freertos.org/t/managing-multiple-tasks-using-arduino-and-freertos/17646 "2023-06-15T03:42:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsfw7698](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@jsfw7698](https://forums.freertos.org/u/jsfw7698)\
**Post date:** [June 15, 2023, 3:42am UTC](https://forums.freertos.org/t/managing-multiple-tasks-using-arduino-and-freertos/17646/1 "2023-06-15T03:42:55Z")

</div>

So, I’m trying to implement a code for Arduino, that reads the values of a humidity sensor, a temperature sensor and based on that opens and closes a water valve, all of this is working great, but the problem shows up when I try to show this data on the LCD display that uses I2C protocol with the LiquidCrystal library, I’ve been attempting with queues, but when I implement it everything stops working, I don’t know if this is a Starvation issue or something else, without FreeRTOS it does works.

I’m attaching the code below because I can’t upload files:

```auto
#include <LiquidCrystal_I2C.h>
#include <Arduino_FreeRTOS.h>
#include <task.h>
#include <Wire.h>
#include <queue.h>

// Pin definitions
#define MOISTURE_SENSOR_PIN A0
#define TEMPERATURE_SENSOR_PIN A1
#define LED_PIN 13

// LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns, 2 rows

// Global variables
float moistureReading = 0.0;
float temperatureReading = 0.0;
bool valveState = false;
const int AirValue = 452; // You need to replace this value with Value_1
const int WaterValue = 220; // You need to replace this value with Value_2
int soilMoistureValue = 0;
int soilMoisturePercent = 0;

void moistureTask( void *pvParameters );
void temperatureTask( void *pvParameters );
void controlTask( void *pvParameters );
void ledTask( void *pvParameters );
void lcdTask( void *pvParameters );

// Tamaño de la cola
#define QUEUE_SIZE 5

// Declaración de la cola
QueueHandle_t queue;

void setup(){
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();

  pinMode(LED_PIN,OUTPUT);

  queue = xQueueCreate(QUEUE_SIZE, sizeof(int));

  xTaskCreate(moistureTask,"Task 1",128,NULL,1,NULL);
  xTaskCreate(temperatureTask,"Task 2",128,NULL,1,NULL);
  xTaskCreate(lcdTask,"Task 5",128,NULL,1,NULL);
  xTaskCreate(controlTask,"Task 3",128,NULL,3,NULL);
  xTaskCreate(ledTask,"Task 4",128,NULL,2,NULL);
  vTaskStartScheduler();
}

void loop (){

}

void moistureTask(void *pvParameters){
  
  while(1) {
    
    int soilMoistureValue = analogRead(MOISTURE_SENSOR_PIN);

    
    soilMoisturePercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
    
    xQueueSend(queue, &soilMoistureValue, portMAX_DELAY);

    if (soilMoisturePercent >= 100) {
      Serial.println("100%");
    }
    else if (soilMoisturePercent <= 0) {
      Serial.println("0%");
    }
    else if (soilMoisturePercent > 0 && soilMoisturePercent < 100) {
      Serial.print(soilMoisturePercent);
      Serial.println("%");
    }

    vTaskDelay(pdMS_TO_TICKS(500)); // Delay 500ms
  }
}

void temperatureTask(void *pvParameters){
  
    while(1) {
    
    int sensorValue = analogRead(TEMPERATURE_SENSOR_PIN);
    temperatureReading = (sensorValue * 5.0 / 1024.0) * 100.0;
    Serial.print("Temperature: ");
    Serial.println(temperatureReading);

    
    vTaskDelay(pdMS_TO_TICKS(500)); // Delay 0.5s
  }
}

void controlTask(void *pvParameters){
  
    while(1) {
    
    if (soilMoisturePercent < 30) {
      valveState = true;
      Serial.println("Valve is open");
    } else {
      valveState = false;
      Serial.println("Valve is closed");
    }

    vTaskDelay(pdMS_TO_TICKS(100)); // Delay 100ms
  }
}

void ledTask(void *pvParameters){
  
    while(1) {
    // Update LED based on valve state
    if (valveState) {
      digitalWrite(LED_PIN, HIGH);
    } else {
      digitalWrite(LED_PIN, LOW);
    }

    vTaskDelay(pdMS_TO_TICKS(100)); // Delay 100ms
  }
}

// Tarea que lee el valor de la cola y lo muestra en la pantalla LCD
void lcdTask(void *pvParameters) {
  while (1) {
    int receivedValue;
    if (xQueueReceive(queue, &receivedValue, portMAX_DELAY)) {
      lcd.setCursor(7, 0); // Posición en la pantalla para mostrar el valor
      lcd.print(receivedValue);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [June 15, 2023, 5:01am UTC](https://forums.freertos.org/t/managing-multiple-tasks-using-arduino-and-freertos/17646/2 "2023-06-15T05:01:06Z")

</div>

Have you tried increasing the stack size of the tasks, especially the LCD task. Depending on the implementation of the LCD library it might be consuming more stack size than128 words.

---

<div class="post-metadata">

**Author:** ![jsfw7698](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@jsfw7698](https://forums.freertos.org/u/jsfw7698)\
**Post date:** [June 15, 2023, 4:18pm UTC](https://forums.freertos.org/t/managing-multiple-tasks-using-arduino-and-freertos/17646/3 "2023-06-15T16:18:12Z")

</div>

Just tried that, still doesn’t work

---

<div class="post-metadata">

**Author:** ![ActoryOu](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/actoryou/32/2880_2.png) [@ActoryOu](https://forums.freertos.org/u/ActoryOu)\
**Post date:** [June 16, 2023, 1:49am UTC](https://forums.freertos.org/t/managing-multiple-tasks-using-arduino-and-freertos/17646/4 "2023-06-16T01:49:23Z")

</div>

Hi @jsfw7698,  
Do you enable [configASSERT](https://www.freertos.org/a00110.html#configASSERT) and [configCHECK\_FOR\_STACK\_OVERFLOW](https://www.freertos.org/a00110.html#configCHECK_FOR_STACK_OVERFLOW)?  
These two options help a lot to capture the problem in early stage.

Thanks.

---

<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:** [June 16, 2023, 7:22am UTC](https://forums.freertos.org/t/managing-multiple-tasks-using-arduino-and-freertos/17646/5 "2023-06-16T07:22:43Z")

</div>

> [@jsfw7698](#):
>
> Just tried that, still doesn’t work

How much did you increase? Can you break the code in the debugger and see what it is doing when it appears hang?
