Тема: RFID-RC522
Основная статья - http://rcl-radio.ru/?p=132410
#include <SPI.h>
#include <MFRC522.h> // http://rcl-radio.ru/wp-content/uploads/2024/08/rfid-master.zip
#include <EEPROM.h>
#include <Wire.h>
#include <U8glib.h> // https://github.com/olikraus/u8glib/
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_FAST);
int x;
int uid[4];
int master_kart[4]{129,71,80,131};
int set,eeprom;
unsigned long times,times_open,tim;
bool open_,tim1;
MFRC522 mfrc522(10,9); //(SS_PIN, RST_PIN)
void setup() {
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);
pinMode(3,INPUT_PULLUP);
if(EEPROM.read(500)!=0){for(int i=0;i<501;i++){EEPROM.update(i,0);}}
SPI.begin();
mfrc522.PCD_Init();
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(7,OUTPUT);// zummer
digitalWrite(7,HIGH);
}
void loop() {
// Чтение UID //////////////////////////////////////////////////////////////////////////////////
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()){delay(100);x=0;
for (int i = 0; i < 4; i++) {
uid[i] = mfrc522.uid.uidByte[i];
if(uid[i]==master_kart[i]){
x++;
}}}
else{cl();}
////////////////////////////////////////////////////////////////////////////////////////////////
//set ==0 read, set == 1 SAVE, set == 2 clear
if(x==4 && digitalRead(2)==LOW && uid[0]!=0 && set==0){set=1;ok();times=millis();cl();delay(2000);}
if(x==4 && digitalRead(3)==LOW && uid[0]!=0 && set==0){set=2;ok();times=millis();cl();delay(2000);}
if(set>0){
if(digitalRead(2)==LOW){eeprom++;if(eeprom>49){eeprom=49;}times=millis();delay(200);}
if(digitalRead(3)==LOW){eeprom--;if(eeprom<0){eeprom=0;}times=millis();delay(200);}
}
////////////////////////////////////////////////////////////////////////////////////////////////
if(uid[0]!=0 && uid[1]!=0 && uid[2]!=0 && uid[3]!=0){
Serial.print("UID: ");
for(int i1=0;i1<4;i1++){Serial.print(uid[i1]);Serial.print(" ");}Serial.println();}
////////////////////////////////////////////////////////////////////////////////////////////////
u8g.firstPage();
do {
/////////////////////////////////////////////////////////////////////////////
if(set==1){
u8g.setFont(u8g_font_profont17r);u8g.drawStr(0,12,"SAVE UID");
u8g.setFont(u8g_font_profont12r);
u8g.drawLine(0, 17, 128, 17);
u8g.drawStr(0,45,"UID:");
u8g.drawStr(0,30,"Nomber:");u8g.setPrintPos(50, 30);u8g.print(eeprom);
if(uid[0]!=0 && uid[1]!=0 && uid[2]!=0 && uid[3]!=0){
EEPROM.update(0+eeprom*4,uid[0]);
EEPROM.update(1+eeprom*4,uid[1]);
EEPROM.update(2+eeprom*4,uid[2]);
EEPROM.update(3+eeprom*4,uid[3]);
}
u8g.setPrintPos(30, 45);u8g.print(EEPROM.read(0+eeprom*4));
u8g.setPrintPos(55, 45);u8g.print(EEPROM.read(1+eeprom*4));
u8g.setPrintPos(80, 45);u8g.print(EEPROM.read(2+eeprom*4));
u8g.setPrintPos(105, 45);u8g.print(EEPROM.read(3+eeprom*4));
}
////////////////////////////////////////////////////////////////////////////
if(set==2){
u8g.setFont(u8g_font_profont17r);u8g.drawStr(0,12,"CLEAR UID");
u8g.setFont(u8g_font_profont12r);
u8g.drawLine(0, 17, 128, 17);
u8g.drawStr(0,45,"UID:");
u8g.drawStr(0,30,"Number:");u8g.setPrintPos(50, 30);u8g.print(eeprom);
if(uid[0]==master_kart[0]&&uid[1]==master_kart[1]&&uid[2]==master_kart[2]&&uid[3]==master_kart[3]){
EEPROM.update(0+eeprom*4,0);
EEPROM.update(1+eeprom*4,0);
EEPROM.update(2+eeprom*4,0);
EEPROM.update(3+eeprom*4,0);
}
u8g.setPrintPos(30, 45);u8g.print(EEPROM.read(0+eeprom*4));
u8g.setPrintPos(55, 45);u8g.print(EEPROM.read(1+eeprom*4));
u8g.setPrintPos(80, 45);u8g.print(EEPROM.read(2+eeprom*4));
u8g.setPrintPos(105, 45);u8g.print(EEPROM.read(3+eeprom*4));
}
//////////////////////////////////////////////////////////////////////////////
if(set==0){
u8g.setFont(u8g_font_profont17r);
if(uid[0]!=0 && uid[1]!=0 && uid[2]!=0 && uid[3]!=0&& millis()-times_open>3000){
for(int i=0;i<50;i++){
if(
uid[0]==EEPROM.read(0+i*4)&&
uid[1]==EEPROM.read(1+i*4)&&
uid[2]==EEPROM.read(2+i*4)&&
uid[3]==EEPROM.read(3+i*4))
{open_=1;times_open=millis();tim=millis();break;}else{open_=0;times_open=millis();tim1=1;}
}
}
}//set=0
if(tim1==1){tim1=0;digitalWrite(7,LOW);delay(100);digitalWrite(7,HIGH);}
if(open_==1 && millis()-times_open<3000){u8g.drawStr(40,30,"OPEN");digitalWrite(4,HIGH);
if(millis()-tim<200){digitalWrite(7,LOW);}
if(millis()-tim>200){digitalWrite(7,HIGH);}
if(millis()-tim>400){tim=millis();}
} else{digitalWrite(4,LOW);digitalWrite(7,HIGH);}
if(open_==0 && millis()-times_open<3000){u8g.drawStr(35,30,"ERROR");digitalWrite(5,HIGH);}
else{digitalWrite(5,LOW);}
} while( u8g.nextPage() );
if(millis()-times>20000){set=0;eeprom=0;}
}
void ok(){
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont17r);
u8g.drawStr(55,30,"OK");
} while( u8g.nextPage() );
}
void cl(){uid[0]=0;uid[1]=0;uid[2]=0;uid[3]=0;}