#include <Arduino_FreeRTOS.h>
#include <task.h>
#include <LiquidCrystal_I2C.h> // Library for LCD
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 5;
const int buttonPin3 = 6;
const int ledhijau1 = 25; // the number of the LED pin
const int ledmerah1 = 27;
const int ledhijau2 = 24;
const int ledmerah2 = 26;
const int ledhijau3 = 29;
const int ledmerah3 = 31;
const int ldrPin1 = A0;
const int ldrPin2 = A1;
const int ldrPin3 = A2;
const int buzzerPin1 = A3;
const int buzzerPin2 = A4;
const int buzzerPin3 = A5;
const int pirPin1 = 7;
const int pirPin2 = 8;
const int pirPin3 = 9;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
// Define task handles
TaskHandle_t task1Handle;
TaskHandle_t task2Handle;
TaskHandle_t task3Handle;
// Define task functions
void task1(void* pvParameters) {
int ldr;
int pir;
String sistem;
int alert;
while(1){
// Code to read LDR sensor for slot 1
ldr = analogRead(ldrPin1);
// Serial.print("1 :");
// Serial.println(ldr);
vTaskDelay(5000 / portTICK_PERIOD_MS);
if(ldr <= 600){
sistem = "OFF";
}else{
sistem = "ON ";
pir = digitalRead(pirPin1);
Serial.print("1 :");
Serial.println(pir);
vTaskDelay(500 / portTICK_PERIOD_MS);
if(pir == 1){
alert = 1;
tone(buzzerPin1, 500); // Send 0.5KHz sound signal...
vTaskDelay(5000 / portTICK_PERIOD_MS); // ...for 5 sec
noTone(buzzerPin1); // Stop sound...
}else{
alert = 0;
}
}
}
// Code to read PIR sensor for slot 1
// Code to activate buzzer/LED for slot 1
}
void task2(void* pvParameters) {
int ldr;
int pir;
String sistem;
int alert;
while(1){
// Code to read LDR sensor for slot 1
ldr = analogRead(ldrPin2);
// Serial.print("2 :");
// Serial.println(ldr);
vTaskDelay(5000 / portTICK_PERIOD_MS);
if(ldr <= 600){
sistem = "OFF";
}else{
sistem = "ON ";
pir = digitalRead(pirPin2);
Serial.print("2 :");
Serial.println(pir);
vTaskDelay(500 / portTICK_PERIOD_MS);
if(pir == 1){
alert = 1;
tone(buzzerPin2, 400); // Send 0.4KHz sound signal...
vTaskDelay(5000 / portTICK_PERIOD_MS); // ...for 5 sec
noTone(buzzerPin2); // Stop sound...
}else{
alert = 0;
}
}
}
// Code to read PIR sensor for slot 2
// Code to activate buzzer/LED for slot 2
}
void task3(void* pvParameters) {
int ldr;
int pir;
String sistem;
int alert;
while(1){
// Code to read LDR sensor for slot 1
ldr = analogRead(ldrPin3);
// Serial.print("1 :");
// Serial.println(ldr);
vTaskDelay(5000 / portTICK_PERIOD_MS);
if(ldr <= 600){
sistem = "OFF";
}else{
sistem = "ON ";
pir = digitalRead(pirPin3);
Serial.print("3 :");
Serial.println(pir);
vTaskDelay(500 / portTICK_PERIOD_MS);
if(pir == 1){
alert = 1;
tone(buzzerPin3, 300); // Send 0.3KHz sound signal...
vTaskDelay(5000 / portTICK_PERIOD_MS); // ...for 5 sec
noTone(buzzerPin3); // Stop sound...
}else{
alert = 0;
}
}
}
// Code to read PIR sensor for slot 3
// Code to activate buzzer/LED for slot 3
}
void setup() {
Serial.begin(9600);
// Initialize hardware and RTOS
// Create task 1
xTaskCreate(task1, "Task 1", configMINIMAL_STACK_SIZE, NULL, 1, &task1Handle);
// Create task 2
xTaskCreate(task2, "Task 2", configMINIMAL_STACK_SIZE, NULL, 1, &task2Handle);
// Create task 3
xTaskCreate(task3, "Task 3", configMINIMAL_STACK_SIZE, NULL, 1, &task3Handle);
// Start the RTOS scheduler
vTaskStartScheduler();
}
void loop() {
// Empty loop as all tasks are handled by the RTOS scheduler
}