Hello, I'm making a automatic gate using RFID reader. Here I have used EEPROM to save the RFID tag numbers. I want to add new RFID tag number to EEPROM If the number is not in the EEPROM. I tried many ways but nothing worked. Can anyone give me a solution for this. My code is as follows.
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <EEPROM.h>
#include <SafeString.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int readsuccess;
byte readcard[4];
char str[32] = "";
String StrUID;
String temp = "";
boolean granted = false;
char myTags[][9] = {};
char reloadedTags[sizeof(myTags) / sizeof(myTags[0])][9];
Servo gate;
int pos = 0;
//---------------------------------------------------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
gate.attach(8);
gate.write(0);
//write_Tag();
// eeprom_clear();
}
// ------------------------------------------------------------------------------------------------------------------------------
void loop() {
readsuccess = getid();
if (readsuccess) {
Serial.println(StrUID);
checkAccess (StrUID);
add(StrUID);
}
}
// -------------------------------------------------------------------------------------------------------------------------------
int getid() {
if (!mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return 0;
}
for (int i = 0; i < 4; i++) {
readcard[i] = mfrc522.uid.uidByte[i]; //storing the UID of the tag in readcard
array_to_string(readcard, 4, str);
StrUID = str;
}
mfrc522.PICC_HaltA();
return 1;
}
// ------------------------------------------------------------------------------------------------------------------------------
void array_to_string(byte array[], unsigned int len, char buffer[]) {
for (unsigned int i = 0; i < len; i++) {
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len * 2] = '\0';
}
// -------------------------------------------------------------------------------------------------------------------------------
void checkAccess (String StrUID) { //Function to check if an identified tag is registered to allow access
while (!Serial);
EEPROM.get(0, reloadedTags);
for (int tag = 0; tag < (sizeof(myTags) / sizeof(myTags[0])); tag++) {
for (int c = 0; c < 8; c++) {
temp += reloadedTags[tag][c];
}
//Serial.println(temp);
if (temp == StrUID) {
//Serial.println("found");
open();
granted = true;
}
else{
granted = false;
delay(500);
}
temp = "";
}
return granted;
Serial.println("false");
}
//-------------------------------------------------------------------------------------------------------------------------------------
void open() {
int pos = 0;
for (pos = 0; pos <= 90; pos += 1) {
gate.write(pos);
}
delay(2000);
for (pos = 90; pos >= 0; pos -= 1) {
gate.write(pos);
}
}
//---------------------------------------------------------------------------------------------------------------------------------
void write_Tag() {
while (!Serial);
EEPROM.put(0, myTags);
}
//------------------------------------------------------------------------------------------------------------------------------------
void eeprom_clear() {
while (!Serial);
EEPROM.put(0, 0);
}
//----------------------------------------------------------------------------------------------------------------------------------
void add(String StrUID) {
if(granted==false){
createSafeStringFromCharPtrWithSize(myTags4, myTags[sizeof(myTags) / sizeof(myTags[0])], 9);
myTags4 = StrUID.c_str();
//Serial.println(myTags4);
// while (!Serial);
// EEPROM.put(0, myTags);
// delay(100);
// EEPROM.put(0, myTags);
for (int i = 0; i <sizeof(myTags) / sizeof(myTags[0])+1; i++) {
for (int j = 0; j < 8; j++) {
Serial.print(myTags[i][j]);
//EEPROM.put(0, myTags);
}
Serial.println("");
}
}
}
The add function is used to save the data to EEPROM if it is unavailable. Reading from the EEPROM and comparing with the current RFID number works fine. the problem is with the updating the EEPROM. Please help me on this.