Time error on freeRTOS

I using an Arduino Uno to run my code. When i use seconds to test my tasks it works fine. But when i change it to the correct times, it simply does not work. 6 hours => 40 seconds, 18 hours =>52 seconds, 5 minutes => 40 seconds. I dont wtf is happening, pls help me. This is my code:

#include <Arduino_FreeRTOS.h>
#include <dht11.h>

// Pinos utilizados
const int rele13 = 13;
const int rele12 = 12;
const int rele11 = 11;
const int rele10 = 10;
const int sensorUmidade = A0;
#define DHT11PIN 5

// Variáveis globais
float valorSensorTemperatura;
dht11 DHT11;

const int AirValue = 540;   // you need to replace this value with Value_1
const int WaterValue = 260;  // you need to replace this value with Value_2
int intervals = (AirValue - WaterValue)/3;
int soilMoistureValue = 0;

void tarefaRele13(void *pvParameters) {
  while (1) {
    digitalWrite(rele13, HIGH); // Ligar rele 13
    vTaskDelay(18 * 60 * 60 * 1000 / portTICK_PERIOD_MS); // 18 horas
    digitalWrite(rele13, LOW); // Desligar rele 13
    vTaskDelay(6 * 60 * 60 * 1000 / portTICK_PERIOD_MS); // 6 horas
  }
}

void tarefaSensorUmidade(void *pvParameters) {
  while (1) {
    soilMoistureValue = analogRead(sensorUmidade);
    //Serial.println(soilMoistureValue);
    if (soilMoistureValue < AirValue && soilMoistureValue > (AirValue - intervals)) { // Valor especĂ­fico para solo seco
      digitalWrite(rele12, HIGH);
      delay(100);
      digitalWrite(rele12, LOW);
      delay(3000);
      digitalWrite(rele12, HIGH);
      delay(100);
      digitalWrite(rele12, LOW);
      vTaskDelay(5 * 60 * 1000 / portTICK_PERIOD_MS); // 5 minutos
    }
    
  }
}

void tarefaSensorTemperatura(void *pvParameters) {
    while (1) {
        int chk = DHT11.read(DHT11PIN);
        valorSensorTemperatura = DHT11.temperature;
        //Serial.print("Temperature (°C): ");
        //Serial.println(valorSensorTemperatura);
        if(valorSensorTemperatura < 23){
            digitalWrite(rele10, HIGH);
            delay(5000);
            digitalWrite(rele10, LOW);
          }
        
        vTaskDelay(5 * 60 * 1000 / portTICK_PERIOD_MS);
    }
}


void setup() {
    pinMode(rele13, OUTPUT);
    pinMode(rele12, OUTPUT);
    pinMode(rele11, OUTPUT);
    pinMode(rele10, OUTPUT);
    Serial.begin(9600);
    xTaskCreate(tarefaRele13, "TarefaRele13", configMINIMAL_STACK_SIZE, NULL, 4, NULL);
    xTaskCreate(tarefaSensorUmidade, "TarefaSensorUmidade", configMINIMAL_STACK_SIZE, NULL, 3, NULL);
    xTaskCreate(tarefaSensorTemperatura, "TarefaSensorTemperatura", configMINIMAL_STACK_SIZE, NULL, 2, NULL);
    vTaskStartScheduler();
}

void loop() {
    // Não é necessário nenhum código aqui
}

First, check the size of an int on your machine, if it is only 16 bits, it can’t store a value above 32767 so you calculations will overflow and get wrong values.

Second, check the size you have defined your Tick to be, if it is only 16 bits, then the biggest delay can only be 65535 ticks, which if you are running the tick at 1000 Hz, is just over a minute.

If you need long delays, you need to use a 32 bit tick.

1 Like