-1

I want to use an ultrasonicsensor (HC-SR04) and a TFT Display (Sainsmart ST7735) with my Arduino Uno and FreeRTOS. The Display is similar to Adafruit ST7735. I have to do a multi-threading project with FreeRTOS and i thought this would be nice and easy.

I run the code and the Display only shows: Distance: 0 cm

#include <Arduino_FreeRTOS.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>

#define TFT_CS   10
#define TFT_RST  9
#define TFT_DC   8

#define TRIGGER_PIN 6
#define ECHO_PIN    7

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

// Global variable to store ultrasonic sensor value
volatile unsigned long ultrasonicValue = 0;

// Ultrasonic sensor reading task
void ultrasonicTask(void *pvParameters) {
  (void) pvParameters;
  
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  for (;;) {
    // Send ultrasonic pulse
    digitalWrite(TRIGGER_PIN, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIGGER_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIGGER_PIN, LOW);

    // Read the pulse duration
    unsigned long pulseDuration = pulseIn(ECHO_PIN, HIGH);

    // Calculate distance in cm (divide by 58 to convert to cm)
    ultrasonicValue = pulseDuration / 58;

    // Delay before the next reading
    vTaskDelay(pdMS_TO_TICKS(1000));
  }
}

// Display task
void displayTask(void *pvParameters) {
  (void) pvParameters;

  tft.initR(INITR_BLACKTAB);
  tft.setRotation(3);
  tft.fillScreen(ST7735_BLACK);
  tft.setTextColor(ST7735_WHITE);
  tft.setTextSize(2);
  
  for (;;) {
    // Clear the display
    tft.fillScreen(ST7735_BLACK);

    // Display the ultrasonic value
    tft.setCursor(0, 0);
    tft.print("Distance: ");
    tft.print(ultrasonicValue);
    tft.print(" cm");

    // Update the display every 500 milliseconds
    vTaskDelay(pdMS_TO_TICKS(500));
  }
}

void setup() {
  Serial.begin(9600);
  
  // Create the tasks
  xTaskCreate(ultrasonicTask, "UltrasonicTask", 128, NULL, 1, NULL);
  xTaskCreate(displayTask, "DisplayTask", 128, NULL, 2, NULL);

  // Start the FreeRTOS scheduler
  vTaskStartScheduler();
}

void loop() {
  // Empty loop since tasks are running in FreeRTOS
}

I expect the display to show the current distance measured with the ultrsonicsensor.

I also tried printing an variable i and increase it's value every iteration. This only works with vTaskDelay(pdMS_TO_TICKS(10)); not with vTaskDelay(pdMS_TO_TICKS(1000));. The 'ultrasonicValue' hasn't changed in any modified test run. It seems that the tft Display has Problems with the delay.

  • Task stack is probably too small. You should enable FreeRTOS stack checking and define configASSERT properly. There was an analog question time ago regarding the measurement here. You’ve to enclose the measurement sequence setting the GPIO pins by a critical section or disable interrupts globally because it relies on exact timing. So the sequence shouldn’t be interrupted by anything else. – HS2 Jul 25 '23 at 06:37

1 Answers1

0

Problem was the stack like https://meta.stackoverflow.com/users/8641242/hs2 mentioned