//ENG Wael ALkharraz
#include <Servo.h>
#include <Password.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
Servo myservo;
Password password = Password("1*3*#"); // password
int dlugosc = 5; // length of the password
int x;
int pos = 0;
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);//rs en d4 d5 d6 d7
int ledRed = 11;
int ledGreen = 12;
int buzzer = 13;
int ilosc; // number of clicks
const byte ROWS = 4; // rows //أسطر
const byte COLS = 4; // cols // أعمدة
char keys[ROWS][COLS] =
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2,3,4,5};
byte colPins[COLS] = {6,7,8,9};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
myservo.attach(10);
Serial.begin(9600);
keypad.addEventListener(keypadEvent);
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(ledRed, HIGH);
digitalWrite(ledGreen, LOW);
lcd.begin(16, 2);
lcd.setCursor(1,0);
lcd.print("EGWAELALKHARRAZ");
lcd.setCursor(0,1);
lcd.print(" ENTER PASSWORD");
}
//********************************************************
//********************************************************
void loop()
{
keypad.getKey();
}
//********************************************************
//********************************************************
void keypadEvent(KeypadEvent eKey)
{
switch (keypad.getState())
{
case PRESSED:
int i;
for( i = 1; i <= 3; i++ )
{
tone(13, 1000, 500);
noTone(13);
}
Serial.print("Presse: ");
Serial.println(eKey);
switch (eKey)
{
/*
case '#':
break;
case '*':
break;
*/
default:
ilosc=ilosc+1;
password.append(eKey);
}
//Serial.println(ilosc);
if(ilosc == 1)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(" < PIN >");
lcd.setCursor(0,1);
lcd.print(eKey);
}
if(ilosc == 2)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(" < PIN >");
lcd.setCursor(0,1);
lcd.print("**_");
}
if(ilosc == 3)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(" < PIN >");
lcd.setCursor(0,1);
lcd.print("***_");
}
if(ilosc == 4)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(" < PIN >");
lcd.setCursor(0,1);
lcd.print("****_");
}
if(ilosc == 5)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(" < PIN >");
lcd.setCursor(0,1);
lcd.print("*****_");
}
if(ilosc == 6)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(" < PIN >");
lcd.setCursor(0,1);
lcd.print("******_");
}
if(ilosc == 7)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(" < PIN >");
lcd.setCursor(0,1);
lcd.print("*******_");
}
if(ilosc == 8)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(" < PIN >");
lcd.setCursor(0,1);
lcd.print("********");
}
if(ilosc == dlugosc)
{
delay(250);
checkPassword();
ilosc = 0;
}
}
}
//********************************************************
//********************************************************
void checkPassword()
{
if (password.evaluate())
{
int i;
for( i = 1; i <= 2; i++ )
{
tone(13, 1000, 200);
}
ilosc = 0;
Serial.println("Success");
digitalWrite(ledRed, LOW);
digitalWrite(ledGreen, HIGH);
lcd.clear();
lcd.setCursor(0,1);
lcd.print(" << SUCCESS >>");
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
delay(5000);
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
}
else
{
int i;
for( i = 1; i <= 3; i++ )
{
tone(13, 1000, 200);
delay(200);
noTone(13) ;
delay(200);
}
x=x+1;
ilosc = 0;
password.reset();
Serial.println("Wrong");
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, HIGH);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("code incorrect ");
delay(500);
lcd.clear();
lcd.setCursor(0,1);
lcd.print(" ");
if(x==3){tone(13, 1000, 300);}
delay(500);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("code incorrect ");
if(x==3){tone(13, 1000, 300);}
delay(500);
lcd.clear();
lcd.setCursor(0,1);
lcd.print(" ");
delay(500);
if(x==3){tone(13, 1000, 300);}
lcd.clear();
lcd.setCursor(1,0);
lcd.print("code incorrect ");
delay(1000);
if(x==3){tone(13, 1000, 600);x=0;}
lcd.clear();
lcd.setCursor(2,0);
lcd.print(" ENTRER CODE");
lcd.setCursor(7,1);
lcd.print("SVP");
}
}