Book issue/renew project using RFID code help

Hello everyone! I'm fairly new to Arduino and just know the bare minimum to write a code in Arduino. So i am tring to do a book issue/renew system using RFID on arduino and need a bit of help here. The base of the program was taken from the RFID library dump and i've written the code in void loop. This is my code.

#include <SPI.h>
    #include <MFRC522.h>
    #define SS_PIN 10
    #define RST_PIN 9
    MFRC522 mfrc522(SS_PIN, RST_PIN); 
    int a,c1=0;
    void setup()
    {
       Serial.begin(9600);
       SPI.begin();      // Initiate  SPI bus
       mfrc522.PCD_Init();   // Initiate MFRC522
       Serial.println("Approximate your card to the reader...");
       Serial.println();
    }
    void loop()
    {
       Serial.println("ENTER THE STUDENT IDENTIFICATION CODE");
       if(mrfc522.uid.uidByte[0]==0x0c && mrfc522.uid.uidByte[1]==0x8c && 
          mrfc522.uid.uidByte[2]==0x91 && mrfc522.uid.uidByte[3]==0x79)
       {
          Serial.println("WELCOME /n 1.ISSUE /n 2.RENEWAL");
         if(Serial.available()>0)
         {
            a=Serial.read();
         }
         switch(a)
         {
           case 1:
           Serial.print("Scan the book for issue");
            if(!mrfc522.PICC_IsNewCardPresent())
            {
              Serial.print("Invalid Book");
            }
            if(!mrfc522.PICC_ReadCardSerial())
            {
              return;
            }
            if(mrfc522.uid.uidByte[0]==0x51 && mrfc522.uid.uidByte[1]==0xc5 && mrfc522.uid.uidByte[2]==0x11 && mrfc522.uid.uidByte[3]==0x2E && c1==0)
            {
              Serial.print("Book 1 has been issued");
              c1==1;
            }
            else
            {
              Serial.print("Book 1 has already been issued. Please renew it");
            }
            break;
      case 2:
            Serial.print("Scan the book for renew");
            if(!mrfc522.PICC_IsNewCardPresent())
            {
              Serial.print("Invalid Book");
            }
            if(!mrfc522.PICC_ReadCardSerial())
            {
              return;
            }
            if(mrfc522.uid.uidByte[0]==0x51 && mrfc522.uid.uidByte[1]==0xc5 && mrfc522.uid.uidByte[2]==0x11 && mrfc522.uid.uidByte[3]==0x2E && c1==1)
            {
              Serial.print("Book 1 has been renewed");
              
            }
            else
            {
              Serial.print("Book 1 has'nt been issued yet. Please issue it to renew");
            }
            break;
      default:
            Serial.print("Invalid option");
            break;
    }
    }
    }

So basically i'm scanning a students ID and then he makes a choice between issue and renew and the book is scanned accordingly. when compiling this code i get this error.

Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"

    C:\Users\tarun.9720\Desktop\liB_auto\liB_auto.ino: In function 'void 
    loop()':

    liB_auto:18: error: 'mrfc522' was not declared in this scope

    if(mrfc522.uid.uidByte[0]==0x0c && mrfc522.uid.uidByte[1]==0x8c && 
    mrfc522.uid.uidByte[2]==0x91 && mrfc522.uid.uidByte[3]==0x79)

      ^

    exit status 1
    'mrfc522' was not declared in this scope

Can anyone help me with this error. I'm a newbie so i might have done something wrong in the code.

EDIT1: So i've wrote 'mrfc' instead of 'mfrc' which was causing the compilation error. Now the code complies successfully but the output is on a infinte loop.

This statement is on loop. Can anyone help me in what wrong i've done in the code?

mfrc mrfc

Two different words.
Leo..

mrfc or mfrc?

Whoops.

Wawa:
mfrc mrfc

Two different words.
Leo..

Didn't notice that. Real embarrassing. But thanks a lot now i can try if it works!

if(Serial.available()>0)
         {
            a=Serial.read();
         }
         switch(a)
         {
           case 1:

2 problems here:

  1. switch() will continue to check old value if no new value is entered.
  2. are you sure you want to test against 1 and not '1'?

arduino_new:

if(Serial.available()>0)

{
            a=Serial.read();
        }
        switch(a)
        {
          case 1:




2 problems here:
1) switch() will continue to check old value if no new value is entered.
2) are you sure you want to test against **1** and not **'1'**?

since a is declared with int datatype i thought it was 1 that would be checked and not '1'.
Could you help me with what i'm doing wrong? Now with the mrfc mfrc confusion settled the code compiles but the output only shows the statement "ENTER THE STUDENT IDENTIFICATION CODE" on an infinite loop.

No. Serial.read will return the value received which in all likelihood is the character 1 or 49.

DKWatson:
No. Serial.read will return the value received which in all likelihood is the character 1 or 49.

If that's the case how do i take the value of variable a from the serial monitor?

If you receive a character from serial monitor, you receive text.

You can test for '1' (note the single quotes), the numeric values 49 (decimal) or 0x31'.

If you want to convert the character '1' to the integer 1, you can subtract the ascii value for the character '0'.

char ch = Serial.read();
Serial.print(ch - '0');

You should test first if the received character is between '0' and '9'.

sterretje:
If you receive a character from serial monitor, you receive text.

You can test for '1' (note the single quotes), the numeric values 49 (decimal) or 0x31'.

If you want to convert the character '1' to the integer 1, you can subtract the ascii value for the character '0'.

char ch = Serial.read();

Serial.print(ch - '0');



You should test first if the received character is between '0' and '9'.

Even with these changes the Infinite loop doesn't seem to stop. What am i doing wrong?

LoneWolf999:
What am i doing wrong?

Not posting your updated code :wink:

LoneWolf999:
Even with these changes the Infinite loop doesn't seem to stop. What am i doing wrong?

You never instantiate "mfrc522" before checking for its content. So the first test will never be true. So you never enter that block of code.

sterretje:
Not posting your updated code :wink:

The code is the same, except that i updated 'mrfc' to 'mfrc'. No other changes were made.

arduino_new:
You never instantiate "mfrc522" before checking for its content. So the first test will never be true. So you never enter that block of code.

So how can i fix this?

LoneWolf999:
So how can i fix this?

I am not familiar with the library but I presume you need to call "mrfc522.PICC.IsNewCardPresent()" first to see if there is a card to read.

Have you study the examples that come with the library?

LoneWolf999:
The code is the same, except that i updated 'mrfc' to 'mfrc'. No other changes were made.

No, it's not the same. We're always a bit sceptical as we can not see what you exactly changed.

From what you say, you did not even change what I tried to explain; so there is still e.g. a test (case) for 1 and not for '1' in your code.