I am having difficulties with this code I have edited, the portions that operate the servo and the RFID tag reader (MFRC522), but the screen is not printing the messages onto the the LCD display.
When I run the arduino example “Hello World”, the LCD displays the text perfectly fine, this leads me to believe the wiring is correct as I have duplicated the output/input pins used for Hello World and put it into the program I am using.
When the program is ran, it occasionally displays the characters “ff”
Here is the code I am using;
/*
----------------------------------------------------------------------------
Typical pin layout used:
-----------------------------------------------------------------------------------------
MFRC522 Arduino Arduino Arduino Arduino Arduino
Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
Signal Pin Pin Pin Pin Pin Pin
-----------------------------------------------------------------------------------------
RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
SPI SS SDA(SS) 10 53 D10 10 10
SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myservo; // create servo object to control a servo
String read_rfid;
String ok_rfid_1 = "6722323b";
String ok_rfid_2 = "99be859e";
int posClosed = 180; // variable to store the servo position for locked
int posOpen = 0; //same for open...
/*
Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
lcd.begin(16, 2);
lcd.clear();
lcd.print("Scan your card:");
Serial.println("Scan your card:");
//Choose which lock below:
myservo.attach(7); // attaches the servo on pin 7 to the servo object
}
/*
Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
read_rfid = "";
for (byte i = 0; i < bufferSize; i++) {
read_rfid = read_rfid + String(buffer[i], HEX);
}
}
void open_lock() {
//Use this routine when working with Servos.
//lcd.clear();
lcd.print("Servo Unlocking");
myservo.write(posOpen);
Serial.println("Servo Unlocking");
delay(10000);
myservo.write(posClosed);
lcd.clear();
lcd.print("Servo Locking");
Serial.println("Servo Locking");
}
void loop() {
lcd.clear();
lcd.print("test");
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
if (read_rfid == ok_rfid_1) {
open_lock();
lcd.clear();
lcd.print(read_rfid + " (FRED FARMER)" + " -Access Granted");
Serial.println(read_rfid + " (FRED FARMER)" + " -Access Granted");
delay(10000);
lcd.clear();
lcd.print("Scan a card:");
Serial.println("Scan a card:");
}
if (read_rfid == ok_rfid_2) {
open_lock();
lcd.clear();
lcd.print(read_rfid + " (JOHN DOE) " + " -Access Granted");
Serial.println(read_rfid + " (JOHN DOE) " + " -Access Granted");
delay(10000);
lcd.clear();
lcd.print("Scan a card:");
Serial.println("Scan a card:");
}
else {
lcd.clear();
lcd.print(read_rfid + " -Access Denied");
Serial.println(read_rfid + " -Access Denied");
delay(10000);
lcd.clear();
lcd.print("Scan your card:");
Serial.println("Scan your card:");
}
}