Erroe problem: expected unqualified-id before 'if'

Hello!

I'm currently working with RC-522 RFID and Arduino UNO.

I put the code for card reading in a loop and it works just fine though the problem is that it prints the serial number more than once (ofc because it's in a loop). I tried putting the if function alone, but I get the following error: expected unqualified-id before 'if'.

The following is the code:

#include <SPI.h>
#include <RFID.h>
#define SS_PIN 10
#define RST_PIN 9
RFID rfid(SS_PIN, RST_PIN);
String rfidCard;

void setup() {
  Serial.begin(9600);
  Serial.println("Starting the RFID Reader...");
  SPI.begin();
  rfid.init();
}

void loop() {
  if (rfid.isCard()) {
    if (rfid.readCardSerial()) {
      rfidCard = String(rfid.serNum[0]) + " " + String(rfid.serNum[1]) + " " + String(rfid.serNum[2]) + " " + String(rfid.serNum[3]);
      Serial.println(rfidCard);
  
    }
    rfid.halt();
  }
}

So, does anyone have any suggestions on how to fix the following error or on how to print the RFID serial number only once?

Thanks a lot!!

Your code is incomplete.

How so?

On my tablet I don’t see complete code, perhaps because I can’t scroll.

But this


if (rfid.isCard()) {
  if (rfid.readCardSerial()) {
     rfidCard = String(rfid.serNum[0]) + " " + String(rfid.serNum[1]) + " " + String(rfid.serNum[2]) + " " + String(rfid.serNum[3]);
     Serial.println(rfidCard);
    
           }
    rfid.halt();
  }

is outside of any function, where such things are not allowed by the syntax of C/C++.

Any statements like that have to be within the definition of a function.

You did not see that in any examples you may have come across.

That’s enough to make all kindsa errors pop up; sometimes the exact error reported is just an after effect of misplaced items and can be hard to decipher.

It would help in the future to

  • post your entire code (if indeed you had not)

  • cut and paste the error message in its entirety

which would include info pointing out ‘xactly where the compiler started to get unhappy.

HTH

a7

Try deleting the "}" before the first "if"

Because that "if" isn't in a function.

Edit: and now you've changed the code. Grrrr.

Please don’t change the earlier posting.

It makes the following comments into nonsense, and srsly erodes our enthusiasm for following along and helping.

Publish new or fixed code in a new post, please.

a7

Sorry! I added the loop function. Even though I want to get rid of it.

The place for anything that you only want to happen once at the start is in the setup() function.

If you post code and claim an error message for it, post the actual code (all of it) and the actual error message (all of it).

Don't then amend the code already posted.

My enthusiasm for this topic is now seriously eroded, and I leave you to it.

1 Like

@unknownm98987
Topic restored and locked.
Please do not delete topics.
Please flag to mods if you are not happy with the way you are treated.

Thank you.

2 Likes