#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define BUTTON_PIN 15
// Declare a TaskHandle_t to manage the task state
TaskHandle_t TaskHandle_0;
void Core0Task(void* pvParameters) {
// This task will run on core 0 and do nothing but delay itself.
for (;;) {
Serial.print("Task running on core ");
Serial.println(xPortGetCoreID());
delay(500); // Task action goes here.
}
}
void setup() {
// Initialize the button pin as an input.
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Print the core number.
Serial.begin(115200);
while(!Serial);
Serial.print("Setup running on core ");
Serial.println(xPortGetCoreID());
// Create a suspended task on core 0
xTaskCreatePinnedToCore(
Core0Task, /* Task function. */
"Core0Task", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&TaskHandle_0, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
vTaskSuspend(TaskHandle_0); // Start the task suspended
}
void loop() {
// Check for button press
if (digitalRead(BUTTON_PIN) == LOW) {
// Invert the state of the task on core 0
if (eTaskGetState(TaskHandle_0) == eSuspended) {
Serial.println("Resumed");
vTaskResume(TaskHandle_0);
} else {
Serial.println("Suspended");
vTaskSuspend(TaskHandle_0);
}
// Delay to debounce the button
delay(200);
}
}