Hi, i am trying to create a security system with arduino and these three components: the LCD (16x2), the KeyPad (4x4) and the Card Reader.
The problem is that i can't understand why they wont work all in the same sketch.
I don't think that i wired them uncorectly because if i try to use them individually, they work perfectly.
Here's the code:
#include <LiquidCrystal.h> //include LCD library (standard library)
#include <Keypad.h> //include keypad library
#include <SPI.h>
#include <MFRC522.h>
#define redLED 10 //define the LED pins
#define greenLED 11
#define SS_Pin 10
#define RST_Pin 9
#define LED_G 4
#define LED_R 5
#define Buzzer 2
MFRC522 mfrc522(SS_Pin, RST_Pin);
char* password ="1234"; //create a password
int pozisyon = 0; //keypad position
const byte rows = 4; //number of the keypad's rows and columns
const byte cols = 4;
char keyMap [rows] [cols] = { //define the cymbols on the buttons of the keypad
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins [rows] = {1, 2, 3, 4}; //pins of the keypad
byte colPins [cols] = {5, 6, 7, 8};
Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);
LiquidCrystal lcd (A0, A1, A2, A3, A4, A5); // pins of the LCD. (RS, E, D4, D5, D6, D7)
void setup(){
lcd.begin(16, 2);
pinMode(redLED, OUTPUT); //set the LED as an output
pinMode(greenLED, OUTPUT);
setLocked (true); //state of the password
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode(LED_G, OUTPUT);
pinMode(LED_R, OUTPUT);
pinMode(Buzzer, OUTPUT);
Serial.println("Put your card to the reader...");
Serial.println();
}
void loop(){
//KeyPad and LCD code
char whichKey = myKeypad.getKey(); //define which key is pressed with getKey
lcd.setCursor(0, 0);
lcd.print(" Welcome");
lcd.setCursor(0, 1);
lcd.print(" Enter Password");
if(whichKey == '*' || whichKey == '#' || whichKey == 'A' || //define invalid keys
whichKey == 'B' || whichKey == 'C' || whichKey == 'D'){
pozisyon=0;
setLocked (true);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Invalid Key!");
delay(1000);
lcd.clear();
}
if(whichKey == password [pozisyon]){
pozisyon ++;
}
if(pozisyon == 4){
setLocked (false);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("*** Verified ***");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Mert Arduino");
lcd.setCursor(0, 1);
lcd.print("Tutorial Project");
delay(7000);
lcd.clear();
}
delay(100);
//Card Reader Code
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.print("UID tag :");
String content = "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "xx xx xx xx") { //string for the correct key
Go();
} else {
Deny();
}
}
void setLocked(int locked){
if(locked){
Deny();
}
else{
Go();
}
}
void Go() {
Serial.println("Go");
delay(3000);
}
void Deny() {
Serial.println("Deny");
delay(3000);
}
If someone can help me i would apreciate that. ![]()