1

Тема: RFID-RC522

Основная статья - http://rcl-radio.ru/?p=132410

http://forum.rcl-radio.ru/uploads/images/2024/08/e4ad83c1838e16e5bd6c445ba473274c.png


#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;}  

2

Re: RFID-RC522

http://forum.rcl-radio.ru/uploads/images/2024/08/4cd59ff7290df19f14726ed65da4645f.png

3

Re: RFID-RC522

http://forum.rcl-radio.ru/uploads/images/2024/08/cef49ea7ef263b9c7455fe29029eef51.png

http://forum.rcl-radio.ru/uploads/images/2024/08/4ec518bed9fe14e7084ec48ce38afd3d.png

http://forum.rcl-radio.ru/uploads/images/2024/08/9f7d853a99d91627ed0f840a9231dc90.png

http://forum.rcl-radio.ru/uploads/images/2024/08/4e6b9c87c0820ce468c003250b8684c7.png

http://forum.rcl-radio.ru/uploads/images/2024/08/eb1817d1dba10de1d8c908f120dee569.png

http://forum.rcl-radio.ru/uploads/images/2024/08/6baf23ace59b9ac437d16e8ecd134290.png

4

Re: RFID-RC522

LCD1602_I2C

#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 <LiquidCrystal_I2C.h>  // http://forum.rcl-radio.ru/misc.php?action=pan_download&item=45&download=1 

LiquidCrystal_I2C lcd(0x27,16,2);
 
int x;
int uid[4];
int master_kart[4]{129,71,80,131}; 
int set,eeprom; 
unsigned long times,times_open,tim; 
int open_,tim1; 

 
MFRC522 mfrc522(10,9); //(SS_PIN, RST_PIN)   
 
void setup() {
  Serial.begin(9600); 
  lcd.init();lcd.noBacklight();lcd.clear();
  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(200);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;lcd.clear();ok();times=millis();cl();delay(2000);}
   if(x==4 && digitalRead(3)==LOW && uid[0]!=0 && set==0){set=2;lcd.clear();ok();times=millis();cl();delay(2000);}

  if(set>0){
    if(digitalRead(2)==LOW){lcd.clear();eeprom++;if(eeprom>49){eeprom=49;}times=millis();delay(200);}
    if(digitalRead(3)==LOW){lcd.clear();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();}
////////////////////////////////////////////////////////////////////////////////////////////////
  if(set==1){
    lcd.backlight();
    lcd.setCursor(0, 0);lcd.print("SAVE  UID|NUM:");
    lcd.setCursor(14, 0);lcd.print(eeprom);
   
    if(uid[0]!=0 && uid[1]!=0 && uid[2]!=0 && uid[3]!=0){lcd.clear();
      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]);
      }
      
    lcd.setCursor(0, 1);lcd.print(EEPROM.read(0+eeprom*4));lcd.print(".");
    lcd.setCursor(4, 1);lcd.print(EEPROM.read(1+eeprom*4));lcd.print(".");
    lcd.setCursor(8, 1);lcd.print(EEPROM.read(2+eeprom*4));lcd.print(".");
    lcd.setCursor(12, 1);lcd.print(EEPROM.read(3+eeprom*4));
    }
////////////////////////////////////////////////////////////////////////////
  if(set==2){
    lcd.backlight();
    lcd.setCursor(0, 0);lcd.print("CLEAR UID|NUM:");
    lcd.setCursor(14, 0);lcd.print(eeprom);

    if(uid[0]==master_kart[0]&&uid[1]==master_kart[1]&&uid[2]==master_kart[2]&&uid[3]==master_kart[3]){lcd.clear();
      EEPROM.update(0+eeprom*4,0);
      EEPROM.update(1+eeprom*4,0);
      EEPROM.update(2+eeprom*4,0);
      EEPROM.update(3+eeprom*4,0);
      }

    lcd.setCursor(0, 1);lcd.print(EEPROM.read(0+eeprom*4));lcd.print(".");
    lcd.setCursor(4, 1);lcd.print(EEPROM.read(1+eeprom*4));lcd.print(".");
    lcd.setCursor(8, 1);lcd.print(EEPROM.read(2+eeprom*4));lcd.print(".");
    lcd.setCursor(12, 1);lcd.print(EEPROM.read(3+eeprom*4));
    }
//////////////////////////////////////////////////////////////////////////////  
  if(set==0){
  if(uid[0]!=0 && uid[1]!=0 && uid[2]!=0 && uid[3]!=0){ 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;break;}else{open_=2;}}}}//set=0
 
  if(open_==1){open_=0;lcd.backlight();lcd.setCursor(6, 0);lcd.print("OPEN");digitalWrite(4,HIGH);
  for(int n=0;n<8;n++){digitalWrite(7,LOW);delay(200);digitalWrite(7,HIGH);delay(200);}
  lcd.noBacklight();lcd.clear();digitalWrite(4,LOW);digitalWrite(7,HIGH);}
   
  if(open_==2){open_=0;lcd.backlight();lcd.setCursor(5, 0);lcd.print("ERROR");digitalWrite(5,HIGH);
  digitalWrite(7,LOW);delay(100);digitalWrite(7,HIGH);delay(3000);digitalWrite(5,LOW);lcd.noBacklight();lcd.clear();}
 
  if(millis()-times>10000){lcd.noBacklight();lcd.clear();set=0;eeprom=0;}
}

void ok(){ lcd.backlight();lcd.setCursor(0, 0);lcd.print("OK");}

void cl(){uid[0]=0;uid[1]=0;uid[2]=0;uid[3]=0;}