Stap 1: Set-up, Code
#include
#include #include #include #include int greenLed=22; int redLed=23; int servoPin=24;// Servo int piezoPin=25; char* ourCode="1234"; int currentPosition=0; Servo myservo; int pos = 0;
const byte rows=4; const byte cols=3;
char keys[rows][cols]={ {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte rowPins[rows]={13,10,9,8}; byte colPins[cols]={7,6,1}; Keypad keypad=Keypad(makeKeymap(keys),rowPins,colPins,rows,cols); LiquidCrystal Lcd (12,11,5,4,3,2);
void setup() { Lcd.begin(16,2); myservo.attach(24); displayCodeEntryScreen(); pinMode(piezoPin,OUTPUT); pinMode(servoPin,OUTPUT); pinMode(redLed,OUTPUT); pinMode(greenLed,OUTPUT); digitalWrite(redLed,LOW); digitalWrite(greenLed,LOW); }
void loop() { int l; char key=keypad.getKey(); Lcd.setCursor(0,0); Lcd.print(" ENTER PASSWORD "); if(int(key)!=0){ Lcd.setCursor(4,1); for(l=0;l<=currentPosition;++l) {Lcd.print("*");} if(key==ourCode[currentPosition]) { ++ currentPosition; if(currentPosition==4) {unlockDoor(); currentPosition=0; } } else{invalidCode(); currentPosition=0; } } } void invalidCode() { digitalWrite(redLed,HIGH); digitalWrite(servoPin,LOW); digitalWrite(piezoPin,HIGH); clearScreen(); Lcd.setCursor(0,0); Lcd.print("ACCESS DENIED!"); Lcd.setCursor(0,1); Lcd.print("INVALID CODE "); delay(5000); digitalWrite(redLed,LOW); digitalWrite(piezoPin,LOW);
displayCodeEntryScreen(); }
void turnHandle() { 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(15); // waits 15ms for the servo to reach the position } 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(15); // waits 15ms for the servo to reach the position } }
void unlockDoor() { digitalWrite(greenLed,HIGH); clearScreen(); Lcd.setCursor(0,0); Lcd.print(" ACCESS GRANTED "); Lcd.setCursor(0,1); Lcd.print(" WELCOME!! "); digitalWrite(servoPin,HIGH); turnHandle(); delay(500); digitalWrite(greenLed,LOW); digitalWrite(servoPin,LOW); displayCodeEntryScreen(); } void displayCodeEntryScreen() { clearScreen(); Lcd.setCursor(0,0); Lcd.print(" HELLO AND "); Lcd.setCursor(0,1); Lcd.print(" WELCOME! "); delay(3000); clearScreen(); } void clearScreen() { Lcd.setCursor(0,0); Lcd.print(" "); Lcd.setCursor(0,1); Lcd.print(" "); }
VOID unlockDoor() {digitalWrite(greenLed,HIGH); clearScreen(); Lcd.setCursor(0,0); LCD.Print ("toegang toegekend"); Lcd.setCursor(0,1); LCD.Print ("Welkom!"); digitalWrite(servoPin,HIGH); turnHandle(); delay(500); digitalWrite(greenLed,LOW); digitalWrite(servoPin,LOW);