[SOLVED] RFID+ PIN + LCD Widget Problem

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;  
 }  

Thats alot of code in your voide loop

any solution? I edited it to this but still doesn’t work!

#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>  
 #include <SimpleTimer.h>
 SimpleTimer timer;
 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] = {43,149,119,172};  
 byte knownTac1[4] = {77,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);
   timer.setInterval(100L, rfidscan);
  // 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.  

  Serial.println("Waiting for Card");  
  digitalWrite(redpin, HIGH);  
 }  

void loop(){
 Blynk.run();
  }
  
void rfidscan() {  
  // 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, " Card");  
  Serial.println("Waiting for Card");  
  digitalWrite(greenpin, LOW);  
  digitalWrite(redpin, HIGH);  
 } // void rfidscan() 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;  
 }  

It’s not working because now you are not really doing much except waiting for App input.

You probably need to use timers to scan for your RFID and keypad sensor input.

Just a heads up, this forum is not a code factory or a how to program tutorial. We are here to help you with Blynk related questions, and while that does occasionally involve some basic programming advice, this is the 2nd program you have brought up in three days, looking for general “how to make work” programming help.

Perhaps you should start with the basics offered in the Sketch Builder and work your way up from there? Once you have a better understanding of Blynk and it’s commands and structure, you will be able to Blynkify other programs much easier.

1 Like

um… sorry…
I don’t want to know how to program , i myself can’t see why the code shouldn’t be working and so i thought i would ask it in blynk community , as it is a blynk project after all! And the “i need help with my project” section is there to help the blynk community to learn more about blynk if i am not wrong!
I just wanna know more about it and i am trying something new , not tried by anyone else!

Thanks for pointing out what might be wrong!

1 Like

While Blynk can be used without learning to program, it will be limited to basic functions, as any other similar no-coding type application (just much nicer looking :smiley: ).

It actually is NOT a “Blynk” program that you are wishing to modify… thus my reasoning for bringing it up… It will take programming knowledge to combine an in depth program like that with Blynk’s App and Server IoT methodology.

The Blynk community IS here to help each other… help others learn Blynk that is, but NOT to do the work for them if they don’t what to learn themselves.

Umm, sorry… but that is not likely :wink: Many others have done some incredible things with Blynk… but again… not without an applied effort on their part to overcome the learning curve.

Spend some quality time going through the Documents, Help Center and Sketch Builder… and then you will eventually understand what I am saying.

My advice would be to go back to the original code that you found on the internet and get that working on your Arduino.
Once that’s working then modify it to work with Blynk. You have the right idea about having the code that scans for an RFID card in a separate function and as previously stated you need to be calling this code on a timer from your void loop. Sketch builder will give you examples of how this is done, you just need to change the name of the function that is being called by the timer.

On a slightly different note, you’re currently connecting your Arduino to the internet using a USB connection to your PC. This clearly isn’t going to work if you’re using this as a long-term RFID access system. If it’s going to be used in a location where you have access to wired Ethernet you could add an Ethernet shield, along with the appropriate code, to enable the Arduino to talk to Blynk. If Wi-Fi is a better solution then you could connect your Arduino to an ESP8266 to give internet access, but a better option may be to run the whole thing on an ESP8266 (you can program them from the Arduino IDE like an Arduino and they have more memory and faster processor than Arduinos).
The only thing to look out for is that the ESP8266 uses 3.3v logic compared to 5v for the Arduino. Your RFID reader and keypad may be using 5v logic outputs which would kill the ESP without additional circuitry.

Pete.

2 Likes

Thanks! Followed Your Advice and its working Greaaatt nowww!!! :smiley:
Blynk rocks!

Sounds great! @PeteKnight had some good advice :+1:

If you can, post what you did differently that worked. And it might be nice if you could show your working code for the next person searching this forum for code examples and ideas.

Happy Blynking!