#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//keypad
Servo myservo;
int pos = 0;
bool delayLCD = false;
/* Display */
LiquidCrystal_I2C lcd(0x27, 20, 4);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
uint64_t value = 0;
//keypad
#define LDR_PIN 13
#define ECHO_PIN 7
#define TRIG_PIN 8
const float BETA = 3950;
const float GAMMA = 0.7;
const float RL10 = 50;
const int tankrelayPin = 13;
const int PIR1relayPin = 12;
const int PIR2relayPin = 11;
const int waterrelayPin = 10;
const int fanrelayPin = 9;
float temcelsius = 0.00;
int pirState1 = LOW; // we start, assuming no motion detected
int PIR1 = 0;
int pirState2 = LOW; // we start, assuming no motion detected
int PIR2 = 0;
bool isLock = false;
bool firsttime = true;
void setup() {
Serial.begin(115200);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.clear();
lcd.cursor();
lcd.setCursor(1, 0);
myservo.attach(6);
pos = 180;
myservo.write(pos);
lcd.setCursor(3, 0);
lcd.print("Set Password:");
pinMode(LDR_PIN, INPUT);
pinMode(tankrelayPin, OUTPUT);
pinMode(PIR1relayPin, OUTPUT);
pinMode(PIR2relayPin, OUTPUT);
pinMode(waterrelayPin, OUTPUT);
pinMode(fanrelayPin, OUTPUT);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
pinMode(LED_BUILTIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
//keypad
String result = "";
String pw = "";
String door = "";
String doorCount = "";
String err = "";
String errcount = "";
void showLCD() {
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0, 2);
lcd.print("Celsius:");
lcd.print(getCelsius());
lcd.setCursor(0, 3);
lcd.print("Lux:");
lcd.print(Lux());
lcd.setCursor(0, 1);
lcd.print(doorCount);
}
void processInput(char key) {
if (result.length() < 4) {
result += key;
lcd.clear();
lcd.print("Password: " + result + "");
pw = "O";
firsttime = true;
}
if (result.length() == 4 && pw == "O") {
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("| " + result + " |");
lcd.setCursor(1, 1);
lcd.print("Is Your Password");
delay(1000);
pw = "X";
isLock = true;
firsttime = false;
err = "O";
lcd.clear();
}
if (err == "O" && errcount.length() < 2) {
lcd.clear();
errcount += key;
lcd.print("Enter Password");
lcd.setCursor(0, 2);
lcd.print("Celsius:");
lcd.print(getCelsius());
lcd.setCursor(0, 3);
lcd.print("Lux:");
lcd.print(Lux());
lcd.setCursor(0, 1);
}
if (errcount.length() == 2 && doorCount.length() < 4) {
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0, 2);
lcd.print("Celsius:");
lcd.print(getCelsius());
lcd.setCursor(0, 3);
lcd.print("Lux:");
lcd.print(Lux());
lcd.setCursor(0, 1);
doorCount += key;
lcd.print(doorCount);
}
if (key == '#' && isLock == false && firsttime != true) {
lcd.clear();
lcd.print("lock");
pos = 180;
myservo.write(pos);
isLock = true;
delay(1000);
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0, 2);
lcd.print("Celsius:");
lcd.print(getCelsius());
lcd.setCursor(0, 1);
doorCount = "";
}
if (key == 'A' && isLock == false && firsttime != true) {
lcd.clear();
result = "";
pw = "";
door = "";
doorCount = "";
err = "";
errcount = "";
pos = 180;
myservo.write(pos);
lcd.print("Set Password");
}
if (doorCount.length() == 4 && doorCount == result) {
lcd.clear();
lcd.print("Door Unlock ");
delay(1000);
lcd.clear();
pos = 0;
myservo.write(pos);
lcd.print("# = Lock ");
lcd.setCursor(0, 1);
lcd.print("A = New Code ");
isLock = false;
}
if (doorCount.length() == 4 && doorCount != result) {
lcd.clear();
lcd.print("Password Denied");
delay(1000);
lcd.clear();
doorCount = "";
lcd.print("Enter Password");
lcd.setCursor(0, 1);
}
// return result;
}
//keypad
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration ;
}
void Distance() {
float distance = 0.0344 / 2 * readDistanceCM();
// Serial.print("Measured distance: ");
// Serial.println(distance);
if (distance <= 30) {
digitalWrite( tankrelayPin, HIGH);
}
if (distance >= 100) {
digitalWrite( tankrelayPin, LOW);
}
}
float getCelsius() {
int analogValue = analogRead(A10);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
return celsius;
}
void Celsius() {
int analogValue = analogRead(A10);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// Serial.print("Temperature: ");
// Serial.print(celsius);
// Serial.println(" ℃");
if (celsius >= 30) {
digitalWrite( fanrelayPin, HIGH);
} else {
digitalWrite( fanrelayPin, LOW);
}
temcelsius = celsius;
}
void Time() {
DateTime now = rtc.now();
// Serial.print("Current time: ");
// Serial.print(now.year(), DEC);
// Serial.print('/');
// Serial.print(now.month(), DEC);
// Serial.print('/');
// Serial.print(now.day(), DEC);
// Serial.print(" (");
// Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
// Serial.print(") ");
// Serial.print(now.hour(), DEC);
// Serial.print(':');
// Serial.print(now.minute(), DEC);
// Serial.print(':');
// Serial.print(now.second(), DEC);
// Serial.println();
if (now.second() == 10) {
digitalWrite( waterrelayPin, HIGH);
}
if (now.second() == 20) {
digitalWrite( waterrelayPin, LOW);
}
}
int getTime() {
DateTime now = rtc.now();
return now.second();
}
float Lux() {
int analogValueLux = analogRead(A15);
float voltage = analogValueLux / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
if (lux > 100) {
// Serial.print("Light! ");
// Serial.print(lux);
} else {
// Serial.print("Dark! ");
// Serial.print(lux);
}
return lux;
}
void PIR_1() {
// Serial.println();
PIR1 = digitalRead(22); // read input value
if (PIR1 == HIGH) { // check if the input is HIGH
if (pirState1 == LOW) {
// we have just turned on
// Serial.println("Motion1 detected!");
digitalWrite( PIR1relayPin, HIGH);
// We only want to print on the output change, not state
pirState1 = HIGH;
}
} else {
if (pirState1 == HIGH) {
// we have just turned of
// Serial.println("Motion1 ended!");
digitalWrite( PIR1relayPin, LOW);
// We only want to print on the output change, not state
pirState1 = LOW;
}
}
}
void PIR_2() {
// Serial.println();
PIR2 = digitalRead(24); // read input value
if (PIR2 == HIGH) { // check if the input is HIGH
if (pirState2 == LOW) {
// we have just turned on
// Serial.println("Motion2 detected!");
digitalWrite( PIR2relayPin, HIGH);
// We only want to print on the output change, not state
pirState2 = HIGH;
}
} else {
if (pirState2 == HIGH) {
// we have just turned of
// Serial.println("Motion2 ended!");
digitalWrite(PIR2relayPin, LOW);
// We only want to print on the output change, not state
pirState2 = LOW;
}
}
}
void loop() {
float currentcelsius = getCelsius();
if (getTime() % 2 != 0) {
delayLCD = false;
}
if (firsttime == false && isLock == true && getTime() % 2 == 0 && delayLCD == false) {
delayLCD = true;
showLCD();
}
//keypad
char key = keypad.getKey();
if (key) {
processInput(key);
}
//keypad
//วัดระยะ
Distance();
//วัดอุณหภูมิ
Celsius();
//เวลา
Time();
//ค่าแสง
if (Lux() < 100) {
//PIR1
PIR_1();
//PIR2
PIR_2();
} else {
digitalWrite( PIR1relayPin, LOW);
digitalWrite( PIR2relayPin, LOW);
}
// Serial.println("\n\n");
}