Hi, I'm trying to use 4 Arduino RFID scanners to signal a piezo when all 4 RFID scanners have a card on top. I'm quite new to coding so I might have some obvious mistakes.
This here is my code:
#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
int piezoPin = 8;
// List of Tags UIDs that are allowed to open the puzzle
byte tagarray[][4] = {
{0x4B, 0x17, 0xBC, 0x79},
{0x8A, 0x2B, 0xBC, 0x79},
{0x81, 0x29, 0xBC, 0x79},
{0xE6, 0xDF, 0xBB, 0x79},
};
// Inlocking status :
int tagcount = 0;
bool access = false;
#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
/* 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();
for (int x = 0; x < sizeof(tagarray); x++) // tagarray's row
{
for (int i = 0; i < mfrc522[reader].uid.size; i++) //tagarray's columns
{
if ( mfrc522[reader].uid.uidByte[i] != tagarray[x][i]) //Comparing the UID in the buffer to the UID in the tag array.
{
DenyingTag();
break;
}
else
{
if (i == mfrc522[reader].uid.size - 1) // Test if we browesed the whole UID.
{
AllowTag();
}
else
{
continue; // We still didn't reach the last cell/column : continue testing!
}
}
}
if (access) break; // If the Tag is allowed, quit the test.
}
if (access)
{
if (tagcount == NR_OF_READERS)
{
Piezo();
}
else
{
MoreTagsNeeded();
}
}
else
{
UnknownTag();
}
/*Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));*/
// Halt PICC
mfrc522[reader].PICC_HaltA();
// Stop encryption on PCD
mfrc522[reader].PCD_StopCrypto1();
} //if (mfrc522[reader].PICC_IsNewC..
} //for(uint8_t reader..
}
/**
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);
}
}
void printTagcount() {
Serial.print("Tag n°");
Serial.println(tagcount);
}
void DenyingTag()
{
tagcount = tagcount;
access = false;
}
void AllowTag()
{
tagcount = tagcount + 1;
access = true;
}
void Initialize()
{
tagcount = 0;
access = false;
}
void Piezo()
Serial.println("Congratulations!");
tone(piezoPin, 1000, 500);
//tone(piezoPin, 1000, 500);
//delay(1000);
}
void MoreTagsNeeded()
{
printTagcount();
Serial.println("System needs more cards");
tone(piezoPin, 1000, 500);
access = false;
}
void UnknownTag()
{
Serial.println("This Tag isn't allowed!");
printTagcount();
tone(piezoPin, 1000, 500);
}
}
This is the error message I get:
Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"
C:\Users\190240\Desktop\4_RFID_Readers\4_RFID_Readers.ino: In function 'void loop()':
4_RFID_Readers:100:15: error: 'Piezo' was not declared in this scope
Piezo();
^~~~~
C:\Users\190240\Desktop\4_RFID_Readers\4_RFID_Readers.ino:100:15: note: suggested alternative: 'fileno'
Piezo();
^~~~~
fileno
4_RFID_Readers:104:11: error: 'MoreTagsNeeded' was not declared in this scope
MoreTagsNeeded();
^~~~~~~~~~~~~~
4_RFID_Readers:109:9: error: 'UnknownTag' was not declared in this scope
UnknownTag();
^~~~~~~~~~
C:\Users\190240\Desktop\4_RFID_Readers\4_RFID_Readers.ino:109:9: note: suggested alternative: 'AllowTag'
UnknownTag();
^~~~~~~~~~
AllowTag
C:\Users\190240\Desktop\4_RFID_Readers\4_RFID_Readers.ino: At global scope:
4_RFID_Readers:156:3: error: expected initializer before 'Serial'
Serial.println("Congratulations!");
^~~~~~
4_RFID_Readers:157:7: error: expected constructor, destructor, or type conversion before '(' token
tone(piezoPin, 1000, 500);
^
4_RFID_Readers:162:1: error: expected declaration before '}' token
}
^
exit status 1
'Piezo' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
It comes up with Piezo not being declared and I don't know why. Any help would be appreciated!
Cheers!