RC522 RFID Reader waiting for TAG, when I need Main Loop to run constantly

Discussion in 'Programmer's Corner' started by combover61, Jan 27, 2016.

  1. combover61

    Thread Starter New Member

    Oct 16, 2015
    18
    0
    Main Loop worked great.

    RFID read, write and display worked great.

    Combined the two, and the RFID is waiting for a TAG to be read ... preventing the Main Loop from running constantly.

    How can I have the RFID reader constantly available for new TAGs, yet keep my Main Loop running constantly?

    Code below, RFID library that is called is attached:
    Code (C):
    1.  
    2. void loop() {
    3.  
    4.     if ( ! mfrc522.PICC_IsNewCardPresent()) {           // Look for new RFID cards
    5.  
    6.         return; }
    7.  
    8.     if ( ! mfrc522.PICC_ReadCardSerial()) {             // Select one of the RFID cards
    9.  
    10.         return; }
    11.  
    12.     mfrc522.PICC_DumpToSerial(&(mfrc522.uid));          // Dump debug info about the card. PICC_HaltA() is automatically called.
    13.  
    14.   if (mfrc522.uid.uidByte[0] == 0x61) {                 //
    15.  
    16.     Serial.println(“blue”);                      
    17.  
    18.   } else {
    19.  
    20.     Serial.println(“card”);
    21.  
    22.   }
    23.  
    24.   Serial.println();                                     // Go to next line
    25.  
    26.  
    27.   // read the state of the vibration sensor latch
    28.  
    29.   HITState = digitalRead(HITPin);     // vibration sensor latched ON by debounce circuitry?
    30.  
    31.                                       // if it is, the HITState is HIGH
    32.  
    33.   if (HITState == HIGH) {      
    34.  
    35.     digitalWrite(CLEARHITPin, HIGH);  // reset debounce circuitry
    36.  
    37.     delay(500);                       // wait for 1/2 second
    38.  
    39.     digitalWrite(CLEARHITPin, LOW);   // complete reset of debounce circuitry
    40.  
    41.  
    42.  
    43.   LEDS = LEDS-1;           // set LEDS variable for number of LEDs to light
    44.  
    45.  
    46.  
    47.   switch (LEDS) {          
    48.  
    49.     case 0:  
    50.  
    51.       analogWrite(BARGRAPHPin, 00);   // 0  LEDs ON
    52.  
    53.       break;
    54.  
    55.     case 1:  
    56.  
    57.       analogWrite(BARGRAPHPin, 13);   // 1  LED  ON    
    58.  
    59.       break;
    60.  
    61.     }
    62.  
    63.   } else {
    64.  
    65.   }
    66.  
    67.     delay(50);                       // wait for 0.05 second
    68.  
    69. }
    Mod edit: Added code tags
     
    Last edited by a moderator: Jan 28, 2016
  2. JWHassler

    Active Member

    Sep 25, 2013
    262
    47
    Name your platform and use code tags
     
  3. Picbuster

    Well-Known Member

    Dec 2, 2013
    818
    101
    Read RFID under interrupt. the main loop will run until it 'sees' a rfid received flag.
    takes you 10 min to change your program.
     
  4. MrSoftware

    Senior Member

    Oct 29, 2013
    1,133
    333
    Using interrupts is a great idea, and alternatively there might be a way to make a non-blocking read call to your device. Perhaps poll the device to see if data is waiting. You would have to see the docs for the device or its driver/library to determine this.
     
Loading...