Paso 3: El código
Tengo este código de otra persona. No recuerda su nombre o donde lo conseguí así que créditos al chico anónimo. Pese pongo algunos cambios importantes en el código por lo que acababa de usar la base del código de este tipo.
Antes de copiar el código y empezar a subir implementar la biblioteca correcta en la carpeta de la biblioteca en el mapa de arduino. Usted puede encontrar esto como siguiendo: Windows C:--> archivos de programa (x86)--> Arduino.
De todos modos, aquí está el código (pero mejor descarga el archivo):
#include #include #include #include Servo myservo;//constants for LEDs int greenLED = 22; int redLED = 23;LiquidCrystal lcd(13, 12, 11, 10, 9, 8, 7); Password password = Password( "4155" ); int currentPosition = 0; const byte ROWS = 4; // Four rows const byte COLS = 4; // Four columns // Define the Keymap char keys[ROWS][COLS] = { { '1','2','3','A', } , { '4','5','6','B', } , { '7','8','9','C', } , { '*','0','#','D', } }; // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins. byte rowPins[ROWS] = { 47, 46, 45, 44}; //connect to the row pinouts of the keypad byte colPins[COLS] = { 51, 50, 49, 48}; //connect to the column pinouts of the keypad// Create the Keypad Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );void setup(){ lcd.begin(16, 2); Serial.begin(9600); keypad.setDebounceTime(50); displayCodeEntryScreen(); keypad.addEventListener(keypadEvent); //add an event listener for this keypad myservo.attach(2); myservo.write(180); //setup and turn off both LEDs pinMode(redLED, OUTPUT); pinMode(greenLED, OUTPUT); digitalWrite(redLED, LOW); digitalWrite(greenLED, LOW); }void displayCodeEntryScreen() { password.reset(); lcd.clear(); lcd.setCursor(2, 0); lcd.print("Enter Code:"); lcd.setCursor(0,1); keypad.addEventListener(keypadEvent); //add an event listener for this keypad //setup and turn off both LEDs pinMode(redLED, OUTPUT); pinMode(greenLED, OUTPUT); digitalWrite(redLED, LOW); digitalWrite(greenLED, LOW); }void loop(){ keypad.getKey(); }//take care of some special events void keypadEvent(KeypadEvent eKey){ switch (keypad.getState()){ case PRESSED: lcd.print(eKey); switch (eKey){ case '#': checkPassword(); break; case '*': displayCodeEntryScreen(); break; default: password.append(eKey); } switch (keypad.getState()){ case PRESSED: switch (eKey){ case 'D': myservo.write(180); displayCodeEntryScreen(); } } } }void checkPassword(){ if (password.evaluate()){ digitalWrite(greenLED, HIGH); lcd.clear(); delay(30); lcd.setCursor(1, 0); lcd.print("Acces Granted"); lcd.setCursor(4, 1); lcd.print("Welcome"); unlockdoor(); delay(2500); displayCodeEntryScreen(); } else{ loop(); { redlight(); } lcd.clear(); delay(10); lcd.setCursor(2, 0); lcd.print("Acces Denied"); delay(2500); lcd.clear(); displayCodeEntryScreen(); } }void unlockdoor(){ //controls servo that locks the door myservo.write(90); digitalWrite(greenLED, HIGH); delay(5000); }void redlight(){ digitalWrite(redLED, HIGH); delay(100); digitalWrite(redLED, LOW); delay(100); digitalWrite(redLED, HIGH); delay(100); digitalWrite(redLED, LOW); delay(100); digitalWrite(redLED, HIGH); delay(100); digitalWrite(redLED, LOW); delay(100); digitalWrite(redLED, HIGH); delay(100); digitalWrite(redLED, LOW); delay(100); digitalWrite(redLED, HIGH); delay(100); digitalWrite(redLED, LOW); delay(100); digitalWrite(redLED, HIGH); delay(100); digitalWrite(redLED, LOW); delay(100); digitalWrite(redLED, HIGH); delay(100); digitalWrite(redLED, LOW); delay(100); digitalWrite(redLED, HIGH); delay(100); }