kode untuk sistem RFID Arduino yang menggunakan 2 RFID untuk masuk dan keluar

Berikut adalah contoh kode untuk sistem RFID Arduino yang menggunakan 2 RFID untuk masuk dan keluar:

#include <SoftwareSerial.h>

#include <MFRC522.h>


// Define pins for RFID readers

#define SS_PIN_IN 10

#define RST_PIN_IN 9

#define SS_PIN_OUT 8

#define RST_PIN_OUT 7


// Create instances of MFRC522 RFID readers

MFRC522 mfrc522In(SS_PIN_IN, RST_PIN_IN);

MFRC522 mfrc522Out(SS_PIN_OUT, RST_PIN_OUT);


// Define variables for storing RFID tag data

byte tagDataIn[4];

char tagStringIn[16];

byte tagDataOut[4];

char tagStringOut[16];


// Define variables for storing attendance data

int presentCount = 0;

int absentCount = 0;


// Define software serial for sending attendance data to computer

SoftwareSerial mySerial(2, 3);  // RX, TX


void setup() {

  // Initialize serial communication

  Serial.begin(9600);

  mySerial.begin(9600);

  

  // Initialize RFID readers

  SPI.begin();

  mfrc522In.PCD_Init();

  mfrc522In.PCD_SetAntennaGain(mfrc522In.RxGain_max);

  mfrc522Out.PCD_Init();

  mfrc522Out.PCD_SetAntennaGain(mfrc522Out.RxGain_max);

  

  // Print message to indicate setup is complete

  Serial.println("Ready to scan RFID tags");

}


void loop() {

  // Check if a tag is present at the entrance reader

  if (mfrc522In.PICC_IsNewCardPresent() && mfrc522In.PICC_ReadCardSerial()) {

    // Read tag data

    for (byte i = 0; i < 4; i++) {

      tagDataIn[i] = mfrc522In.uid.uidByte[i];

    }

    

    // Convert tag data to a string

    sprintf(tagStringIn, "%02X%02X%02X%02X", tagDataIn[0], tagDataIn[1], tagDataIn[2], tagDataIn[3]);

    

    // Check if the tag is already registered

    if (tagStringIn == "00112233") {  // Replace with the RFID tag ID for registered students

      // Increment the present count

      presentCount++;

      mySerial.println("PRESENT");

      

      // Print attendance data to serial monitor

      Serial.print("Present: ");

      Serial.print(presentCount);

      Serial.print(" Absent: ");

      Serial.println(absentCount);

    } else {

      Serial.println("Unauthorized tag");

    }

    

    // Halt PICC and prepare for new card

    mfrc522In.PICC_HaltA();

    mfrc522In.PCD_StopCrypto1();

  }

  

  // Check if a tag is present at the exit reader

  if (mfrc522Out.PICC_IsNewCardPresent() && mfrc522Out.PICC_ReadCardSerial()) {

    // Read tag data

    for (byte i = 0; i < 4; i++) {

      tagDataOut[i] = mfrc522Out.uid.uidByte[i];

    }

    

    // Convert tag data to a string

    sprintf(tagStringOut, "%02X%02X%02X%02X", tagDataOut[0], tagDataOut[1], tagDataOut[2], tagDataOut[3]);

    

    // Check if the tag is already registered

    if (tagStringOut == "00112233") {  // Replace with the RFID tag ID


0 Response to "kode untuk sistem RFID Arduino yang menggunakan 2 RFID untuk masuk dan keluar"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel

Iklan Bawah Artikel