#include <Wire.h>
#include <Servo.h>
#include <virtuabotixRTC.h>
#define SLAVE_ADDR 8
const int BTN1 = 9;
const int BTN2 = 10;
const int BTN3 = 11;
const int BTN4 = 13;
const int BTN_AWAY = 3;
const int LED_RED = 4;
const int LED_GREEN = 5;
const int SERVO_PIN = 6;
const int BUZZER = 7;
const int MIC_PIN = A0;
const int RTC_RST = 2;
const int RTC_CLK = 8;
const int RTC_DAT = 12;
Servo doorServo;
virtuabotixRTC myRTC(RTC_CLK, RTC_DAT, RTC_RST);
const int secretCode[4] = {1, 2, 3, 4};
int enteredStep = 0;
bool systemUnlocked = false;
bool awayMode = false;
unsigned long lastPressTime = 0;
const unsigned long debounceDelay = 250;
unsigned long lastSyncTime = 0;
const unsigned long syncInterval = 500;
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(BTN1, INPUT_PULLUP);
pinMode(BTN2, INPUT_PULLUP);
pinMode(BTN3, INPUT_PULLUP);
pinMode(BTN4, INPUT_PULLUP);
pinMode(BTN_AWAY, INPUT_PULLUP);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(BUZZER, OUTPUT);
doorServo.attach(SERVO_PIN);
doorServo.write(0);
// myRTC.setDS1302Time(0, 30, 14, 5, 30, 10, 2026);
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
Serial.println(F("Zhuye kulyptaldy. PIN-kodty retimen teriniz."));
}
void loop() {
if (!systemUnlocked) {
checkPinEntry();
} else {
handleAwayMode();
if (millis() - lastSyncTime > syncInterval) {
lastSyncTime = millis();
sendStatusToSlave();
readSlaveSensors();
}
}
delay(30);
}
void checkPinEntry() {
int pressed = 0;
if (digitalRead(BTN1) == LOW) pressed = 1;
else if (digitalRead(BTN2) == LOW) pressed = 2;
else if (digitalRead(BTN3) == LOW) pressed = 3;
else if (digitalRead(BTN4) == LOW) pressed = 4;
if (pressed != 0 && millis() - lastPressTime > debounceDelay) {
lastPressTime = millis();
if (pressed == secretCode[enteredStep]) {
enteredStep++;
tone(BUZZER, 1500, 100);
if (enteredStep == 4) {
unlockSystem();
}
} else {
enteredStep = 0;
tone(BUZZER, 300, 400);
Serial.println(F("Kate PIN! Kayta teriniz."));
}
}
}
void unlockSystem() {
systemUnlocked = true;
enteredStep = 0;
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
doorServo.write(90);
tone(BUZZER, 2000, 300);
Serial.println(F("PIN durys! Zhuye belsendirildi."));
}
void handleAwayMode() {
if (digitalRead(BTN_AWAY) == LOW && millis() - lastPressTime > debounceDelay) {
lastPressTime = millis();
awayMode = !awayMode;
digitalWrite(LED_GREEN, awayMode ? LOW : HIGH);
tone(BUZZER, awayMode ? 1800 : 1200, 150);
Serial.println(awayMode ? F("Away KOSYLDY") : F("Away OSHIRILDI"));
}
if (awayMode) {
int soundLevel = analogRead(MIC_PIN);
if (soundLevel > 600) {
tone(BUZZER, 2500, 1000);
Serial.println(F("DABYL! Away kezinde dybys anyktaldy!"));
}
}
}
void sendStatusToSlave() {
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(systemUnlocked ? 1 : 0);
Wire.endTransmission();
}
void readSlaveSensors() {
Wire.requestFrom(SLAVE_ADDR, 2);
if (Wire.available() == 2) {
int temp = Wire.read();
int hum = Wire.read();
Serial.print(F("Slave -> Temp: "));
Serial.print(temp);
Serial.print(F(" C, Hum: "));
Serial.print(hum);
Serial.println(F(" %"));
}
}