Merging two codes..RFID read and beep if id is authorized/unauthorized

Hi Everyone,

I am trying to make an RFID lock for a cabinet but for the meantime, I would like to make it say "Authenticated" for authorized tags and copy the tag's serial number if unauthorized.
After googling how to do it, I stumbled upon this code:

#include <MFRC522.h>
//Get library from GitHub - ljos/MFRC522: Arduino RFID Library for MFRC522
//Sketch: gudjonholm@gmail.com
#include <SPI.h>

/*
Pins SPI UNO Mega2560 Leonardo
1 (NSS) SAD (SS) 10 53 10
2 SCK 13 52 SCK1
3 MOSI 11 51 MOSI1
4 MISO 12 50 MISO1
5 IRQ * * *
6 GND GND GND GND
7 RST 5 ? Reset
8 +3.3V (VCC) 3V3 3V3 3.3V

  • Not needed
    1 on ICPS header
    */
    #define SAD 10
    #define RST 5
    MFRC522 nfc(SAD, RST);

#define ledPinOpen 2
#define ledPinClosed 3

void setup() {
pinMode(ledPinOpen , OUTPUT);
pinMode(ledPinClosed, OUTPUT);
SPI.begin();
Serial.begin(115200);
Serial.println("Looking for MFRC522.");
nfc.begin();
byte version = nfc.getFirmwareVersion();
if (! version) {
Serial.print("Didn't find MFRC522 board.");
while(1); //halt
}

Serial.print("Found chip MFRC522 ");
Serial.print("Firmware ver. 0x");
Serial.print(version, HEX);
Serial.println(".");
}

#define AUTHORIZED_COUNT 1 /If you want more Authorized of cards set the count here, and then add the serials below/
byte Authorized[AUTHORIZED_COUNT][6] = {

{0x45, 0x14, 0x39, 0x2E, 0xFF, 0xFF, }
/,{0x10, 0x14, 0x39, 0x2E, 0xFF, 0xFF, }/ /f. example how to add more authorized cards/

};

void printSerial(byte *serial);
boolean isSame(byte *key, byte *serial);
boolean isAuthorized(byte *serial);

void loop() {
byte status;
byte data[MAX_LEN];
byte serial[5];
boolean Opening = false;
digitalWrite(ledPinOpen, Opening);
digitalWrite(ledPinClosed, !Opening);
status = nfc.requestTag(MF1_REQIDL, data);

if (status == MI_OK) {
status = nfc.antiCollision(data);
memcpy(serial, data, 5);

if(isAuthorized(serial))
{
Serial.println("Authenticated");
Opening = true;
}
else
{
printSerial(serial);
Serial.println("is NOT authenticated");
Opening = false;
}

nfc.haltTag();
digitalWrite(ledPinOpen, Opening);
digitalWrite(ledPinClosed, !Opening);
delay(2000);
}//if (status == MI_OK)

delay(500);

}//void loop()

boolean isSame(byte key, byte serial)
{
for (int i = 0; i < 4; i++) {
if (key _!= serial
)
_
* {*
* return false;*
* }*
* }*

* return true;*
}
boolean isAuthorized(byte *serial)
{
* for(int i = 0; i<AUTHORIZED_COUNT; i++)
_
{_
_ if(isSame(serial, Authorized))
return true;
}
return false;
}
void printSerial(byte *serial)
{
Serial.print("Serial:");
for (int i = 0; i < 4; i++) {
Serial.print(serial, HEX);
Serial.print(" ");
}
}
What it does is it reads RFID tags and cards and if a tag/card is authorized, it would say
"Authenticated" and on the other hand, copy the unauthorized tag's serial and say "Serial:XX 31 XX 53 is NOT authenticated"
I would like to add a beep when a tag is authorized and 3 beeps if tag is not authorized.
I googled for a simple beep code and this is what I came up with:
/

Piezo
This example shows how to run a Piezo Buzzer on pin 9
using the analogWrite() function.
It beeps 3 times fast at startup, waits a second then beeps continuously
at a slower pace
/
void setup() {
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
beep(25);
beep(50);
beep(100);
delay(1000);
}
void loop() {
beep(200);
}
void beep(unsigned char delayms){
analogWrite(9, 20); // Almost any value can be used except 0 and 255*

* // experiment to get the best tone*
* delay(delayms); // wait for a delayms ms*
* analogWrite(9, 0); // 0 turns it off*
* delay(delayms); // wait for a delayms ms
}*

Both codes work individually when uploaded but I have no idea how to merge them together.
Any help is appreciated. :slight_smile:
I also read this post: Demonstration code for several things at the same time - Project Guidance - Arduino Forum but I could not understand it.._

Welcome but please use code tags to post code. Have you tried to combine two code together? Post your attempt. Beep code is extremely simple. If you can't combine them, maybe you should try some simpler tutorials first.

#include <MFRC522.h>
//Get library from https://github.com/ljos/MFRC522
//Sketch: gudjonholm@gmail.com
#include <SPI.h>

/*
Pins	SPI	   UNO  Mega2560  Leonardo
1 (NSS) SAD (SS)   10     53        10
2       SCK        13     52        SCK1
3       MOSI       11     51        MOSI1
4       MISO       12     50        MISO1
5       IRQ        *      *         *
6       GND       GND     GND       GND
7       RST        5      ?         Reset
8      +3.3V (VCC) 3V3     3V3       3.3V
* Not needed
1 on ICPS header
*/
#define SAD 10
#define RST 5
MFRC522 nfc(SAD, RST);


#define ledPinOpen    2
#define ledPinClosed  3


void setup() {
  pinMode(ledPinOpen  , OUTPUT);   
  pinMode(ledPinClosed, OUTPUT);   
  SPI.begin();
  Serial.begin(115200);
  Serial.println("Looking for MFRC522.");
  nfc.begin();
  byte version = nfc.getFirmwareVersion();
  if (! version) {
    Serial.print("Didn't find MFRC522 board.");
    while(1); //halt
  }
    {pinMode(9, OUTPUT);
  }
  Serial.print("Found chip MFRC522 ");
  Serial.print("Firmware ver. 0x");
  Serial.print(version, HEX);
  Serial.println(".");
}

#define AUTHORIZED_COUNT 1 /*If you want more Authorized of cards set the count here, and then add the serials below*/
byte Authorized[AUTHORIZED_COUNT][6] = {

                            {0x45, 0x14, 0x39, 0x2E, 0xFF, 0xFF, }
                           /*,{0x10, 0x14, 0x39, 0x2E, 0xFF, 0xFF, }*/  /*f. example how to add more authorized cards*/

                          };


                        
                          
void printSerial(byte *serial);
boolean isSame(byte *key, byte *serial);
boolean isAuthorized(byte *serial);



void loop() {
  byte status;
  byte data[MAX_LEN];
  byte serial[5];
  boolean Opening = false;
  digitalWrite(ledPinOpen, Opening);
  digitalWrite(ledPinClosed, !Opening);
  status = nfc.requestTag(MF1_REQIDL, data);

  if (status == MI_OK) {
    status = nfc.antiCollision(data);
    memcpy(serial, data, 5);
    
    if(isAuthorized(serial))
    { 
      Serial.println("Authenticated");
      Opening = true;
    }
    else
    { 
      printSerial(serial);
      Serial.println("is NOT authenticated");
      Opening = false;
    }
    
    nfc.haltTag();
    digitalWrite(ledPinOpen, Opening);
    digitalWrite(ledPinClosed, !Opening);
    delay(2000);
  }//if (status == MI_OK)

  delay(500);

}//void loop()

boolean isSame(byte *key, byte *serial)
{
    for (int i = 0; i < 4; i++) {
      if (key[i] != serial[i])
      { 
        return false; 
      }
    }
    
    return true;

}

boolean isAuthorized(byte *serial)
{
    for(int i = 0; i<AUTHORIZED_COUNT; i++)
    {
      if(isSame(serial, Authorized[i]))
        return true;
    }
   return false;
}

void printSerial(byte *serial)
{
        Serial.print("Serial:");
    for (int i = 0; i < 4; i++) {
      Serial.print(serial[i], HEX);
      Serial.print(" ");
    }
}

I tried inserting these lines after void loop but it says a function-definition is not allowed here before '{' token appears

 { 
  beep(200); 
}

void beep(unsigned char delayms){
  analogWrite(9, 20);      // Almost any value can be used except 0 and 255
                           // experiment to get the best tone
  delay(delayms);          // wait for a delayms ms
  analogWrite(9, 0);       // 0 turns it off
  delay(delayms);          // wait for a delayms ms   
}

@liudr: If you could show me where I should combine them, it would be deeply appreciated. :slight_smile:

Robin2 created this thread to explain how to merge 2 codes.

You can't put that code that calls the beep, after loop(). The beep function itself may go below, but the line that calls it must be in loop() (or setup()).

It would be a good idea right now to pretend delay() doesn't exist and start with the habit of using the BlinkWithoutDelay approach. It's a bit to get your head round, but well worth the effort as you'll find it useful for all sorts of things not just flashing leds.