#include <LiquidCrystal.h>
#include <IRremote.h>
#include <Servo.h>
#include <LedControl.h>
#include <Keypad.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] = {22, 24, 26, 28};
byte colPins[COLS] = {30, 32, 34, 36};
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';
char receivedChar;
int newChar = 0;
const int motionPin = 7;
const int ledPin = 6;
const int buzzPin = 8;
const int IRPin = 9;
IRrecv receiver(IRPin);
void setup() {
pinMode(buzzPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(motionPin, INPUT);
Serial.begin(9600);
Serial.println("Starting Application");
receiver.begin(IRPin);
lcd.begin(16,2);
myservo.attach(10);
myservo.write(0);
lc.shutdown(0,false);
lc.setIntensity(0,0);
lc.clearDisplay(0);
setLed();
}
void loop() {
float lux = getLux();
int motion = checkMotion();
updateLcd(lux, motion);
setLed(lux);
if(receiver.decode()){
unsigned long receivedNumber = receiver.decodedIRData.decodedRawData;
receivedChar = mapNumberToCharacter(receivedNumber);
Serial.println(receivedChar);
newChar = 1;
receiver.resume();
}
else{
receivedChar = customKeypad.getKey();
if (receivedChar != NO_KEY) {
Serial.println(receivedChar);
newChar = 1;
}
}
if(newChar == 1){
if(timer >= 1000){
timer = 0;
}
if(state == 'c'){
getCode(receivedChar);
}
else if (receivedChar == '='){
lockDoor();
}
newChar = 0;
}
}
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(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
timer = 1000;
}
timer++;
}
else if (timer == 1000){
digitalWrite(ledPin, LOW);
timer++;
}
}
int checkMotion(){
int motion = digitalRead(motionPin);
if (motion == HIGH) {
timer = 0;
}
return motion;
}
void updateLcd(float lux, int motion){
lcd.setCursor(0,1);
if(motion == HIGH){
lcd.print("Motion ");
}
else{
lcd.print("No Motion ");
}
lcd.print(lux);
}
void lockDoor(){
Serial.println("Lock");
state = 'c';
lc.setLed(0, 3, 2, true);
lc.setLed(0, 2, 2, true);
myservo.write(180);
delay(200);
playLockSound();
}
void unlockDoor(){
Serial.println("Unlock");
state = 'o';
lc.setLed(0, 3, 2, false);
lc.setLed(0, 2, 2, false);
myservo.write(0);
delay(200);
playUnlockSound();
}
void playLockSound(){
unsigned long startTime = millis();
while (millis() - startTime < 250) {
digitalWrite(buzzPin, HIGH);
delayMicroseconds(4577);
digitalWrite(buzzPin, LOW);
delayMicroseconds(4577);
}
}
void playUnlockSound(){
unsigned long startTime = millis();
while (millis() - startTime < 250) {
digitalWrite(buzzPin, HIGH);
delayMicroseconds(2272);
digitalWrite(buzzPin, LOW);
delayMicroseconds(2273);
}
}
void playKeySound(){
unsigned long startTime = millis();
while (millis() - startTime < 70) {
digitalWrite(buzzPin, HIGH);
delayMicroseconds(3477);
digitalWrite(buzzPin, LOW);
delayMicroseconds(3477);
}
}
char mapNumberToCharacter(unsigned long number) {
switch (number) {
case 2540240640:
return '0';
case 3476094720:
return '1';
case 3877175040:
return '2';
case 2239430400:
return '3';
case 4010868480:
return '4';
case 3342401280:
return '5';
case 2774204160:
return '6';
case 3175284480:
return '7';
case 3041591040:
return '8';
case 2907897600:
return '9';
case 1336999680:
return '*';
case 1470693120:
return '=';
default:
return 'X'; // Placeholder character for other cases
}
}
void getCode(char key){
lcd.setCursor(0,0);
lcd.print('*');
if(index == 0){
lcd.setCursor(0,0);
lcd.print(" ");
}
if(key == '*'){
playKeySound();
lcd.setCursor(0,0);
lcd.print(" ");
enteredCode[0] = '\0';
index = 0;
}
else if(index < 4 && isDigit(key)){
playKeySound();
enteredCode[index++] = key;
lcd.setCursor(index - 1, 0);
lcd.print('*');
}
else if (index == 4 && key == '='){
lcd.setCursor(0,0);
lcd.print(" ");
if(strcmp(enteredCode, "1234") == 0){
enteredCode[0] = '\0';
index = 0;
unlockDoor();
}
else{
enteredCode[0] = '\0';
index = 0;
delay(200);
playLockSound();
}
}
}
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);
}