TaskHandle_t taskAHandle;
TaskHandle_t taskBHandle;
void TaskA(void *pvParameters) {
while (1) {
if (Serial.available()) {
String input = Serial.readStringUntil('\n'); // read until newline
// Allocate heap memory for the message
char *msg = (char *)pvPortMalloc(input.length() + 1);
if (msg != NULL) {
strcpy(msg, input.c_str());
// Notify Task B and pass pointer via notification value
xTaskNotify(taskBHandle, (uint32_t)msg, eSetValueWithOverwrite);
}
}
vTaskDelay(pdMS_TO_TICKS(10)); // small delay to yield CPU
}
}
void TaskB(void *pvParameters) {
while (1) {
uint32_t msgPtr;
// Wait indefinitely for notification from Task A
if (xTaskNotifyWait(0, 0, &msgPtr, portMAX_DELAY) == pdTRUE) {
char *msg = (char *)msgPtr;
Serial.print("Task B received: ");
Serial.println(msg);
// Free heap memory after use
vPortFree(msg);
}
}
}
void setup() {
Serial.begin(115200);
// Create Task A
xTaskCreate(TaskA, "TaskA", 4096, NULL, 1, &taskAHandle);
// Create Task B
xTaskCreate(TaskB, "TaskB", 4096, NULL, 1, &taskBHandle);
}
void loop() {
// Empty, tasks run independently
}