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);
}
}