跳到主要內容

選修分組-專題 / OLD / 芝麻開門

 

[Arduino範例] RFID RC522辨識系統入門,讀取UID和比對 

Mifare RFID-RC522模組實驗 

Ray 的芝麻開門  

最新程式(2021/1223)

1

 

1
 
#define SS_PIN 10                   //RC522 NSS 連接 Arduino pin 10
#define RST_PIN 9                   //RC522 RST 連接 Arduino pin 9
#define LED_G 5                     //綠色 LED 長腳連接 Arduino pin 5
#define LED_R 4                     //紅色LED 長腳連接 Arduino pin 4
#define BUZZER 2                    //蜂鳴器正極 連接 Arduino pin 2
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
Servo myServo;                       //define servo name
 
void setup() 
{
  Serial.begin(9600);                // Initiate a serial communication
  SPI.begin();                       // Initiate  SPI bus
  mfrc522.PCD_Init();                // Initiate MFRC522
  myServo.attach(3);                 // 伺服馬達黃線 連接 Arduino pin 3
  myServo.write(0);                   //servo start position
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  Serial.println("Put your card to the reader...");
  Serial.println();
 
}
void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  //if (content.substring(1) == "1A 4E 60 19") //old
  if (content.substring(1) == "D7 97 26 E0") //在此鍵入讓房門打開的 IC 卡片 ID 碼
  {
    //Serial.println("Authorized access");  //old。
    Serial.println("芝麻開門");  //如果感應模組讀到正確的 ID,則綠燈亮,何服馬達轉動,打開房門。
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    myServo.write(180);
    delay(5000);
    myServo.write(0);
    digitalWrite(LED_G, LOW);
  }
 
 else   {
    //Serial.println(" Access denied");//old。
    Serial.println("你是陌生人");   //如果感應模組讀到非設定好的ID,則紅燈亮,伺服馬達不轉動,房門依舊所著。
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(1000);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
}
 
1

時間類別單位標題發佈點閱
跳至網頁頂部