- Kod: Markera allt
#include <SPI.h>
#include <MFRC522.h>
// PIN Numbers : RESET + SDAs
#define RST_PIN 9
#define SS_1_PIN 10
#define SS_2_PIN 8
#define SS_3_PIN 7
#define SS_4_PIN 6
// Relay PINS
#define relayIN 4
// List of Tags UIDs that are allowed to open the puzzle
byte tagarray[4] = {
0x2B,
0xBB,
0x81,
0xE6,
};
uint8_t tag[4];
#define NR_OF_READERS 4
byte ssPins[] = {SS_1_PIN, SS_2_PIN, SS_3_PIN, SS_4_PIN};
// Create an MFRC522 instance :
MFRC522 mfrc522[NR_OF_READERS];
/**
Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
/* Initializing Inputs and Outputs */
pinMode(relayIN, OUTPUT);
digitalWrite(relayIN, LOW);
/* looking for MFRC522 readers */
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN);
Serial.print(F("Reader "));
Serial.print(reader);
Serial.print(F(": "));
mfrc522[reader].PCD_DumpVersionToSerial();
//mfrc522[reader].PCD_SetAntennaGain(mfrc522[reader].RxGain_max);
}
}
/*
Main loop.
*/
void loop() {
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
// Looking for new cards
if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
Serial.print(F("Reader "));
Serial.print(reader);
// Show some details of the PICC (that is: the tag/card)
Serial.print(F(": Card UID:"));
dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
Serial.println();
tag[reader] = mfrc522[reader].uid.uidByte[0];
}
}
if (tag[0] == tagarray[0])
{
tag[0] = 0;
if (tag[1] == tagarray[1])
{
tag[1] = 0;
if (tag[2] == tagarray[2])
{
tag[2] = 0;
if (tag[3] == tagarray[3])
{
tag[3] = 0;
digitalWrite(relayIN, HIGH);
}
}
}
}
}
/**
Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte * buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
Testa denna
Den kollar bara första nummret i kort UID hoppas dina fyra är olika där.
Du fyller i dina värden här.
byte tagarray[4] = {
0x2B,
0xBB,
0x81,
0xE6,
};
När alla fyra är rätt så går utgång relayIN hög för alltid. Reseta arduinon för att låsa igen.