Paso 4: código
#include <Keypad.h>#include <LiquidCrystal.h> #include<Wire>int greenLed=22; int redLed=23; int motorPin=24; int piezoPin=25; char* ourCode="1234"; int currentPosition=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); //lcd.init(); displayCodeEntryScreen(); pinMode(piezoPin,OUTPUT); pinMode(motorPin,OUTPUT); pinMode(redLed,OUTPUT); pinMode(greenLed,OUTPUT); digitalWrite(redLed,LOW); digitalWrite(greenLed,LOW); // put your setup code here, to run once:}void loop() { int l; char key=keypad.getKey(); if(int(key)!=0){ Lcd.setCursor(10,0); Lcd.print(" "); Lcd.setCursor(10,0); 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(motorPin,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 unlockDoor() { digitalWrite(greenLed,HIGH); clearScreen(); Lcd.setCursor(0,0); Lcd.print(" ACCESS GRANTED "); Lcd.setCursor(0,1); Lcd.print(" WELCOME!! "); digitalWrite(motorPin,HIGH); delay(5000); digitalWrite(greenLed,LOW); digitalWrite(motorPin,LOW); displayCodeEntryScreen(); } void displayCodeEntryScreen() { clearScreen(); Lcd.setCursor(0,0); Lcd.print("Hallow,welcome"); Lcd.setCursor(0,1); Lcd.print("Enter code"); } void clearScreen() { Lcd.setCursor(0,0); Lcd.print(" "); Lcd.setCursor(0,1); Lcd.print(" "); }
// put your main code here, to run repeatedly: