Mijn code ziet er als volgt uit. Deze op het internet gevonden die mag gebruikt worden voor gebruik.
#include <Keypad.h> //Library for the matrix keypad available from http://playground.arduino.cc/code/Keypad
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> //You must use F. Malpartida's new LCD library for Serial LCD https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
#define I2C_ADDR 0x27 // Define I2C Address for the LCD. This varies by device, see the data sheet for yours
// Set up serial to parallel configuration for LCD
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
// define an LCD instance
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
// define keypad mapping
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap. Note the position of # and * alter between models
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 (top to bottom) to these Arduino pins.
byte rowPins[ROWS] = {
9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 (left to right) to these Arduino pins.
byte colPins[COLS] = {
12, 11, 10 };
// Create the Keypad instance
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
pinMode(4, OUTPUT); // pin for relais
pinMode(3, OUTPUT); // pin for green LED
pinMode(2, OUTPUT); // pin for red LED
lcd.begin (16,2); // initialise LCD
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
// clear the LCD
lcd.clear();
// move the cursor top left
lcd.home();
// print a message on the LCD to enter the code
lcd.print(" ENTER CODE");
lcd.setCursor(0, 1);
lcd.print("");
lcd.setCursor(0, 3);
}
void loop(){
String mastercode = "1234"; // the password
String enteredcode = ""; // the code you enter
String reason = " INCORRECT CODE "; // the default reason why entry failed
// read a 6 digit code (you can have any length)
for (int i = 0; i < 8; i++){
char key = kpd.waitForKey();
if(key != NO_KEY){ // if you aren't reading no keypress
lcd.print("*"); // print a # to the LCD so people don't see the number but you know you pressed a button
enteredcode += key; // append the keypress to the end of the entered code string
}
}
if (enteredcode == mastercode) // if the code is correct
{
lcd.setCursor (0, 3);
lcd.print(" ");
lcd.setCursor (0, 1);
lcd.print(" CODE CORRECT "); // print a success message
digitalWrite(3, HIGH); // turn on the green LED
digitalWrite(4, HIGH); // turn on the relais
delay (6000); // wait 3 seconds while the door is opened
digitalWrite(3, LOW); // turn off the LED
digitalWrite(4, LOW); // turn off the relais
//reset the LCD for the next user
lcd.clear();
lcd.home();
lcd.print(" ENTER CODE");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
}
else { // if the code is wrong or cancelled
// tell the user what happened
lcd.setCursor (0, 3);
lcd.print(" ");
lcd.setCursor (0, 1);
lcd.print(reason); // display the reason for failure. Incorrect code by default unless cancel is pressed
digitalWrite(2, HIGH); // turn on the red LED
delay (3000); // wait 3 seconds before allowing a retry
digitalWrite(2, LOW); // turn off LED
// reset the LCD for the next try
lcd.clear();
lcd.home();
lcd.print(" ENTER CODE");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 3);
}
}