当前位置:   article > 正文

arduino控制RFID门禁卡_arduino rfid门禁

arduino rfid门禁

一、实物连接

在这里插入图片描述

二、代码实现



#include"rfid.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2);
RFID rfid; //create a variable type of RFID
#define relayPin 8  //relay module attach to pin8

uchar serNum[5]; // array to store your ID

void setup()
{
  lcd.init(); //initialize lcd
  lcd.backlight(); //turn on the backlight
  Serial.begin(9600);
  rfid.begin(7, 5, 4, 3, 6, 2);//rfid.begin(IRQ_PIN,SCK_PIN,MOSI_PIN,MISO_PIN,NSS_PIN,RST_PIN)
  delay(100); 
  rfid.init(); //initialize the RFID
  pinMode(relayPin, OUTPUT);  //set relayPin as OUTPUT
  digitalWrite(relayPin,HIGH); //and high level
  //Serial.begin(9600);
  lcd.setCursor(0,0);
  lcd.print("    Welcome!    "); //print"    Welcome!    "
  delay(2000);//delay 2s

}
void loop()
{
  uchar status;
  uchar str[MAX_LEN];
   // Search card, return card types
  status = rfid.request(PICC_REQIDL, str);
  if (status != MI_OK)
  {
    return;
  }
  // Show card type
  rfid.showCardType(str);
  //Prevent conflict, return the 4 bytes Serial number of the card
  status = rfid.anticoll(str);

  if (status == MI_OK)
  {
    //Serial.print("The card's number is: ");
    lcd.setCursor(0,0);
    lcd.print(" ID: ");
    memcpy(serNum, str, 5);
    rfid.showCardID(serNum);//show the card ID
    // Serial.println();

    // Check people associated with card ID 8F3D0329
    uchar* id = serNum;
    if( id[0]==0x8F && id[1]==0x3D && id[2]==0x03 && id[3]==0x29 ) 
    {
      digitalWrite(relayPin,LOW);
      // Serial.println("Hello Dannel!");
      lcd.setCursor(0,1);
      lcd.print(" Hello Dannel! ");
      delay(2000);
      lcd.clear();
      digitalWrite(relayPin,HIGH);
    } 
    //if the card id is AB8058A3,then relay connect 
    else if(id[0]==0xAB && id[1]==0x80 && id[2]==0x58 && id[3]==0xA3) 
    {
      digitalWrite(relayPin,LOW);
      //Serial.println("Hello Arduino");
      lcd.setCursor(0,1);
      lcd.print("Hello Arduino");
      delay(2000);
      lcd.clear();
      digitalWrite(relayPin,HIGH);
    } 
    else
    {
      //Serial.println("Hello unkown guy!");
      lcd.setCursor(0,1);
      lcd.print("Hello unkown guy");
      delay(2000);
      lcd.clear();
    }
  }
  lcd.setCursor(0,0);
  lcd.print("    Welcome!    ");
  delay(2000);
  rfid.halt(); //command the card into sleep mode 
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/839652
推荐阅读
相关标签
  

闽ICP备14008679号