By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

How can I get my code to wait for me to scan my second RFID?

P: 3
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?
Expand|Select|Wrap|Line Numbers
  1. // Using Arduino Uno and RFIDs RC522
  2. #include <require_cpp11.h>
  3. #include <MFRC522.h>
  4. #include <deprecated.h>
  5. #include <MFRC522Extended.h>
  6.  
  7. #include <SPI.h>
  8. #include <MFRC522.h>
  9. #define SS_PIN 10
  10. #define RST_PIN 9
  11. MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
  12. int count = 0; 
  13. int greenLED = 5;
  14. int redLED = 6;
  15. int yellowLED = 7;
  16.  
  17. void setup()
  18. {
  19.   pinMode(greenLED, OUTPUT);
  20.   pinMode(redLED, OUTPUT);
  21.   pinMode(yellowLED, OUTPUT);
  22.  
  23.  
  24.   Serial.begin(9600);   // Initiate a serial communication
  25.   SPI.begin();      // Initiate  SPI bus
  26.   mfrc522.PCD_Init();   // Initiate MFRC522
  27.   Serial.println("Approximate your card to the reader...");
  28.   Serial.println();
  29.  
  30. }
  31. void loop()
  32. {
  33.   // Look for new cards
  34.   if ( ! mfrc522.PICC_IsNewCardPresent())
  35.   {
  36.     return;
  37.   }
  38.   // Select one of the cards
  39.   if ( ! mfrc522.PICC_ReadCardSerial()) {
  40.  
  41.     return;
  42.   }
  43.   //Show UID on serial monitor
  44.  
  45.   Serial.print("UID tag :");
  46.   String content= "";
  47.   byte letter;
  48.   for (byte i = 0; i < mfrc522.uid.size; i++) {
  49.  
  50.  
  51.      Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  52.      Serial.print(mfrc522.uid.uidByte[i], HEX);
  53.      content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  54.      content.concat(String(mfrc522.uid.uidByte[i], HEX));
  55.  
  56.   }
  57.   Serial.println();
  58.   //Serial.print("Message : ");
  59.   content.toUpperCase();
  60.   int count = 0;
  61.  
  62.   if (content.substring(1) == "89 E7 75 47") //change here the UID of the card/cards that you want to give access
  63.   {
  64.     Serial.println("Authorized access"); 
  65.     Serial.println();
  66.     digitalWrite(yellowLED, HIGH);
  67.     delay(1000);
  68.     digitalWrite(yellowLED, LOW);
  69.  
  70.   count = count + 1;
  71.   }
  72.  
  73.  else   {
  74.     Serial.println(" Access denied");
  75.     Serial.println();
  76.     digitalWrite(redLED, HIGH);
  77.     delay(1000);
  78.     digitalWrite(redLED, LOW);
  79.  
  80.  
  81. if (content.substring(2) == "F9 9A 76 47")
  82.   {
  83.     Serial.println("Authorized access"); 
  84.     Serial.println();
  85.     digitalWrite(yellowLED, HIGH);
  86.     delay(1000);
  87.     digitalWrite(yellowLED, LOW); 
  88.     count = count + 1;
  89.   }
  90.  else   {
  91.     Serial.println(" Access denied");
  92.     Serial.println();
  93.     digitalWrite(redLED, HIGH);
  94.     delay(1000);
  95.     digitalWrite(redLED, LOW);
  96.   }
  97.  
  98. if (count == 2 ) {
  99.     Serial.println(" Done");
  100.     Serial.println();
  101.     digitalWrite(greenLED, HIGH);
  102.     delay(3000);
  103.     digitalWrite(greenLED, LOW);
  104.  
  105. }
  106.  
  107. }
  108. }
  109.  
1 Week Ago #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,026
Is that not solved by a 'standard' while statement?

Expand|Select|Wrap|Line Numbers
  1. do
  2. {
  3.   //  whatever
  4. } while ( !condition );
  5.  
The condition is, in this case, 'scan second RFID succeeded'.
1 Week Ago #2

P: 60
I am not sure what you mean by "pause the code". The execution will always be stopped on using any function that requires user attention like inputs etc.

How is the "scan the second RFID" thing done? If that is something provided by the user, just insert the appropriate function between the two ifs. If that's something returned by a library function, just call that function after the first if. The program execution is sequential. Since you want the info of the second RFID before the second if, just make a way to fetch that info before going to the second if.

And then the code can continue on to the second if statement with the information of the second RFID.
1 Week Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.