SemaphoreHandle_t xCameraMutex;
SemaphoreHandle_t xAlarmMutex;
SemaphoreHandle_t xLightMutex;
SemaphoreHandle_t xGlobalMutex; // Global mutex to prevent deadlocks
// Task 1: Security System - Core 0, Highest Priority
void SecuritySystemTask(void *pvParameters) {
while (1) {
if (xSemaphoreTake(xGlobalMutex, portMAX_DELAY) == pdTRUE) { // Take global mutex
if (xSemaphoreTake(xCameraMutex, portMAX_DELAY) == pdTRUE) {
Serial.println("SecuritySystem: Activated the Camera");
vTaskDelay(pdMS_TO_TICKS(1000));
Serial.println("SecuritySystem: Trying to trigger the Alarm");
if (xSemaphoreTake(xAlarmMutex, portMAX_DELAY) == pdTRUE) {
Serial.println("SecuritySystem: Triggered the Alarm");
vTaskDelay(pdMS_TO_TICKS(1000));
xSemaphoreGive(xAlarmMutex);
Serial.println("SecuritySystem: Released Alarm");
} else {
Serial.println("SecuritySystem: Failed to acquire Alarm");
}
xSemaphoreGive(xCameraMutex);
Serial.println("SecuritySystem: Released Camera (Mutex A)");
}
xSemaphoreGive(xGlobalMutex); // Release global mutex
}
vTaskDelay(pdMS_TO_TICKS(500));
}
}
// Task 2: Alarm System - Core 0, Lower Priority than Security Task
void AlarmSystemTask(void *pvParameters) {
while (1) {
if (xSemaphoreTake(xGlobalMutex, portMAX_DELAY) == pdTRUE) { // Take global mutex
if (xSemaphoreTake(xAlarmMutex, portMAX_DELAY) == pdTRUE) {
Serial.println("AlarmSystem: Activated the Alarm");
vTaskDelay(pdMS_TO_TICKS(1000));
Serial.println("AlarmSystem: Trying to activate the Camera");
if (xSemaphoreTake(xCameraMutex, portMAX_DELAY) == pdTRUE) {
Serial.println("AlarmSystem: Activated the Camera");
vTaskDelay(pdMS_TO_TICKS(1000));
xSemaphoreGive(xCameraMutex);
Serial.println("AlarmSystem: Released Camera");
} else {
Serial.println("AlarmSystem: Failed to acquire Camera");
}
xSemaphoreGive(xAlarmMutex);
Serial.println("AlarmSystem: Released Alarm");
}
xSemaphoreGive(xGlobalMutex); // Release global mutex
}
vTaskDelay(pdMS_TO_TICKS(500));
}
}
// Task 3: Lighting System - Core 1, Lowest Priority
void LightingSystemTask(void *pvParameters) {
while (1) {
if (xSemaphoreTake(xLightMutex, portMAX_DELAY) == pdTRUE) {
Serial.println("LightingSystem: Controlled Lights");
vTaskDelay(pdMS_TO_TICKS(1000));
Serial.println("LightingSystem: Trying to activate the Camera");
if (xSemaphoreTake(xCameraMutex, portMAX_DELAY) == pdTRUE) {
Serial.println("LightingSystem: Activated the Camera");
vTaskDelay(pdMS_TO_TICKS(1000));
xSemaphoreGive(xCameraMutex);
Serial.println("LightingSystem: Released Camera");
} else {
Serial.println("LightingSystem: Failed to acquire Camera");
}
xSemaphoreGive(xLightMutex);
Serial.println("LightingSystem: Released Lights");
}
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void setup() {
Serial.begin(115200);
xCameraMutex = xSemaphoreCreateMutex();
xAlarmMutex = xSemaphoreCreateMutex();
xLightMutex = xSemaphoreCreateMutex();
xGlobalMutex = xSemaphoreCreateMutex(); // Create global mutex
// Assigning tasks to specific cores with priorities
xTaskCreatePinnedToCore(SecuritySystemTask, "SecuritySystemTask", 2048, NULL, 3, NULL, 0); // Core 0, Highest Priority
xTaskCreatePinnedToCore(AlarmSystemTask, "AlarmSystemTask", 2048, NULL, 2, NULL, 0); // Core 0, Lower Priority
xTaskCreatePinnedToCore(LightingSystemTask, "LightingSystemTask", 2048, NULL, 1, NULL, 1); // Core 1, Lowest Priority
}
void loop() {
// Do nothing here
}