Last active
February 27, 2023 16:29
-
-
Save rahji/74b79b87364c839132af9076f81d9f86 to your computer and use it in GitHub Desktop.
RFID Code for Kat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <MFRC522.h> | |
#define SS_PIN 10 | |
#define RST_PIN 9 | |
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. | |
boolean successfullyReadtheFirstCard = false; | |
boolean card1Read, card2Read, card3Read; | |
unsigned long LED1OnTimestamp = 0; | |
const int LED1Pin = 3; | |
const int LEDTimeLimit = 3000; | |
// this function checks to see if there is a card to be read (true or false) | |
boolean thereIsACard() | |
{ | |
// if there's a card and it can be read... | |
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) | |
{ | |
return true; | |
} else { // no card was present or it couldn't be read | |
return false; | |
} | |
} | |
// this function returns the uppercase ID of the card being read, as a String | |
String returnCardID() | |
{ | |
String content = ""; | |
for (byte i = 0; i < mfrc522.uid.size; i++) | |
{ | |
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); | |
content.concat(String(mfrc522.uid.uidByte[i], HEX)); | |
} | |
content.toUpperCase(); | |
return content.substring(1); | |
} | |
void turnOffOldLEDs() { | |
if (millis() - LED1OnTimestamp >= LEDTimeLimit) { | |
digitalWrite(LED1Pin, LOW); | |
} | |
/* | |
if (millis() - LED2OnTimestamp >= LEDTimeLimit) { | |
digitalWrite(LED2Pin, LOW); | |
} | |
*/ | |
} | |
void setup() | |
{ | |
pinMode(LED1Pin, OUTPUT); | |
Serial.begin(9600); // Initiate a serial communication | |
SPI.begin(); // Initiate SPI bus | |
mfrc522.PCD_Init(); // Initiate MFRC522 | |
Serial.println("Ready"); | |
} | |
void loop() | |
{ | |
// skip the rest of the loop if there isn't a card to read | |
if (thereIsACard() == false) | |
return; | |
// read the ID of the card into the "cardID" variable | |
String cardID = returnCardID(); | |
// print the card ID | |
Serial.print("Card ID read: "); | |
Serial.println(cardID); | |
if (cardID == "91 D4 35 1D") { | |
card1Read = true; | |
} | |
else if (cardID == "91 D4 35 1E") { | |
card2Read = true; | |
} | |
else if (cardID == "91 D4 35 1F") { | |
card3Read = true; | |
} | |
if (cardID == "74 67 0E 04" && card1Read) | |
{ | |
Serial.println("card 1 was read, followed by card A!"); | |
digitalWrite(LED1Pin, HIGH); | |
LED1OnTimestamp = millis(); | |
} | |
turnOffOldLEDs(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment