Edit: I have found out that it was something wrong with the cables. I have solved the problem. Thanks for everyone who tried to help.
I am working a project that should basically do:
- Read the RFID card UID.
- Get the selection from user using pushbuttons.
- Restart the code for new cards.
The problem is, It reads the card and gets the input from pushbuttons. But after the first loop, It doesn't detect other cards (or the same card again). Here is my code: (I am using miguelbalboa's MFRC522 library and Arduino UNO)
#include <MFRC522Hack.h>
#include <MFRC522Extended.h>
#include <deprecated.h>
#include <MFRC522Debug.h>
#include <require_cpp11.h>
#include <MFRC522.h>
// protype 1 copy
#include <SPI.h>
// Define pin numbers for mfrc522
#define SS_PIN 10
#define RST_PIN 9
#define BLUE_BUT A0
#define RED_BUT A1
// Define pin numbers for lcd
const int RS = A0 , EN = A1 , D4 = A5 , D5 = A4 , D6 = A3 , D7 = A2;
int uids[] = {14805 ,25673};
int votedArray[] = {};
String uidNames[] = {"WHITE" , "BLUE"};
// Create LCD and MFRC522 instances
MFRC522 mfrc522(SS_PIN , RST_PIN);
unsigned long getCardID(int showCardData = 0){
/* this function gets the uid of the rfid card
and returns the uid in unsigned long format */
/* this fn. should be called with argument 1
if printing the card data to the serial monitor
is needed. */
unsigned long UID_unsigned;
UID_unsigned = mfrc522.uid.uidByte[0] << 24;
UID_unsigned += mfrc522.uid.uidByte[1] << 16;
UID_unsigned += mfrc522.uid.uidByte[2] << 8;
UID_unsigned += mfrc522.uid.uidByte[3];
if(showCardData==1){
Serial.println("UID Unsigned int");
Serial.println(UID_unsigned);
String UID_string = (String)UID_unsigned;
long UID_LONG=(long)UID_unsigned;
Serial.println("UID Long :");
Serial.println(UID_LONG);
Serial.println("UID String :");
Serial.println(UID_string); } // print card data to the serial monitor
int UID_int = (int) UID_unsigned;
return UID_int;
}
void showCardData(){
/* this fn. calls getCardID function
with argument 1 to print the data to
the serial monitor (kind of shortcut)*/
getCardID(1);
}
int findInList(int uid , int uidArray[]){
/* this fn. returns the index of the uid in the array if found,
returns -1 if not found*/
int numOfElements = sizeof(uidArray)/sizeof(uidArray[0]); // calculate the num.of elem. in array
for (int i = 0 ; i <= numOfElements ; i++) {
if(uid == uidArray[i]){
return i; // if element is found in the array, return true
}
}
return -1;
}
String getCardName(int cardUID){
int UIDIndex = findInList(cardUID , uids);
if(UIDIndex == -1){
return "ERROR";
}
else {
String cardName = uidNames[UIDIndex];
return cardName;
}
}
void printCardName(int cardUID){
String cardName = getCardName(cardUID);
if(cardName == "ERROR"){
Serial.print("ERROR! CARD UID: ");
Serial.println(cardUID);
}
else {
Serial.print("CARD NAME: ");
Serial.println(cardName);
}
}
void setup() {
Serial.begin(9600); // Init. serial comm. with the PC
SPI.begin(); // Init. SPI bus for MFRC522
mfrc522.PCD_Init(); // Init. MFRC522 card
pinMode(BLUE_BUT , INPUT);
pinMode(RED_BUT , INPUT);
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()){
Serial.println("CARD NOT PRESENT");
return;
}
// Verify if the NUID has been readed
if ( ! mfrc522.PICC_ReadCardSerial()) {
Serial.println("CARD NOT read");
return; }
int cardUID = getCardID();
Serial.println("CARD UID READ");
printCardName(cardUID);
Serial.println("SELECT WITH BUTTONS");
//getVote(cardUID);
int blueState = LOW , redState = LOW ;
while(1) {
blueState = digitalRead(BLUE_BUT);
redState = digitalRead(RED_BUT);
if(blueState == HIGH){
Serial.println("BLUE");
break;
}
if(redState == HIGH){
Serial.println("RED");
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
break;
}
}
Serial.println("PROGRAM COMPLETE");
}
I have already tried adding
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
but didn't help.
And here is the screenshot of the Serial Monitor after reading the first card and getting the button input.
I have checked the similar problems, but the solutions didn't help me.