0
 for (byte i = 0; i < mfrc522.uid.size; i++)
 {
 Serial.print(mfrc522.uid.uidByte{i} < 0x10 ? " 0" : " ");
 Serial.print(mfrc522.uid.uidByte{i}, HEX);
 content.concat(String(mfrc522.uid.uidByte{i} < 0x10 ? " 0" : " "));
 content.concat(String(mfrc522.uid.uidByte{i}, HEX));
 }

I use this code to get UID of an RFID card, but the error "expected ')' before '{' token" comes out. Does anybody know how to solve this problem?

ocrdu
1,7953 gold badges12 silver badges24 bronze badges
asked Nov 23, 2020 at 9:28
2
  • 2
    This is a simple C(++) error and not Arduino-specific. To address array elements you should use square brackets ([, ]) instead of curly brackets. Replace all instances of {i} with [i] and you will be good to go. Commented Nov 23, 2020 at 9:31
  • 3
    I’m voting to close this question because it's a trivial syntax error question. Commented Nov 23, 2020 at 12:52

1 Answer 1

1

Welcome to this site, next time use ctrl-k to align your code.

The problem is in:

Serial.print(mfrc522.uid.uidByte{i} < 0x10 ? " 0" : " ");

To get the value of an array, use [i], not {i}, thus

Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");

(similar to other lines).

To give some additional hints:

  • Try to prevent duplicated code by storing the byte into a variable. Now you can use uidValue in the subsequent lines.

    uint8_t uidValue = mfrc522.uid.uidByte[i];

  • Store 0x10 inside a constant.

So you get:

const uint8_t MAX_HEX_SINGLE_DIGIT_VALUE 0x0F;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
 uint8_t uid_byte = mfrc522.uid.uidByte[i];
 Serial.print(uid_byte < LOW_VALUE <= MAX_HEX_SINGLE_DIGIT_VALUE ? " 0" : " ");
 Serial.print(uid_byte, HEX);
 content.concat(String(uid_byte < MAX_HEX_SINGLE_DIGIT_VALUE ? " 0" : " "));
 content.concat(String((uid_byte, HEX));
 }

I assume the Serial lines are only for debugging, otherwise you can combine them too.

answered Nov 23, 2020 at 9:29
8
  • 1
    Thank you very much, Michel. Commented Nov 23, 2020 at 9:32
  • 3
    Michel, if you answer a syntax error question, then at least without syntax errors. I thought you agreed that syntax error questions shouldn't be answered. Commented Nov 23, 2020 at 12:26
  • 2
    they will reject a syntax error question. it is off-topic on SO Commented Nov 23, 2020 at 12:31
  • 1
    to ask a question for every syntax error is not a way to learn codding. a codding course is a much better way Commented Nov 23, 2020 at 17:57
  • 1
    @Juraj .. However, at least instead of answering a duplicate question could be found. Although I hope if the OP stays interested he will do a course, or at least find out how C(++) syntax works. Commented Nov 23, 2020 at 21:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.