Here's the whole error:
Arduino: 1.6.7 (Windows 7), Board: "Arduino/Genuino Uno"
*\readMifare\readMifare.ino: In function 'void loop()':
readMifare:113: error: expected primary-expression before ']' token
if (uid[] == { 0x04, 0xEC, 0x89, 0x32, 0x55, 0x42, 0x80 }) {
^
readMifare:113: error: expected primary-expression before '{' token
if (uid[] == { 0x04, 0xEC, 0x89, 0x32, 0x55, 0x42, 0x80 }) {
^
readMifare:113: error: expected ')' before '{' token
readMifare:121: error: expected '}' at end of input
}
^
exit status 1
expected primary-expression before ']' token
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Here's the code
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK (2)
#define PN532_MOSI (3)
#define PN532_SS (4)
#define PN532_MISO (5)
// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (3) // Not connected by default on the NFC Shield
// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:
// Use this line for a breakout with a software SPI connection (recommended):
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
// Use this line for a breakout with a hardware SPI connection. Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);
// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
// also change #define in Adafruit_PN532.cpp library file
#define Serial SerialUSB
#endif
#include <Servo.h>
Servo servo1;
void setup(void) {
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(115200);
Serial.println("Hello!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A Card ...");
servo1.attach(9);
}
void loop(void) {
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (uid[] = { 0x04, 0xEC, 0x89, 0x32, 0x55, 0x42, 0x80 }) {
int position;
for(position = 0; position < 180; position += 2){
servo1.write(position);
delay(20);
}
}
Please note that I'm using an example from the Adafruit official PN532 library examples and trying to edit it.
2 Answers 2
I wouldn't use strncmp
because the UID might have zeroes in it. This looks safer:
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
const uint8_t wantedUid [] = { 0x04, 0xEC, 0x89, 0x32, 0x55, 0x42, 0x80 };
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success &&
uidLength == sizeof (wantedUid) &&
memcmp (uid, wantedUid, sizeof wantedUid) == 0) {
...
I threw in a test for the uidLength being the expected size, otherwise you might find a 4-byte card happens to match the first four bytes of the wanted UID.
memcmp
compares a block of memory for the number of bytes you specify. It returns zero on a match.
You probably need something like:
uint8_t uidTarget[] = { 0x04, 0xEC, 0x89, 0x32, 0x55, 0x42, 0x80 };
...
if (success && !strncmp((const char*) uidTarget,(const char*)uidSource,8)) {
Your '=' tries to do an assignment, and an '==' would check if the pointers to the arrays are identical, rather than the contents of the arrays. strncmp() tests equality of strings of characters, and the (const char*) casts the uint8_t values as characters.
Nick Gammon's answer is better.
-
added what you told me to add, error: readMifare:113: error: invalid conversion from 'uint8_t* {aka unsigned char*}' to 'const char*' [-fpermissive] if (success && !strncmp(uid,{ 0x04, 0xEC, 0x89, 0x32, 0x55, 0x42, 0x80 },8)) { readMifare:113: error: cannot convert '<brace-enclosed initializer list>' to 'const char*' for argument '2' to 'int strncmp(const char*, const char*, size_t)' readMifare:121: error: expected '}' at end of input } exit status 1 invalid conversion from 'uint8_t* {aka unsigned char*}' to 'const char*' [-fpermissive]Dahacker305– Dahacker3052016年02月16日 19:21:53 +00:00Commented Feb 16, 2016 at 19:21
-
@Dahacker305: remove the
const
.Edgar Bonet– Edgar Bonet2016年02月16日 20:23:23 +00:00Commented Feb 16, 2016 at 20:23 -
"if (success && !strncmp(uid,{ 0x04, 0xEC, 0x89, 0x32, 0x55, 0x42, 0x80 },8))" - no you cannot do that. The ID must be an array variable.Mikael Patel– Mikael Patel2016年02月17日 10:24:24 +00:00Commented Feb 17, 2016 at 10:24
==
.=
is for assignment. Common mistake, even by the professionals.