/*
INPUT
1- KEYPAD
2- variable resistor
3- PIR
4- DHT
5- LDR
Output
1- LED
2- Servo
3- MOTOR
4- FAN
5- LCD
6- BUZZER
7-
*/
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <Servo.h>
String passcode= "1111";
String key_now="";
#define LCD_ROW1_DELAY 2000
#define LCD_ROW2_DEAY 5000
#define light_threshold_high 800
#define light_threshold_low 500
Servo lock_servo;
//KEYPAD SETUP
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//LCDSETUP
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
enum row{
S_temp,S_humidity,S_fan_speed,S_light,S_presense,S_light_status,S_alarm ,S_count
}row1;
//FAN_MOTOR
#define MOTOR_PIN 11
#define POT_PIN A0
#define LOCK_SERVO 10
#define BUZZER 12
#define LED 13
//DHT
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22
#define DHTPIN A2
DHT dht(DHTPIN, DHTTYPE);
#define PIR A3
#define LDR A1
///VARIABLES ---
float temp=0;
float humidity=0;
float light=0;
bool presence=0;
bool lock_open=0;
bool update_row1_now=false;
char key;
int light_PS,PIR_PS,fan_PS,alarm_PS;
#define boot_animation_timeout 2000
unsigned long last_LCD_update =0;
unsigned long LCD_update_row1 =0;
unsigned long LCD_update_row2 =LCD_ROW2_DEAY;
void setup() {
Serial.begin(9600);
lock_servo.attach(LOCK_SERVO);
//LCD
lcd.init();
lcd.backlight();
//OUTPUTS
lcd.setCursor(2,0);
lcd.print("INATIALIZING");
while(millis()-last_LCD_update<boot_animation_timeout)
{
lcd.setCursor(0,1);
lcd.print("_-_-_-_-_-_-_-_-");
delay(100);
lcd.setCursor(0,1);
lcd.print("-_-_-_-_-_-_-_-_");
delay(100);
}
lcd.clear();
pinMode(MOTOR_PIN,OUTPUT);
pinMode(LOCK_SERVO,OUTPUT);
pinMode(BUZZER,OUTPUT);
pinMode(LED,OUTPUT);
//INPUTS
pinMode(POT_PIN,INPUT);
pinMode(PIR,INPUT);
pinMode(LDR,INPUT);
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(MOTOR_PIN,map(analogRead(POT_PIN),0,1024,0,255));
read_sensors();
read_keypad();
control_lock();
control_light();
check_PS();
update_lcd();
}
void read_sensors()
{
presence=digitalRead(PIR);
light=analogRead(LDR);
temp=dht.readTemperature();
humidity=dht.readHumidity();
}
void read_keypad()
{
key = keypad.getKey();
if (key == '#') {
if (key_now== passcode && !lock_open){
lock_open=1;
key_now="";
lcd.setCursor(0, 1);
lcd.print(" ACCESS GRANTED ");
LCD_update_row2=millis();
} else if (lock_open)
{
lock_open=0;
lcd.setCursor(0, 1);
lcd.print(" LOCKED ");
LCD_update_row2=millis();
}
else
{
lcd.setCursor(0, 1);
lcd.print(" WRONG KEY ");
LCD_update_row2=millis();
lock_open=0;
key_now="";
}
}
else if (key == '*') {
key_now = "";
lcd.setCursor(0, 1);
lcd.print(" ");
}
else if ( key != NO_KEY && key != '#' && !lock_open && key !='A'&& key !='B'&& key !='C'&& key !='D' ) {
key_now += String(key);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
for(int i=0; i<key_now.length();i++)
{
lcd.print("*");
LCD_update_row2=millis();
}
}
else if (key == 'A')
digitalWrite(BUZZER,!digitalRead(BUZZER));
/* if (key != NO_KEY) {
Serial.println(key);
}
*/
}
void control_lock()
{
if (lock_open)
lock_servo.write(180);
else
lock_servo.write(0);
}
void control_light()
{
if(light>light_threshold_high)
digitalWrite(LED,0);
if(light<light_threshold_low)
digitalWrite(LED,1);
}
void update_lcd()
{
if(millis()-LCD_update_row1>LCD_ROW1_DELAY || update_row1_now)
{
//Serial.println(row1);
if(!update_row1_now)
row1=row1+1;
lcd.setCursor(0, 0);
lcd.print(F(" "));
LCD_update_row1=millis();
}
row1=row1>=S_count?0:row1;
//S_temp,S_humidity,S_fan_speed,S_light,S_presense,S_light_status,S_alarm
lcd.setCursor(0, 0);
switch(row1)
{
case S_temp:
lcd.print("TEMPERATURE:");
lcd.print(temp,1);
break;
case S_humidity:
lcd.print("HUMIDITY:");
lcd.print(humidity,0);
lcd.print("%");
break;
case S_fan_speed:
lcd.print("FAN:");
lcd.print(map(analogRead(POT_PIN),0,1024,0,100));
break;
case S_light:
lcd.print("LIGHT %:");
lcd.print(map(analogRead(LDR),0,1024,0,100));
break;
case S_presense:
if(digitalRead(PIR))
{
lcd.print("MOVEMET DETECTED");
break;
}
row1=row1+1;
case S_light_status:
if(digitalRead(LED))
lcd.print("LIGHTS ON");
else
lcd.print("LIGHTS OFF");
break;
case S_alarm :
if(digitalRead(BUZZER))
lcd.print("ALARM ACTIVE");
else
lcd.print(" ALARM OFF ");
break;
default:
break;
}
if(millis()-LCD_update_row2>LCD_ROW2_DEAY)
{
lcd.setCursor(0, 1);
if(!lock_open)
{
lcd.print(" ENTER PASSCODE ");
}
if(lock_open)
{
lcd.print("PRESS # TO LOCK ");
}
}
}
void check_PS()
{
//S_temp,S_humidity,S_fan_speed,S_light,S_presense,S_light_status,S_alarm
update_row1_now=1;
if(light_PS!=digitalRead(LED))
row1=S_light_status;
else if(PIR_PS!=presence)
row1=S_presense;
else if(fan_PS-5 >analogRead(POT_PIN) ||fan_PS+5 <analogRead(POT_PIN) )
row1=S_fan_speed;
else if(alarm_PS!=digitalRead(BUZZER))
row1=S_alarm ;
else
update_row1_now=0;
light_PS=digitalRead(LED);
PIR_PS=presence;
fan_PS=analogRead(POT_PIN);
alarm_PS=digitalRead(BUZZER);
}