Is there a way for me to pause my code in the middle of the two if statements so I can scan the second RFID so the code can continue on to the second if statement with the information from the second RFID? I want to scan two RFIDs within the duration of the code.
// Using Arduino Uno and RFIDs RC522
#include <require_cpp11.h>
#include <MFRC522.h>
#include <deprecated.h>
#include <MFRC522Extended.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int count = 0;
int greenLED = 5;
int redLED = 6;
int yellowLED = 7;
void setup()
{
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
}
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();
int count = 0;
if (content.substring(1) == "89 E7 75 47") //change here the UID of the card/cards that you want to give access
{
Serial.println("Authorized access");
Serial.println();
digitalWrite(yellowLED, HIGH);
delay(1000);
digitalWrite(yellowLED, LOW);
count = count + 1;
}
else {
Serial.println(" Access denied");
Serial.println();
digitalWrite(redLED, HIGH);
delay(1000);
digitalWrite(redLED, LOW);
if (content.substring(2) == "F9 9A 76 47")
{
Serial.println("Authorized access");
Serial.println();
digitalWrite(yellowLED, HIGH);
delay(1000);
digitalWrite(yellowLED, LOW);
count = count + 1;
}
else {
Serial.println(" Access denied");
Serial.println();
digitalWrite(redLED, HIGH);
delay(1000);
digitalWrite(redLED, LOW);
}
if (count == 2 ) {
Serial.println(" Done");
Serial.println();
digitalWrite(greenLED, HIGH);
delay(3000);
digitalWrite(greenLED, LOW);
}
}
}
You can use a variable to keep track what needs to be done. The basic framework below.
void loop()
{
static byte state = 1;
switch (state)
{
case 1:
// read card 1
...
...
// indicate that we now can read card 2
state = 2;
break;
case 2:
// read card 1
...
...
// indicate that both cards are read
state = 3;
break;
case 3:
// do whatever you want to do when both cards are read
...
...
// indicate that we start all over again
state = 1;
break;
}
}
Is there a way for me to pause my code in the middle of the two if statements so I can scan the second RFID so the code can continue on to the second if statement with the information from the second RFID?
Here is the first if statement:
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
It says "If there is not a card present, forget it. Loop isn't doing anything".
Here is the second if statement:
if ( ! mfrc522.PICC_ReadCardSerial()) {
It tries to read the card that is present, and if not successful, it, too, tells loop to forget trying to do anything.
I've told you that you need to re-write that piss-poor example code. But, you refuse to do that.
So, there is no way in hell that your code will EVER work the way you want, no matter how many more threads you start.