/*
2_8_2_blink_LEDs_dual_core
We create two tasks and assign one task to core 0 and another
to core 1. Arduino sketches run on core 1 by default.
• The ESP32 is dual-core;
• Arduino sketches run on core 1 by default;
• To use core 0, you need to create tasks;
• You can use the xTaskCreatePinnedToCore() to a specific core;
• Using this method, you can run two different tasks independently and
simultaneously using the two cores.
*/
// Start by creating a task handle for Task1 and Task2
// called Task1 and Task2.
TaskHandle_t Task1;
TaskHandle_t Task2;
// LED pins assigned
const int led1 = 2;
const int led2 = 4;
void setup() {
Serial.begin(115200);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
// Create Task1 using the xTaskCreatePinnedToCore() function:
// Task1 will be implemented with the Task1code() function.
// So, we need to create that function later in the code.
// We give the task priority 1, and pinned it to core 0.
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
// We create Task2 using the same method:
// Create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task2code, /* Task function. */
"Task2", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
delay(500);
}
// After creating the tasks, we need to create the functions that will execute those tasks.
// Task1code: blinks an LED every 1000 ms
void Task1code( void * pvParameters ){
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
for(;;){ //(;;)this creates an infinite loop
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
delay(1000);
}
}
//Task2code: blinks an LED every 700 ms
void Task2code( void * pvParameters ){
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
for(;;){
digitalWrite(led2, HIGH);
delay(700);
digitalWrite(led2, LOW);
delay(700);
}
}
void loop() {
}