One sketch overriding the other

Hello everyone, during this COVID crisis I decided to learn Arduino programing. I am a high school science teacher and have always wanted to create something with robotics, programing, etc. During this past week my day has been consumed by learning as much as I can and I spend 4 hrs in the morning researching and building (I have nothing to do all day) followed by 2 hrs of watching videos on YouTube on coding and projects others have done before going to bed. I was able to combine some projects by tweaking little things here and there but now I am stumped. I have come a long way in my research so yes, I know Google is my friend, but I feel I need guidance from someone. I created an IR door lock opener with a green light that comes on and a buzzer, then the servo will automatically lock the door after 7 seconds. This code works. I then created a different sketch which is a RFID door lock opener with a green light and buzzer that will lock the door after 7 seconds, such as with the IR door lock. A red light and buzzer will turn on if a wrong card is used. This code works as well. I wanted to combine both of these sketches. My goal was to not touch my classroom door handle as much as possible so when someone knocks I can remote unlock the door for them to come in or my students can take the key ring when they need to step out and can come in using the RFID. When I bring up the RFID sketch and add the IR door lock sketch to it, the IR sketch seems to override the RFID one. Only the remote, light and buzzer works. When I use the key ring, the servo, light and buzzer will not work. When I verify the code, there are no problems. Whats odd is that when I add the loop part of the IR sketch at the end of the RFID, nothing works. I changed my 'if' statements to 'else' or 'else if' in various different combinations thinking the program is reading the first part and 'if' it doesn't read the IR sketch then 'else if' the RFID but that has not worked. The <IRremote.h> does not turn orange font like the <SPI.h>
<MFRC522.h> or <Servo.h>. I don't think that's a problem as the IR sketch works on its own as I mentioned. When I combine both sketches and I click on the Serial Monitor it does have my statement that I wrote "Put your card to the reader..." but it will not detect my key ring. Again only the IR remote works. Can anyone see where I may have gone wrong with the code?

Here is the IR sketch:

#include <IRremote.h>
#include <Servo.h>
int IRpin = 11;
IRrecv irrecv(IRpin);
decode_results results;
Servo myservo;
#define BUZZER 2

int LED_G=7;
 
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
myservo.attach(3);

pinMode (LED_G, OUTPUT);
pinMode (BUZZER, OUTPUT);  //BUZZER
}

void loop() {
if(irrecv.decode(&results))
{
      irrecv.resume();
}
if (results.value == 16724175)
{
  delay(500);
  digitalWrite (LED_G, HIGH);
  digitalWrite(BUZZER, HIGH);
  delay(500);
  digitalWrite(BUZZER, LOW);
  myservo.write(0);
  delay(7500);
  myservo.write(100);
  digitalWrite (LED_G, LOW);
}
  
}

HERE IS THE RFID sketch:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>

#define SS_PIN 53 // Uno pin 10 Nano pin D10
#define RST_PIN 5 // Uno pin 9 Nano pin D9
#define SERVO_PIN 3
Servo myservo;
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
#define LED_G 7
#define LED_R 6
#define BUZZER 2
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
myservo.attach(SERVO_PIN);
myservo.write( 0 );
delay(7500);
myservo.write( 100 );
Serial.println("Put your card to the reader...");
Serial.println();

pinMode (LED_G, OUTPUT);
pinMode (LED_R, OUTPUT);
pinMode (BUZZER, OUTPUT);

}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");

String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "5A 77 E2 81") //change here the UID of the card
{
Serial.println("Authorized access");
Serial.println();
delay(500);
digitalWrite(LED_G, HIGH);
tone(BUZZER, 500);
delay(300);
noTone(BUZZER);
myservo.write( 0 );
delay(7500);
myservo.write( 100 );
digitalWrite(LED_G, LOW);
}
else {
Serial.println(" Access denied");
digitalWrite(LED_R, HIGH);
tone(BUZZER, 300);
delay(DENIED_DELAY);
digitalWrite(LED_R, LOW);
noTone(BUZZER);
}
}

HERE IS THE COMBINED SKETCHES:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <IRremote.h>

#define SS_PIN 53 // Uno pin 10 Nano pin D10
#define RST_PIN 5 // Uno pin 9 Nano pin D9
#define SERVO_PIN 3
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
#define LED_G 7
#define LED_R 6
#define BUZZER 2


decode_results results;
Servo myservo;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int IRpin=11;
IRrecv irrecv(IRpin);


void setup()
{
Serial.begin(9600); // Initiate a serial communication
irrecv.enableIRIn();


SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
myservo.attach(SERVO_PIN);

myservo.write( 0 );
delay(7500);
myservo.write( 100 );
Serial.println("Put your card to the reader...");
Serial.println();

pinMode (LED_G, OUTPUT);
pinMode (LED_R, OUTPUT);
pinMode (BUZZER, OUTPUT);

}
void loop()
{
  //start of IR SKETCH

if(irrecv.decode(&results))
{
      irrecv.resume();
}
if (results.value == 16724175)
{
  delay(500);
  digitalWrite (LED_G, HIGH);
  digitalWrite(BUZZER, HIGH);
  delay(500);
  digitalWrite(BUZZER, LOW);
  myservo.write(0);
  delay(7500);
  myservo.write(100);
  digitalWrite (LED_G, LOW);
}

//start of RFID SKETCH
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent());
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");

String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "5A 77 E2 81") //change here the UID of the card
{
Serial.println("Authorized access");
Serial.println();
delay(500);
digitalWrite(LED_G, HIGH);
tone(BUZZER, 500);
delay(300);
noTone(BUZZER);
myservo.write( 0 );
delay(7500);
myservo.write( 100 );
digitalWrite(LED_G, LOW);
}

else {
Serial.println(" Access denied");
digitalWrite(LED_R, HIGH);
tone(BUZZER, 300);
delay(DENIED_DELAY);
digitalWrite(LED_R, LOW);
noTone(BUZZER);
}
}

I bumped your Karma for reading the instructions.

Now, learn to use CTRL-T in the IDE to make your code pretty.

First, what board are you using?
Next, there is no pin 53 on any Arduino board.

#define SS_PIN 53

(I'm surprised this didn't give you a compiler error)

I am pretty sure that the font color of the libraries is determined by the keywords file in the library. If the library writer put the name into the keywords file, it turns the font orange. It's for appearances only. (And to warn the user that you have used a keyword in the library).

If you put some Serial.print statements into your if statements, you will likely find where your problem is.
It's been a while since I wrote code for an RFID card, but if there is no card present, then this ends your loop() function. More specifically, execution goes back to the start of loop().

// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent());
{
return;
}

Make each of the "loop sections" into separate functions. Then in the loop() do something like this:

void loop(){
  checkRFID();
  checkIR();
}

For style, use meaningful variable names. Variables should always start in lower case. like ledGreen instead of LED_G.
Constants should always be upper-case, so when reading the code it's easy to see the difference.
(Bad example on my part since LED_G is technically a constant).

And you need more comments. Why, for instance, are you cycling the servo in startup()?

I can tell that you are a science teacher- you wrote one very large paragraph where three or four would have made more sense.

the commands return do what they are named:

return from the actual_function to the function that called actual_function.
As in your case actual_function is "loop()" there is no function on a higher level which results in return to first line of function loop().

So the commands below

//start of RFID SKETCH
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent());
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}

get never executed.

the author ha chosen this construction with return because then there is only one commandinside the if-condition.
which gives more oversight.

Though if you want to expand the code over the basic example you have to change the logic.
For that reason my opinion about this example is "quick & dirty"

To keep oversight you can define a function on your own.

void MyFunction() {
}

even a name like

void BlaBla () {
}

is possible. But the name should be selfexplaining about what does the code inside the function.

So a name like "Check_Card_Number" would explain what the code does

Another hint: variable type "String" beginning with an upercase "S" defined as global variables eats up all memory over time and makes the code crash.
You defined a string inside loop(). loop() is the most outside-function. So loop() is never left.

Strings inside functions that are called from loop means variable is created when entering the function and destroyed again when leaving the function. In this case variable-String is no problem.

Me personally I prefer a library called PString. PString ensure that memorry gets not overwritten over time like with global Strings.

To analyse what your code is doing you can add serial output at any place

here is a code version with more serial-output

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <IRremote.h>

#define SS_PIN 53 // Uno pin 10 Nano pin D10
#define RST_PIN 5 // Uno pin 9 Nano pin D9
#define SERVO_PIN 3
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
#define LED_G 7
#define LED_R 6
#define BUZZER 2


decode_results results;
Servo myservo;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int IRpin = 11;
IRrecv irrecv(IRpin);


void setup()
{
  Serial.begin(9600); // Initiate a serial communication
  irrecv.enableIRIn();
  Serial.println("irrecv.enableIRIn(); done");


  SPI.begin(); // Initiate SPI bus
  Serial.println("SPI.begin(); done");

  mfrc522.PCD_Init(); // Initiate MFRC522
  Serial.println("mfrc522.PCD_Init(); done");

  myservo.attach(SERVO_PIN);
  Serial.println("myservo.attach(SERVO_PIN); done");

  myservo.write( 0 );
  delay(7500);
  myservo.write( 100 );
  Serial.println("Put your card to the reader...");
  Serial.println();

  pinMode (LED_G, OUTPUT);
  pinMode (LED_R, OUTPUT);
  pinMode (BUZZER, OUTPUT);

}
void loop()
{
  //start of IR SKETCH
  Serial.println("loop() start");

  if (irrecv.decode(&results))
  {
    irrecv.resume();
  }
  if (results.value == 16724175)
  {
    delay(500);
    digitalWrite (LED_G, HIGH);
    digitalWrite(BUZZER, HIGH);
    delay(500);
    digitalWrite(BUZZER, LOW);
    myservo.write(0);
    delay(7500);
    myservo.write(100);
    digitalWrite (LED_G, LOW);
  }

  //start of RFID SKETCH
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent());
  {
    Serial.println("if ( ! mfrc522.PICC_IsNewCardPresent()); is true=> will return;");
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    Serial.println("if ( ! mfrc522.PICC_ReadCardSerial()) is true=> will return;");
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");

  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "5A 77 E2 81") //change here the UID of the card
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    myservo.write( 0 );
    Serial.println("right before delay(7500)");
    delay(7500);
    Serial.println("right after delay(7500)");
    myservo.write( 100 );
    digitalWrite(LED_G, LOW);
  }

  else {
    Serial.println(" Access denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(DENIED_DELAY);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
}

best regards Stefan

StefanL38:
the commands return do what they are named:

return from the actual_function to the function that called actual_function.
As in your case actual_function is "loop()" there is no function on a higher level which results in return to first line of function loop().

loop() is called from main(), but main() is hidden from the IDE user. So when you do a return from inside loop(), you just return execution to main() which shortly calls loop() again.

The actual code looks something like this:

int main(void)
{
 init();
 setup();
 for (;;) {
  loop();
 }
 return 0;
}

Hi Steve,

so does this mean that variables of type String get created and destroyed when defined inside loop()?
And more important if loop() calls another function will the called function have access to the String-variable defined inside loop?

If yes this would be a good solution. If no I willprefer PString or SafeString.
best regards Stefan

@spiderman288888 - Your original post would be much easier to read and understand if it were split into logical paragraphs. As it is I started to read it and soon got lost as to what you had done and what the problems are and I suspect that others will have problems too and move on to another thread

Something like this, perhaps

.
Hello everyone, during this COVID crisis I decided to learn Arduino programing. I am a high school science teacher and have always wanted to create something with robotics, programing, etc.

During this past week my day has been consumed by learning as much as I can and I spend 4 hrs in the morning researching and building (I have nothing to do all day) followed by 2 hrs of watching videos on YouTube on coding and projects others have done before going to bed. I was able to combine some projects by tweaking little things here and there but now I am stumped. I have come a long way in my research so yes, I know Google is my friend, but I feel I need guidance from someone.

I created an IR door lock opener with a green light that comes on and a buzzer, then the servo will automatically lock the door after 7 seconds. This code works. I then created a different sketch which is a RFID door lock opener with a green light and buzzer that will lock the door after 7 seconds, such as with the IR door lock. A red light and buzzer will turn on if a wrong card is used. This code works as well. I wanted to combine both of these sketches.

My goal was to not touch my classroom door handle as much as possible so when someone knocks I can remote unlock the door for them to come in or my students can take the key ring when they need to step out and can come in using the RFID. When I bring up the RFID sketch and add the IR door lock sketch to it, the IR sketch seems to override the RFID one. Only the remote, light and buzzer works.

When I use the key ring, the servo, light and buzzer will not work. When I verify the code, there are no problems. Whats odd is that when I add the loop part of the IR sketch at the end of the RFID, nothing works. I changed my 'if' statements to 'else' or 'else if' in various different combinations thinking the program is reading the first part and 'if' it doesn't read the IR sketch then 'else if' the RFID but that has not worked. The <IRremote.h> does not turn orange font like the <SPI.h> <MFRC522.h> or <Servo.h>.

I don't think that's a problem as the IR sketch works on its own as I mentioned. When I combine both sketches and I click on the Serial Monitor it does have my statement that I wrote "Put your card to the reader..." but it will not detect my key ring. Again only the IR remote works.

Can anyone see where I may have gone wrong with the code?

It takes no more effort to write like this and is so much easier to read and understand

UKHeliBob:
and I suspect that others will have problems too and move on to another thread

This is confirmed.

UKHeliBob:
@spiderman288888 - Your original post would be much easier to read and understand if it were split into logical paragraphs. As it is I started to read it and soon got lost as to what you had done and what the problems are and I suspect that others will have problems too and move on to another thread

Something like this, perhapsIt takes no more effort to write like this and is so much easier to read and understand

He said that he is a science teacher. My wife is a biologist and English structure and grammar are a second language to them. I was an engineer and I had to learn sentence structure when thrown into editing technical documents. Grammatically, the paragraph is correct, though hard to read.

SteveMann:
Next, there is no pin 53 on any Arduino board.

Apart from the ones that do, obviously, like Due or 1280/2560-based

#define SS_PIN 53

(I'm surprised this didn't give you a compiler error)

How does a preprocessor macro throw a compilation error?

Mega has pin 53 - and all the way up to 69 actually (a.k.a A15).

...and in fact, pin 53 is the Slave Select pin on the Mega.

I don't see anything obviously wrong with your code. I suspect that you have a pin conflict.

I would try commenting the IR stuff out bit by bit until the RFID starts working to get a better clue of where the problem lies.

Ah, the good ol' paragraph. Yes, that does make my writing easier to read. I do apologize as we science teachers don't care much for proper writing structures. I am honestly trying to make my work environment a safer place with this, and I do find it a shame when people skip over some threads due to punctuation, grammatical errors or sentence structures. Constructive criticism accepted!

I am using an ELEGOO MEGA2560 for pin53.

I did not know about CTRL-T. Thank you!

The strange part is both sketches work perfectly individually. When I upload the RFID sketch, then add the IR sketch to it, upload it, then completely remove the IR sketch, upload it again (as we are left with the original RFID sketch), the RFID wont work.

Thank you all for your replies.

we science teachers don't care much for proper writing structures.

I am appalled by that. When writing about scientific subjects it is surely important that information is presented in a structured way so that it can be understood by those required to read and interpret it. By not caring you are not doing yourself any favours

Geez UKHeliBob, I agreed with you. Are you okay? I kind of had a feeling you would still find something wrong.
It was a joke. I apologized. I will remember to structure everything like a Scientific Journal from now on.

StefanL38:
Hi Steve,

so does this mean that variables of type String get created and destroyed when defined inside loop()?
And more important if loop() calls another function will the called function have access to the String-variable defined inside loop?

If yes this would be a good solution. If no I willprefer PString or SafeString.
best regards Stefan

Every time loop() is called from main(), any variables defined there are brand new.
No, functions don't inherit the scope of the calling function.

Yes, the problem is this part:

if ( ! mfrc522.PICC_ReadCardSerial())
{
 return;
}

will constantly loop and will not read the bottom part of the RFID sketch. I know doing something like this:

void loop()
if (isTagPresent()==true){
       getTagID();
       checkTagID();

would work but I am having a difficult time figuring out how to do this. I know it deals with what SteveMann put here:

SteveMann:
loop() is called from main(), but main() is hidden from the IDE user. So when you do a return from inside loop(), you just return execution to main() which shortly calls loop() again.

The actual code looks something like this:

int main(void)

{
init();
setup();
for (;:wink: {
 loop();
}
return 0;
}

Anyone have any ideas on how to define my own functions?

I tried doing something like this

void loop()


{

  IRsketch();
  button sketch();
  cardpresent();
  readcard();
  the rest();
  }
  //start of IR SKETCH

void IRsketch();{
  if (irrecv.decode(&results))
  {
    irrecv.resume();
  }
  if (results.value == 16724175)
  {
    delay(500);
    digitalWrite (LED_G, HIGH);  //led turns on
    digitalWrite(BUZZER, HIGH);  //buzzer turns on
    delay(500);                 // for half a second
    digitalWrite(BUZZER, LOW);   //buzzer turns off
    myservo.write( 0 );          //servo goes to 0 degrees
    delay(7500);                   //for 7 and a half seconds
    myservo.write( 100 );          //servo goes to 100 degrees
    digitalWrite (LED_G, LOW);   //led green turns off
  }
}


void button sketch();{
  /* start of pushbutton sketch
    Everytime you press pushbutton it will turn on green led and buzzer, then servo
    will go from 0 to 100 degrees for 7 and a half seconds.  Then the push button
    will go from 1 to 0 state (on/off).*/
  if (digitalRead(pushButtonPin) == LOW) {

    buttonPushed = 1;



  }

  if ( buttonPushed ) {

    // change the angle for next time through the loop:



    digitalWrite (LED_G, HIGH);

    digitalWrite(BUZZER, HIGH);

    delay(500);

    digitalWrite(BUZZER, LOW);



    myservo.write(0);

    delay(7500);

    myservo.write(100);

    digitalWrite (LED_G, LOW);



    buttonPushed = 0;



}
  }


  //start of RFID SKETCH
  // Look for new cards


  

  void cardpresent() {
    if
    ( ! mfrc522.PICC_IsNewCardPresent());
    {
      return;
    }
  }

  void readcard(); {
    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
    {
      return;
    }
  }

  void the rest
  Serial.println("Put your card to the reader...");
  Serial.println();

  //Show UID on serial monitor
  Serial.print("UID tag :");

  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "5A 77 E2 81") //change here the UID of the card


  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    myservo.write( 0 );
    delay(7500);
    myservo.write( 100 );
    digitalWrite(LED_G, LOW);
  }

  else {
    Serial.println(" Access denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(DENIED_DELAY);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }

}

but this part says 'IR sketch was not declared'.

void loop()


{

  IRsketch();
  button sketch();
  cardpresent();
  readcard();
  the rest();
  }]

As far as I understand, I declared it here

void IRsketch();{
  if (irrecv.decode(&results))
  {
    irrecv.resume();
  }
  if (results.value == 16724175)
  {
    delay(500);
    digitalWrite (LED_G, HIGH);  //led turns on
    digitalWrite(BUZZER, HIGH);  //buzzer turns on
    delay(500);                 // for half a second
    digitalWrite(BUZZER, LOW);   //buzzer turns off
    myservo.write( 0 );          //servo goes to 0 degrees
    delay(7500);                   //for 7 and a half seconds
    myservo.write( 100 );          //servo goes to 100 degrees
    digitalWrite (LED_G, LOW);   //led green turns off
  }
}

Some help?

void IRsketch();{Oops

I took off the semicolon. I'm getting an 'Error compiling for board Arduino Mega or Mega 2560.'

void loop()
{
  IRsketch();
  buttonsketch();
  cardpresent();
  readcard();
  therest();
  }

  void IRsketch(){
    //start of IR SKETCH

  if (irrecv.decode(&results))
  {
    irrecv.resume();
  }
  if (results.value == 16724175)
  {
    delay(500);
    digitalWrite (LED_G, HIGH);  //led turns on
    digitalWrite(BUZZER, HIGH);  //buzzer turns on
    delay(500);                 // for half a second
    digitalWrite(BUZZER, LOW);   //buzzer turns off
    myservo.write( 0 );          //servo goes to 0 degrees
    delay(7500);                   //for 7 and a half seconds
    myservo.write( 100 );          //servo goes to 100 degrees
    digitalWrite (LED_G, LOW);   //led green turns off
  }

    }
  
void buttonsketch(){
   /* start of pushbutton sketch
    Everytime you press pushbutton it will turn on green led and buzzer, then servo
    will go from 0 to 100 degrees for 7 and a half seconds.  Then the push button
    will go from 1 to 0 state (on/off).*/
  if (digitalRead(pushButtonPin) == LOW) {
    buttonPushed = 1;
  }
  if ( buttonPushed ) {
    // change the angle for next time through the loop:
    digitalWrite (LED_G, HIGH);
    digitalWrite(BUZZER, HIGH);
    delay(500);
    digitalWrite(BUZZER, LOW);
    myservo.write(0);
    delay(7500);
    myservo.write(100);
    digitalWrite (LED_G, LOW);
    buttonPushed = 0;
  }
  
  }

 void cardpresent(){
  //start of RFID SKETCH
  // Look for new cards
  
  if
  ( ! mfrc522.PICC_IsNewCardPresent());
  {
    return;
  }
  
  }
  
void readcard(){
// Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  
  }

  void therest(){
    Serial.println("Put your card to the reader...");
  Serial.println();

  //Show UID on serial monitor
  Serial.print("UID tag :");

  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "5A 77 E2 81") //change here the UID of the card
    {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    myservo.write( 0 );
    delay(7500);
    myservo.write( 100 );
    digitalWrite(LED_G, LOW);
  }

  else {
    Serial.println(" Access denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(DENIED_DELAY);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }

    
    }

I think this still may be a wrong way to go as the main problem is this part in the code as it keeps 'returning' back to the beginning.

if
  ( ! mfrc522.PICC_IsNewCardPresent());
  {
    return;
  }

even if I do this where I named it 'cardpresent' it will skip 'readcard' and 'therest'

{
  IRsketch();
  buttonsketch();
  cardpresent();
  readcard();
  therest();
  }

I'm trying to make it read this part

if
  ( ! mfrc522.PICC_IsNewCardPresent());
  {
    return;
  }

followed by the next part

if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }

then the last part of the code

  Serial.println("Put your card to the reader...");
  Serial.println();

  //Show UID on serial monitor
  Serial.print("UID tag :");

  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "5A 77 E2 81") //change here the UID of the card
    {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    myservo.write( 0 );
    delay(7500);
    myservo.write( 100 );
    digitalWrite(LED_G, LOW);
  }

  else {
    Serial.println(" Access denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(DENIED_DELAY);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }

Anyone have any ideas how to do that?