Advertisement
pleasedontcode

FreeRTOS on ESP32

Aug 27th, 2024
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.45 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include <freertos/FreeRTOS.h>
  3. #include <freertos/task.h>
  4.  
  5. // Task function prototypes
  6. void TaskMotorControl(void *pvParameters);
  7. void TaskSensorData(void *pvParameters);
  8.  
  9. void setup() {
  10.     Serial.begin(115200);
  11.  
  12.     // Create Motor Control Task
  13.     xTaskCreate(
  14.         TaskMotorControl,  // Task function
  15.         "Motor Control",   // Task name
  16.         2048,              // Stack size in bytes
  17.         NULL,              // Task input parameter
  18.         2,                 // Priority (higher number = higher priority)
  19.         NULL);             // Task handle
  20.  
  21.     // Create Sensor Data Task
  22.     xTaskCreate(
  23.         TaskSensorData,    // Task function
  24.         "Sensor Data",     // Task name
  25.         2048,              // Stack size in bytes
  26.         NULL,              // Task input parameter
  27.         1,                 // Priority
  28.         NULL);             // Task handle
  29. }
  30.  
  31. void loop() {
  32.     // FreeRTOS tasks run independently of the loop
  33. }
  34.  
  35. void TaskMotorControl(void *pvParameters) {
  36.     while (true) {
  37.         // Example motor control code
  38.         Serial.println("Controlling motor...");
  39.         vTaskDelay(pdMS_TO_TICKS(100));  // Delay for 100 milliseconds
  40.     }
  41. }
  42.  
  43. void TaskSensorData(void *pvParameters) {
  44.     while (true) {
  45.         // Example sensor data acquisition code
  46.         Serial.println("Reading sensor data...");
  47.         vTaskDelay(pdMS_TO_TICKS(500));  // Delay for 500 milliseconds
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement