0

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:

  1. Read the RFID card UID.
  2. Get the selection from user using pushbuttons.
  3. 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. Serial Monitor

I have checked the similar problems, but the solutions didn't help me.

  • 2
    It's obviously returning on one of the two ifs at the start of your loop. Your job is to work out which. – Majenko Oct 20 '17 at 9:49
  • Thanks for your reply Majenko, you are right and I have checked it. I have found out that the first if statement (PICC_IsNewCardPresent()) causing it. But what can be the problem, why doesn't it detect the cards ? – JamCry Oct 20 '17 at 9:56
  • I think the clue might be the word "new" in there - it won't detect the same card twice (second time it's not new, it's old), and maybe the other cards are the same ID or maybe broken? Who knows... Do you get the same result regardless of which card you try first? – Majenko Oct 20 '17 at 9:58
  • I don't think it is due to cards, because when I run the code without the "getVote" part, It works fine: reads the card UID, prints the name of the card. Something in that part should be affecting the reader. And yes, the result is same regardless of the order of the cards. – JamCry Oct 20 '17 at 10:01
  • Which pins are RED_BUT and BLUE_BUT? – Majenko Oct 20 '17 at 10:03
1

I assume, Buttons and RFID reader both connected to the 3.3v pin on arduino. Move buttons to the 5v, leave RFID on 3.3v alone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.