#include <Keypad.h>; //The keypad and LCD i2c libraries
#include <Wire.h>;
#include "LCD.h";
#include "LiquidCrystal_I2C.h";
#include <Servo.h>;
#include "DHT.h"
#define I2C_ADDR 0x27 //defining the LCD pins
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define LED1 11
#define LED2 12
#define LED3 13
#define KIPAS1 37
#define KIPAS2 39
#define DHTTYPE DHT22
#define DHTPIN1 41
#define DHTPIN2 43
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
#define pirPin 53 // PIR Out pin
int pirStat = 0;
String Str1 = "Sensor A = ";
String Str2 = "Sensor B = ";
String Str3 = "Sensor C = ";
#define servoPin 45
Servo servo1;
int buttonPin = 10;
int light1Pin = A0;
int light2Pin = A1;
int light3Pin = A3;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3 //if you modify your pins you should modify this too
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
//inisialisasi variable
int column = 0;
int currentState = 'MainMenu';
int messageState = 0;
char password[4];
char pass[4];
long timer = 10000;
long previousMillis = 0;
long elapsedTime = 0;
long currentMillis = 0;
int buttonStat = 0;
int gateState;
int prevbuttonState = 0;
int lightThreshold[5];
int tempThreshold[5];
int light1Value = 0;
int light2Value = 0;
int light3Value = 0;
void setup()
{
Serial.begin(9600);
dht1.begin();
dht2.begin();
Serial.println("A & B : Pilih Menu, C : OK");
Serial.println("Sensor akan dijalankan dalam 10 detik jika keypad idle");
lcd.begin (20,4);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
pinMode(buttonPin, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(KIPAS1, OUTPUT);
pinMode(KIPAS2, OUTPUT);
pinMode(pirPin, INPUT);
servo1.attach(servoPin);
delay(2000);
}
void loop()
{
switch(currentState) {
case 'MainMenu': {
char keypressed = myKeypad.getKey();
currentMillis = millis();
lcd.setCursor(0,0);
lcd.print("Select Menu");
if (currentMillis - previousMillis > timer) {
lcd.clear();
currentState = 'ReadLightSensor';
break;
}
if (messageState == 0) {
lcd.setCursor(0,1);
lcd.print("Adjust Password ");
}
if (messageState == 1) {;
lcd.setCursor(0,1);
lcd.print("Enter Password ");
}
if (messageState == 2) {;
lcd.setCursor(0,1);
lcd.print("Set Tempt Threshold ");
}
if (messageState == 3) {
lcd.setCursor(0,1);
lcd.print("Set Light Threshold ");
}
if (keypressed == 'A')
{
messageState--;
timer = timer + 2000;
if (messageState < 0) messageState = 3;
}
if (keypressed == 'B')
{
messageState++;
timer = timer + 2000;
if (messageState > 3) messageState = 0;
}
if (keypressed == 'C')
{
timer = timer + 2000;
if (messageState == 0) {
lcd.clear();
currentState = 'AdjustPassword';
break;
}
if (messageState == 1) {
lcd.clear();
currentState = 'EnterPasswd';
break;
}
if (messageState == 2) {
lcd.clear();
currentState = 'SetTemp';
break;
}
if (messageState == 3) {
lcd.clear();
currentState = 'SetLight';
break;
}
}
break;
}
case 'AdjustPassword': {
char key = myKeypad.getKey();
lcd.setCursor(0,0);
lcd.print("Set Your Password ");
if (key)
{
lcd.setCursor(column,1);
lcd.print(key);
password[column] = key;
//Serial.println(keypressed); //Uncomment this to show it on the serial monitor too
column++;
if (column > 3) {
column = 0;
lcd.setCursor(0,1);
lcd.print("Password Set !");
Serial.println("Password berhasil diatur");
delay(1000);
lcd.clear();
currentState = 'MainMenu';
}
}
break;
}
case 'SetLight': {
char key2 = myKeypad.getKey();
lcd.setCursor(0,0);
lcd.print("Set Light Threshold ");
if (key2)
{
lcd.setCursor(column,1);
lcd.print(key2);
lightThreshold[column] = key2;
//Serial.println(keypressed); //Uncomment this to show it on the serial monitor too
column++;
if (key2 == 'C') {
column = 0;
lcd.setCursor(0,1);
lcd.print("Threshold Set !");
Serial.println("Threshold cahaya berhasil diatur");
delay(1000);
lcd.clear();
currentState = 'MainMenu';
}
}
break;
}
case 'SetTemp': {
char key3 = myKeypad.getKey();
lcd.setCursor(0,0);
lcd.print("Set Tempt Threshold ");
if (key3)
{
lcd.setCursor(column,1);
lcd.print(key3);
tempThreshold[column] = key3;
//Serial.println(keypressed); //Uncomment this to show it on the serial monitor too
column++;
if (key3 == 'C') {
column = 0;
lcd.setCursor(0,1);
lcd.print("Threshold Set !");
Serial.println("Threshold suhu berhasil diatur");
delay(1000);
lcd.clear();
currentState = 'MainMenu';
}
}
break;
}
case 'EnterPasswd': {
char pw = myKeypad.getKey();
lcd.setCursor(0,0);
lcd.print("Enter Password : ");
if (pw)
{
lcd.setCursor(column,1);
lcd.print(pw);
pass[column] = pw;
//Serial.println(keypressed); //Uncomment this to show it on the serial monitor too
column++;
if (column > 3) {
if (array_cmp(pass, password, 4, 4) == true){
lcd.setCursor(0,1);
lcd.print("Password Correct !");
Serial.println("Password benar, gate dibuka.");
delay(1000);
column = 0;
lcd.clear();
currentState = 'OpenGate';
}else{
lcd.setCursor(0,1);
lcd.print("Password Incorrect !");
Serial.println("Password salah !");
delay(1000);
column = 0;
lcd.clear();
currentState = 'MainMenu';
}
}
}
break;
}
case 'Loop': {
currentState = 'EnterPasswd';
break;
}
case 'OpenGate': {
lcd.setCursor(0,0);
lcd.print("Opening Gate ");
servo1.write(0);
buttonStat = digitalRead(buttonPin);
if (buttonStat != prevbuttonState) {
if (buttonStat==HIGH) {
lcd.setCursor(0,1);
lcd.print("Silahkan Lewat");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Closing Gate ");
servo1.write(90);
Serial.println("Gerbang ditutup");
lcd.setCursor(0,1);
lcd.print(" ");
delay(2000);
lcd.clear();
currentState = 'ReadLightSensor';
}
}
break;
}
case 'ReadLightSensor': {
lcd.setCursor(0,0);
lcd.print("Reading Light Sensor");
light1Value = analogRead(light1Pin);
light2Value = analogRead(light2Pin);
light3Value = analogRead(light3Pin);
String StrCombined1 = Str1 + light1Value ;
String StrCombined2 = Str2 + light2Value ;
String StrCombined3 = Str3 + light3Value ;
lcd.setCursor(0,1);
lcd.print(StrCombined1);
lcd.setCursor(0,2);
lcd.print(StrCombined2);
lcd.setCursor(0,3);
lcd.print(StrCombined3);
delay(4000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Checking Threshold");
if (light1Value >= lightThreshold) {
lcd.setCursor(0,1);
lcd.print("A > Threshold = OFF");
digitalWrite(LED1, LOW);
Serial.println("LED A Mati");
}
if (light1Value < lightThreshold) {
lcd.setCursor(0,1);
lcd.print("A < Threshold = ON");
digitalWrite(LED1, HIGH);
Serial.println("LED A Menyala");
}
if (light2Value >= lightThreshold) {
lcd.setCursor(0,2);
lcd.print("B > Threshold = OFF");
digitalWrite(LED2, LOW);
Serial.println("LED B Mati");
}
if (light2Value < lightThreshold) {
lcd.setCursor(0,2);
lcd.print("B < Threshold = ON");
digitalWrite(LED2, HIGH);
Serial.println("LED B Menyala");
}
if (light3Value >= lightThreshold) {
lcd.setCursor(0,3);
lcd.print("C > Threshold = OFF");
digitalWrite(LED3, LOW);
Serial.println("LED C Mati");
}
if (light3Value < lightThreshold) {
lcd.setCursor(0,3);
lcd.print("C < Threshold = ON");
digitalWrite(LED3, HIGH);
Serial.println("LED C Menyala");
}
delay(4000);
pirStat = digitalRead(pirPin);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Checking PIR Sensor");
if(pirStat == HIGH) {
lcd.setCursor(0,2);
lcd.print("Movement Detected !");
lcd.setCursor(0,3);
lcd.print("LED A&B ON");
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
Serial.println("Ada orang, LED A&B dinyalakan");
}
if(pirStat == LOW) {
lcd.setCursor(0,2);
lcd.print("No Movement !");
lcd.setCursor(0,3);
lcd.print("LED A&B OFF");
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
Serial.println("Tidak ada orang, LED dimatikan");
}
delay(4000);
lcd.clear();
currentState = 'TempSensor1';
break;
}
case 'ReadTempSensor1': {
lcd.setCursor(0,0);
lcd.print("Reading Tempt Sensor");
int temp1,temp2;
temp1= dht1.readTemperature();
temp2= dht2.readTemperature();
String Degrees = " *C";
String stringS1 = "Suhu A : ";
String stringS2 = "Suhu B : ";
String stringCmb1 = Str1 + temp1 + Degrees;
String stringCmb2 = Str2 + temp2 + Degrees;
String Nyalakan = ", kipas dinyalakan";
String Matikan = ", kipas dimatikan";
String stringCmb3 = stringS1 + temp1 + Degrees + Nyalakan;
String stringCmb4 = stringS2 + temp2 + Degrees + Nyalakan;
String stringCmb5 = stringS1 + temp1 + Degrees + Matikan;
String stringCmb6 = stringS2 + temp2 + Degrees + Matikan;
lcd.setCursor(0,1);
lcd.print(stringCmb1);
lcd.setCursor(0,2);
lcd.print(stringCmb2);
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Checking Threshold");
if(temp1 >= tempThreshold) {
digitalWrite(KIPAS1, HIGH);
lcd.setCursor(0,1);
lcd.print("A > Thrs = KIPAS ON");
Serial.println(stringCmb3);
}
if(temp1 < tempThreshold) {
digitalWrite(KIPAS1, LOW);
lcd.setCursor(0,1);
lcd.print("A < Thrs = KIPAS OFF");
Serial.println(stringCmb5);
}
if(temp2 >= tempThreshold) {
digitalWrite(KIPAS2, HIGH);
lcd.setCursor(0,2);
lcd.print("B > Thres = KIPAS ON");
Serial.println(stringCmb4);
}
if(temp2 < tempThreshold) {
digitalWrite(KIPAS1, HIGH);
lcd.setCursor(0,2);
lcd.print("B < Thrs = KIPAS OFF");
Serial.println(stringCmb6);
}
delay(4000);
elapsedTime = currentMillis;
previousMillis = currentMillis;
timer = timer + elapsedTime;
lcd.clear();
currentState = 'MainMenu';
break;
}
}
}
boolean array_cmp(char *a, char *b, int len_a, int len_b) {
int n;
if (len_a != len_b) return false;
for (n=0;n<len_a;n++) if (a[n]!=b[n]) return false;
return true;
}