Project is pretty simple I think but in reading a few different forum posts on here I'm a bit confused about whether or not this is the right way to go about this.
The way I'm conceptualizing this is that I have 2 card readers:
- 1 card reader that when a key fob is scanned will indicate that my cat is inside,
- 1 card reader that will indicate he is outside.
(extra detail- there will actually be 3 readers eventually but if this doesn't work with 2, there's no point in bothering with the third).
Is the reason people are saying you can't have 2 rfid readers because everyone was trying to wire them in serial? ... like theyd be trying to access the same pins?
because for my circuit i think i want the rfid readers to be on different pins altogether so i can use which reader is scanned to determine whether my cat is inside or outside.
There are 3 questions/ sources of confusion in the code below.
// fob uid: 4B 28 EF 8A
#include <IRremote.hpp>
#include <SPI.h>
#include <MFRC522.h>
#define INSIDE_RST_PIN 5
#define INSIDE_SS_PIN 53
#define AT_LARGE_RST_PIN 6
#define AT_LARGE_SS_PIN 43
#define KEANU_AT_LARGE_PIN 9 // Pin for an LED that will light up when my cat is outside.
#define KEANU_INSIDE_PIN 8 // Pin for an LED that will light up when he's inside.
// CONFUSION SOURCE #1:
// This 522 chip thing has 5 connections to different data pins on the circuit board, but we're only declaring 2 pins here in instantiation...
// how does the arduino know about the other pins?
MFRC522 inside_mfrc522(INSIDE_SS_PIN, INSIDE_RST_PIN); // the card reader that indicates he's inside.
MFRC522 at_large_mfrc522(AT_LARGE_SS_PIN, AT_LARGE_RST_PIN); // the card reader that indicates he's outside.
// initialize the mfrc readers.
void initMFRC(){
SPI.begin();
inside_mfrc522.PCD_Init();
at_large_mfrc522.PCD_Init();
delay(4); // in the example they said you needed this delay
inside_mfrc522.PCD_DumpVersionToSerial();
at_large_mfrc522.PCD_DumpVersionToSerial();
}
// initialize serial port
void initSerial(){
Serial.begin(9600);
while(!Serial);
}
// turn off all LEDs
void clearLEDS(){
digitalWrite(KEANU_INSIDE_PIN, LOW);
digitalWrite(KEANU_AT_LARGE_PIN, LOW);
}
// light up the cat is outside LED. These are separate functions because
// I'll be adding an LCD screen to this later.
void setStatusToAtLarge(){
digitalWrite(KEANU_AT_LARGE_PIN, HIGH);
}
// light up the cat is inside LED.
void setStatusToInside(){
digitalWrite(KEANU_INSIDE_PIN, HIGH);
}
// toggle the status of where he's at.
void toggleKeanuStatus(bool isAtLarge){
clearLEDS();
if(isAtLarge == true) {
Serial.println("AT LARGE");
setStatusToAtLarge();
} else {
Serial.println("INSIDE");
setStatusToInside();
}
}
void setup(){
initSerial();
initMFRC();
Serial.println(F("Scan PICC to see stuff"));
}
void loop() {
// CONFUSION SOURCE #2:
// PICC ReadCardSerial says i have to call one of 3 functions first
// and this one doesnt require any arguments so i'm calling it to do nothing. Is there a better/cleaner way to in instantiate this?
if ( !inside_mfrc522.PICC_IsNewCardPresent()){}
if ( !at_large_mfrc522.PICC_IsNewCardPresent()){}
// If the inside card reader picked him up, he's inside.
if ( inside_mfrc522.PICC_ReadCardSerial()){
toggleKeanuStatus(false);
}
// If the outside card reader picked him up, he's outside.
if ( at_large_mfrc522.PICC_IsNewCardPresent()){
toggleKeanuStatus(true);
}
}
// CONFUSION SOURCE #3:
// how can i match to his key fob? I can't figure out how to
// match the uid of his key fob (4B 28 EF 8A) to that as a string.
// this prints the uid:
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) { Serial.print(buffer[i], HEX); }
}
// but how can I turn that buffer into a string to match it against?