Arduino Rtos problem

When I tried to run this program the program haltat println("starting..") line . Sometimes it stops at the startin here is my code

#include <SPI.h>
#include "ADS1X15.h"
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Arduino_FreeRTOS.h>


// Variables for storing GPS and sensor data
float lat, lng, speed, voltage, current;

// ADS1115 object for analog-to-digital conversion
ADS1115 ADS(0x48);

// SoftwareSerial object for GPS communication
SoftwareSerial ss(4, 3);

// TinyGPSPlus object for parsing GPS data
TinyGPSPlus gps;

// Task function prototypes
void Read_GPS( void *pvParameters );
void Read_Voltage_Current( void *pvParameters );
void Send_Data( void *pvParameters );
void Blink_LED( void *pvParameters );


void setup() {
  Serial.begin(115200);
  Serial.print("Starting...");
  ss.begin(9600);
  Wire.begin();
  ADS.begin();
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV2);

  // Create tasks for different functionalities
  xTaskCreate(Read_GPS, "Read_GPS", 128, NULL, 1, NULL);
  xTaskCreate(Read_Voltage_Current, "Read_Voltage_Current", 128, NULL, 1, NULL);
  xTaskCreate(Send_Data, "Send_Data", 256, NULL, 1, NULL);
  xTaskCreate(Blink_LED, "Blink_LED", 64, NULL, 1, NULL);
}

void loop() {
  // The loop is empty as tasks handle the main functionality
}

// Task to blink LEDs
void Blink_LED(void *pvParameters) {
  (void) pvParameters;
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  for(;;) {
    
    // Blink LEDs in a pattern
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    vTaskDelay(5000 / portTICK_PERIOD_MS);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    vTaskDelay(100 / portTICK_PERIOD_MS);
  }
}

// Task to read voltage and current data
void Read_Voltage_Current(void *pvParameters) {
  (void) pvParameters;
  for(;;) {
    // Read sensor data
    int16_t V_val = ADS.readADC(0);
    int16_t C_val = ADS.readADC(1);
    float f = ADS.toVoltage(2);
    voltage = V_val * f;
    current = C_val * f;
    Serial.println(voltage,6);
    vTaskDelay(1000 / portTICK_PERIOD_MS);
  }
}

// Task to read GPS data
void Read_GPS(void *pvParameters) {
  (void) pvParameters;
  while (1) {
    // Read GPS data
    while (ss.available() > 0) {
      gps.encode(ss.read());
      if (gps.location.isUpdated()) {
        lat = gps.location.lat();
        lng = gps.location.lng();
        Serial.println(lat,6);
      }
    }
  }
}

// Task to send data
void Send_Data(void *pvParameters) {
  (void) pvParameters;
  while (1) {
    // Send data
    float data[5] = { lat, lng, speed, voltage, current };
    digitalWrite(SS, LOW);
    for (int i = 0; i < 5; i++) {
      byte *dataByte = (byte *)&data[i];
      for (int j = 0; j < sizeof(float); j++) {
        SPI.transfer(dataByte[j]);
      }
    }
    for (int i = 0; i < 5; i++) {
      Serial.println(data[i]);
    }
    digitalWrite(SS, HIGH);
    vTaskDelay(5 / portTICK_PERIOD_MS);
  }
}

Which Arduino are you using, and why are you using SoftwareSerial?

I suspect that SoftwareSerial and FreeRTOS are not compatible.

1 Like

I am using Arduino uno, and software serial is for gps module

directly from the repo docs:

Errata

Testing with the Software Serial library shows some incompatibilities at low baud rates (9600), due to the extended time this library disables the global interrupt. Use the hardware USARTs.

Using hardware uart solve this issue? If so will this effect the serial.print statement

Use the hardware UART, or don't use RTOS.

RTOS is not needed to accomplish the tasks performed by the program you posted.

Time is more important since I need to insert these values to the pid controller for further processing

Your comment seems irrelevant.

RTOS is not needed, as you can use millis() to accurately control the timing of all programmed events. In fact, you can control it more accurately than when using FreeRTOS, since the basic tick rate is 15 ms (very roughly).

Oh ok. Lemme look into it. Thanks for the help

See this excellent tutorial: Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.