#include <Arduino_FreeRTOS.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// تعریف LCD I2C
LiquidCrystal_I2C lcd(0x27, 20, 4);
// پینهای سنسورهای PIR
const int pir1 = 2;
const int pir2 = 3;
const int pir3 = 4;
// پینهای کلیدهای ریموت
const int remoteActivate = 6;
const int remoteDeactivate = 7;
const int silentMode = 8;
const int stopAlarm = 9;
// پینهای دیگر
const int buzzer = 5;
const int led = 13;
// تعریف وضعیتها
bool alarmEnabled = false;
bool silentModeEnabled = false;
bool alarmActive = false;
// سیستم رمزگذاری
String password = "";
String enteredPassword = "";
bool passwordSet = false;
// تعریف کیپد
const byte ROWS = 4; // تعداد ردیفها
const byte COLS = 3; // تعداد ستونها
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {10, 11, 12, 14}; // پینهای ردیف
byte colPins[COLS] = {15, 16, 17}; // پینهای ستون
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// تعریف تسکها
void TaskCheckPIR(void *pvParameters);
void TaskManageLCD(void *pvParameters);
void TaskKeypadInput(void *pvParameters);
void TaskAlarmControl(void *pvParameters);
void setup() {
// تنظیمات اولیه پینها
pinMode(pir1, INPUT);
pinMode(pir2, INPUT);
pinMode(pir3, INPUT);
pinMode(remoteActivate, INPUT);
pinMode(remoteDeactivate, INPUT);
pinMode(silentMode, INPUT);
pinMode(stopAlarm, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(led, OUTPUT);
// راهاندازی LCD
lcd.begin(4 , 20);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("System Starting...");
delay(2000);
lcd.clear();
// راهاندازی تسکها
xTaskCreate(TaskCheckPIR, "CheckPIR", 128, NULL, 1, NULL);
xTaskCreate(TaskManageLCD, "ManageLCD", 256, NULL, 2, NULL);
xTaskCreate(TaskKeypadInput, "KeypadInput", 256, NULL, 3, NULL);
xTaskCreate(TaskAlarmControl, "AlarmControl", 128, NULL, 4, NULL);
// شروع FreeRTOS Scheduler
vTaskStartScheduler();
}
void loop() {
// این تابع خالی است؛ همه چیز در تسکها اجرا میشود
}
// تسک بررسی سنسورهای PIR
void TaskCheckPIR(void *pvParameters) {
while (1) {
if (alarmEnabled && !silentModeEnabled) {
if (digitalRead(pir1) == HIGH || digitalRead(pir2) == HIGH || digitalRead(pir3) == HIGH) {
alarmActive = true;
digitalWrite(buzzer, HIGH);
}
}
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
// تسک مدیریت LCD
void TaskManageLCD(void *pvParameters) {
while (1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarm: ");
lcd.print(alarmEnabled ? (silentModeEnabled ? "Silent" : "Active") : "Off");
lcd.setCursor(0, 1);
if (alarmActive) {
lcd.print("Motion Detected");
}
lcd.setCursor(0, 2);
- lcd.print("Buzzer: ");
lcd.print(digitalRead(buzzer) ? "On" : "Off");
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
// تسک ورودی کیپد
void TaskKeypadInput(void *pvParameters) {
while (1) {
char key = keypad.getKey();
if (key) {
if (!passwordSet && key == '*') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Password:");
password = "";
while (password.length() < 6) {
key = keypad.getKey();
if (key) {
if (key == '#') break;
password += key;
lcd.setCursor(password.length(), 1);
- lcd.print("*");
}
}
passwordSet = true;
} else if (passwordSet) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
enteredPassword = "";
while (enteredPassword.length() < 6) {
key = keypad.getKey();
if (key) {
if (key == '#') break;
enteredPassword += key;
lcd.setCursor(enteredPassword.length(), 1);
lcd.print("*");
}
}
if (enteredPassword == password) {
alarmEnabled = !alarmEnabled;
}
}
}
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
// تسک کنترل بازر و LED
void TaskAlarmControl(void *pvParameters) {
while (1) {
if (alarmEnabled && alarmActive) {
digitalWrite(led, HIGH);
if (!silentModeEnabled) {
digitalWrite(buzzer, HIGH);
} else {
digitalWrite(buzzer, LOW);
}
} else {
digitalWrite(led, LOW);
digitalWrite(buzzer, LOW);
}
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}