#include <Wire.h>
#include "MFRC522_I2C.h"
// 0x28 is i2c address on SDA.
MFRC522 mfrc522(0x28); // Create MFRC522 instance.
char buffer[15] = "";
void setup() {
Serial.begin(9600);
Wire.begin(); // Initialize I2C
mfrc522.PCD_Init(); // Init MFRC522
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
Serial.println("Scanning for a card...");
}
void loop() {
// Look for new cards, and select one if present
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
delay(50);
return;
}
// Now a card is selected. The UID and SAK is in mfrc522.uid.
int bigUid[mfrc522.uid.size];
for (int i = 0; i < mfrc522.uid.size; i++) {
bigUid[i] = mfrc522.uid.uidByte[i];
}
sprintf(buffer, "%03d:%03d:%03d:%03d", bigUid[0], bigUid[1], bigUid[2], bigUid[3]);
// Serial.println("buffer = ");
// Serial.println(buffer);
Compare();
}
void Compare() {
//compare card with known cards
String steveCard = "214:136:105:109";
String omaCard = "006:159:111:109";
String keyRing1 = "059:173:223:018";
Serial.print("Buffer = ");
Serial.println(buffer);
if (keyRing1 == buffer) {
Serial.println("Keyring");
}
else if (steveCard == buffer) {
Serial.println("LUKA");
delay(500);
}
/*else if (omaCard == buffer) {
Serial.println("LENA");
}*/
}
void ShowReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
}
Everything works fine with the second else if statement commented out as above and the following appears in the serial monitor if the correct card is held over the RFID:
Scanning for a card...
Buffer = 214:136:105:109
LUKA
However, if the second else if statement is uncommented the serial monitor shows:
Scanning for a card...
Scanning for a card...
Scanning for a card...
Whatever card is held to the RFID antenna.
What it happening here? It seems as though the sketch keeps restarting but why?
You have an array overrun. You should either use the String class (if you're not on an AVR Arduino) or don't use it at all and use C strings (character arrays). Don't mix that except you know exactly what you're doing!
A C string always have a terminating null character which doesn't fit in your array! As you use sprintf() instead of the much more robust snprintf() you overwrite your memory uncontrolled and depending on the surrounding code that ruins other parts of your program.
Thanks, I originally wrote the sketch for the M5Sstack and it worked fine on that. I have increased the buffer size to 20 and it works now on my Arduino Uno. Is that an acceptable workaround or could it cause problems later?
I will look up snprintf() as I've never used it before.
None, both produce a syntax error (may be caused because you didn't use code tags). As a long term forum member you should have learned that the forum system mangles code if you don't use code tags.
There seems to be no need to save processor time by else clauses so I guess there is no need for else anyway ... The "hardcoded" If-clauses would just do it, though your suggestion with an array and a for loop is more elegant, of course! ;-))
I wrote a sketch for the M5Stack Fire, which is actually an ESP32 with addons. The sketch worked fine on the M5Stack with an array of 15 to save an array of "XXX:XXX:XXX:XXX". I adapted the sketch for the Arduino Uno but when run on the Uno it kept restarting. I asked here what was wrong and Pylon correctly pointed out that an array overrun was the problem. You can see the sketch and thread here
I altered the array "buffer" to 20 from 15 and the sketch works now in the Uno. Pylon explained that a terminating null character was the reason for the overrun. But it is still unclear to me why the array length has to me longer for the Uno than for the ESP32. Can someone please explain?
By the way I have taken note that an array and a for loop would be more elegant than the else if statements in my sketch.
I think this is separate topic (array overrun) and nothing to do with the "else if" issue I originally thought was the problem. I only included the link so as not to have to post the whole code again.
Except that now I am more likely to get replies from people regarding the resolved "else if" issue than the array overrun isuue which is what interests me now. That is why I asked a separate question with a very clear title: Array overrun in ESP32 and Aurduino Uno.
Now I can only hope that Pylon answers as no-one else will know what I am asking from the title of this thread.