I finally got my RFID code working. The output of the card ID is: 00A4EA59. When I convert this from HEX to DEC (in calculator) I get 10807897 which is the code of my card.
But how can I do this conversion in the Arduino code ?
I tried strtol but this gave the obvious error that a const char is expected instead of an unsigned char.
Anybody got an idea ?
The complete code:
unsigned char card[12]; // Contains the value of the RFID card
unsigned char prevcard[12]; // Contains the value of the previously read card
unsigned char i=0; // number of read loops
unsigned int different=0; // Check if the card has been read already
unsigned long prevmillis=0; // Contains time of last use card
unsigned long millisdelay=5000; // Timeout between use of same card
void setup() {
Serial.begin(9600); // Start Serial interface to computer
Serial1.begin(9600); // Start Serial interface for RFID reader
Serial.println("Start Cardreader");
Serial.println("----------------------------------");
}
void loop() {
if (Serial1.available()>0) { // Check if a card has been read
card[i]=Serial1.read(); // Fill the array with the value, 10 times needed
if ((millis()-prevmillis)>millisdelay || millis()<prevmillis) { // Check if the delay has been crossed or that millis started from 0 again
memset(prevcard,0,12); // Clear the prevcard array
}
i++; // add 1 to i
if (i>=11) { // Check if loop has passed 11 times
i=0; // Reset i
if (memcmp(prevcard,card,12)!=0) { // Check if the previous read card is different from the current
different=1;
}
if (different) { // If previous card is different
memmove(prevcard,card,12); // Copy card array contents to prevcard array
different=0; // Reset different check
prevmillis=millis(); // Set new last read time
decode(); // Do something with the RFID keycode
Serial.println("----------------------------------");
}
delay(10); // Delay to avoid double reading
Serial1.flush(); // Clear the serial buffer so we can read a new card
}
}
}
void decode() {
Serial.print("Checksum: ");
for(int p=0;p<4;p++) { // Write only the checksum, not the card number
Serial.print(card[p]);
}
Serial.print(", Card ID: ");
for(int p=3;p<11;p++) { // Write only the card number, not the checksum, outputs 00A4EA59
Serial.print(card[p]);
}
Serial.println("");
}
EDIT: I'm using the Arduino Mega, that's why there's a Serial1