#include <Arduino_FreeRTOS.h>
#include <task.h>
volatile bool Detected = false;
volatile bool Active = true;
void setup() {
pinMode(2, INPUT);
pinMode(9, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(10, INPUT_PULLUP);
Serial.begin(9600);
xTaskCreate(TaskDetection, "Detection", 256, NULL, 2, NULL);
xTaskCreate(TaskAlarm, "Alarm", 256, NULL, 1, NULL);
xTaskCreate(TaskSwitch, "Switch", 256, NULL, 3, NULL);
}
void loop() {
// Empty because tasks run in FreeRTOS
}
void TaskDetection(void *pvParameters) {
for (;;) {
Serial.println("Task2");
if (digitalRead(2) == HIGH && Active) {
Detected = true;
} else {
Detected = false;
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void TaskAlarm(void *pvParameters) {
for (;;) {
Serial.println("Task3");
if (Detected) {
digitalWrite(3, HIGH);
tone(9,500);
Serial.println("ALARM: Intrusion detected!");
} else {
digitalWrite(3, LOW);
noTone(9);
}
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
void TaskSwitch(void *pvParameters) {
for (;;) {
if (digitalRead(10) == LOW) {
Active = false;
Serial.println("System deactivated.");
digitalWrite(4, LOW);
} else {
Active = true;
Serial.println("System activated.");
digitalWrite(4, HIGH);
}
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}