#include <Arduino.h>
unsigned long setupStartTime, setupEndTime, loopStartTime, loopEndTime;
TaskHandle_t Task1;
TaskHandle_t Task2;
// LED pins
const int led1 = 2;
const int led2 = 4;
const int ldr = 5;
const int lamp = 13;
const int pir = 25;
void setup() {
Serial.begin(115200);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(ldr, INPUT);
pinMode(lamp, OUTPUT);
pinMode(pir, INPUT);
//create a task that will be executed in the Task1code() function, with priority 1 and executed on 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);
//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);
}
//Task1code:
void Task1code( void * pvParameters ){
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
for(;;){
if (digitalRead(ldr) == HIGH){
digitalWrite(led1, HIGH);
Serial.print("Noite");
Serial.print("\n");
delay(1000);
}
else {
digitalWrite(led1, LOW);
Serial.print("Dia");
Serial.print("\n");
delay(1000);
}
}
}
//Task2code:
void Task2code( void * pvParameters ){
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
for(;;){
if (digitalRead(pir)== HIGH){
digitalWrite(led2, HIGH);
setupStartTime = micros();
Serial.print("Movimento detectado");
Serial.print("\n");
delay(1000);
}
else{
digitalWrite(led2, LOW);
}
}
}
void loop() {
if (digitalRead(led1) == HIGH && digitalRead(led2) == HIGH){
digitalWrite(lamp, HIGH);
setupEndTime = micros(); // Fim da medição do tempo de execução do setup
Serial.print("Lâmpada acesa");
Serial.print("\n");
Serial.print("Tempo de execucao da função: ");
Serial.print(setupEndTime - setupStartTime);
Serial.println("us");
delay(10000);
digitalWrite(lamp, LOW);
}
}