Trying to use LCD widget , but its always displaying “waiting for card” even if a card is scanned! Any idea what i might have got wrong in modifying the code to work with blynk?
Code Source - Arduino RFID+PIN
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Keypad.h>
char auth[] = "auth";
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {4, 5, 6, 7}; //Rows 0 to 3
byte colPins[numCols]= {14, 15, 16, 17}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
// initialize the library with the numbers of the interface pins
WidgetLCD lcd(V1);
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[4];
byte knownTac0[4] = {44,149,119,172};
byte knownTac1[4] = {74,123,176,171};
byte knownTac2[4] = {102,27,07,73};//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
boolean KNOW = true;
int id;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int redpin = 18;
int greenpin = 19;
void setup() {
// Debug console
DebugSerial.begin(9600);
// Blynk will work through Serial
// Do not read or write this serial manually in your sketch
Serial.begin(9600);
Blynk.begin(Serial, auth);
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
// set up the LCD's number of columns and rows:
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
// Print a message to the LCD and Serial.
lcd.clear();
lcd.print(0, 0, "Waiting for Card");
Serial.println("Waiting for Card");
digitalWrite(redpin, HIGH);
}
void loop() {
{
Blynk.run();
}
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been read
if ( ! rfid.PICC_ReadCardSerial())
return;
lcd.clear();
lcd.print(0, 0, "Card Detected");
Serial.println(F("Card Detected"));
delay(1000);
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
// comparing IDs
if (knownTac0[0] == nuidPICC[0] &&
knownTac0[1] == nuidPICC[1] &&
knownTac0[2] == nuidPICC[2] &&
knownTac0[3] == nuidPICC[3]){
KNOW = true;
id =0 ; // first case
}
else if(knownTac1[0] == nuidPICC[0] &&
knownTac1[1] == nuidPICC[1] &&
knownTac1[2] == nuidPICC[2] &&
knownTac1[3] == nuidPICC[3]){
KNOW = true;
id = 1; // 2nd case
}
else if(knownTac2[0] == nuidPICC[0] &&
knownTac2[1] == nuidPICC[1] &&
knownTac2[2] == nuidPICC[2] &&
knownTac2[3] == nuidPICC[3]){
KNOW = true;
id = 2; // 3rd case
}
else {
KNOW = false;
id=99; // default case
}
bool success = false;
switch(id){
case 0:
lcd.clear();
lcd.print(0, 0, "Sumbal");
Serial.println(F("Sumbal"));
delay(1000);
success = keypad_loop("1111");
break;
case 1:
lcd.clear();
lcd.print(0, 0, "Hamza");
Serial.println(F("Hamza"));
delay(1000);
success = keypad_loop("2222");
break;
case 2:
lcd.clear();
lcd.print(0, 0, "FAHAD");
Serial.println(F("FAHAD"));
delay(1000);
success = keypad_loop("3333");
break;
default:
lcd.clear();
lcd.print(0, 0, "Not Found");
Serial.println("Not found");
delay(1000);
}
if(success){
lcd.clear();
lcd.print(0, 0, "Door Unlocked!");
Serial.println("Door Unlocked!");
digitalWrite(redpin, LOW);
digitalWrite(greenpin, HIGH);
}
else{
lcd.clear();
lcd.print(0, 0, "Try again!");
Serial.println("Try again!");
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
// Print a message to the LCD and Serial.
delay(2000);
lcd.clear();
lcd.print(0, 0, "Waiting for Card");
Serial.println("Waiting for Card");
digitalWrite(greenpin, LOW);
digitalWrite(redpin, HIGH);
} // void loop() ends
///////////////////////////////////////////////////////////////////////////////////////
bool keypad_loop(String PIN){ // PIN section
lcd.clear();
lcd.print(0, 0, "Enter PIN...");
Serial.println("Enter PIN...");
String input;
int i = 0;
bool control = true;
bool success = false;
while(control == true){
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY){
if(i<4){ // 4-digit input
if(i==0)lcd.clear();
i++;
input.concat( String(keypressed) );
lcd.print(0, 0, "*");
}
if(i==4){
control = false;
if(input.equals(PIN) ){
success = true;
}
}
} // NO_KEY if ends
} // while ends
return success;
}