#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
#include <LedControl.h>
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','=','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(A6, A5, A4, A3, A2, A1);
Servo myservo;
LedControl lc = LedControl(11,13,12,1);
int timer = 1000;
char enteredCode[5] = {'\0'};
int index = 0;
char state = 'o';
void setup() {
pinMode(24, OUTPUT);
pinMode(22, INPUT);
Serial.begin(9600);
lcd.begin(16,2);
lcd.print("Unlocked ");
myservo.attach(10);
myservo.write(0);
lc.shutdown(0,false);
lc.setIntensity(0,0);
lc.clearDisplay(0);
setLed();
}
void loop() {
float lux = getLux();
updateLcd(lux);
checkMotion();
setLed(lux);
char key = customKeypad.getKey();
if (key != NO_KEY) {
if(timer >= 1000){
timer = 0;
}
if(state == 'c'){
getCode(key);
}
else if (key == 'A'){
lcd.setCursor(0,0);
lcd.print("Locked ");
state = 'c';
tone(26, 110, 100);
lc.setLed(0, 3, 2, true);
lc.setLed(0, 2, 2, true);
myservo.write(0);
delay(200);
}
}
}
float getLux(){
int analogValue = analogRead(A7);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(50 * 1e3 * pow(10, 0.7) / resistance, (1 / 0.7));
return lux;
}
void setLed(float lux){
if(timer < 1000){
if(lux < 51){
digitalWrite(24, HIGH);
}
timer++;
}
else if (timer == 1000){
digitalWrite(24, LOW);
timer++;
}
}
void checkMotion(){
int motion = digitalRead(22);
if (motion == HIGH) {
timer = 0;
}
}
void updateLcd(float lux){
lcd.setCursor(0,1);
lcd.print("lux: ");
lcd.print(lux);
}
void getCode(char key){
lcd.setCursor(0,0);
lcd.print('*');
if(index == 0){
lcd.setCursor(0,0);
lcd.print(" ");
}
if(key == '*'){
tone(26, 110, 100);
lcd.setCursor(0,0);
lcd.print(" ");
enteredCode[0] = '\0';
index = 0;
}
else if(index < 4 && isDigit(key)){
tone(26, 110, 100);
enteredCode[index++] = key;
lcd.setCursor(index - 1, 0);
lcd.print('*');
}
else if (index == 4 && key == '='){
if(strcmp(enteredCode, "1234") == 0){
lcd.setCursor(0,0);
lcd.print("Unlocked ");
enteredCode[0] = '\0';
index = 0;
state = 'o';
tone(26, 294, 250);
myservo.write(180);
lc.setLed(0, 3, 2, false);
lc.setLed(0, 2, 2, false);
delay(200);
}
else{
lcd.setCursor(0,0);
lcd.print("Incorrect Code");
enteredCode[0] = '\0';
index = 0;
tone(26, 58, 250);
}
}
}
void setLed(){
lc.setLed(0, 7, 1, true);
lc.setLed(0, 7, 2, true);
lc.setLed(0, 7, 3, true);
lc.setLed(0, 7, 4, true);
lc.setLed(0, 7, 5, true);
lc.setLed(0, 7, 6, true);
lc.setLed(0, 6, 1, true);
lc.setLed(0, 6, 2, true);
lc.setLed(0, 6, 3, true);
lc.setLed(0, 6, 4, true);
lc.setLed(0, 6, 5, true);
lc.setLed(0, 6, 6, true);
lc.setLed(0, 5, 1, true);
lc.setLed(0, 5, 2, true);
lc.setLed(0, 5, 3, true);
lc.setLed(0, 5, 4, true);
lc.setLed(0, 5, 5, true);
lc.setLed(0, 5, 6, true);
lc.setLed(0, 4, 1, true);
lc.setLed(0, 4, 2, true);
lc.setLed(0, 4, 3, true);
lc.setLed(0, 4, 4, true);
lc.setLed(0, 4, 5, true);
lc.setLed(0, 4, 6, true);
lc.setLed(0, 3, 5, true);
lc.setLed(0, 2, 5, true);
lc.setLed(0, 1, 2, true);
lc.setLed(0, 1, 5, true);
lc.setLed(0, 0, 3, true);
lc.setLed(0, 0, 4, true);
}