ReadBearLabs BLE shield + Adafruit RFID shield problems

I’m having issues getting the RBL BLE shield and the adafruit RFID shield to work nicely together. I want to scan a card, and then tell my phone via the bluetooth shield that a card has been scanned. The issue I’m having is in the loop()… The BLE shield needs the loop to keep running to scan for my phone and connect to it. However, the RFID shield wants to stop the loop() and wait for a card to be present, then let everything continue. I’ve considered using a state machine, but I’m pretty sure I’ll run into the same problem with the RFID shield function getting ‘stuck’ waiting for a card in a given state. Code below.

void loop(void) { 
  
  boolean success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  
//this line stops the loop and waits for a card to be present
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
  
  if (success) {
    Serial.println("Found a card!");
    Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("UID Value: ");
    for (uint8_t i=0; i < uidLength; i++) 
    {
      Serial.print(" 0x");Serial.print(uid[i], HEX); 
    }
    Serial.println("");
    
//print that you've found a card to BLE device (sends to phone)
      ble_write('Found the right Card');

 // Wait 1 second before continuing   
    delay(1000);
  }
  else
  {
    Serial.println("Timed out waiting for a card");
  }

// This is the function that scans for my phone and 'enables' communication
ble_do_events();  
}

Specifically, nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength); wants to stop the loop and wait for a card, and ble_do_events(); needs the loop to keep running to scan and find my phone.

Any help would be appreciated, thanks!

An RTOS should work ,try to run the function in the thread that is loaded by the RTOS and no matter how much delay is there it should work, as if there would be a delay then the RTOS will issue system resources to the other thread that is the main() or loop() in case of our Arduino.

I'm not very well versed in RTOS, could you point me in the right direction? Is this doable in the arduino environment? Thanks!

For anyone who sees this later and is wondering, I resolved the issue by modifying the library for the RFID shield readPassiveTargetID() function to be non blocking (changed a while loop to an if statement so it didn't get stuck).

Good!

I just got back!
Well the RTOS is supported on Arduino various available like the chibiOS, other FreeRTOS like stuff, offcourse you didnt needed all this to solve your problem but I couldn't get that deep in the code so just provided you with what I read in the post.

mtomovich:
For anyone who sees this later and is wondering, I resolved the issue by modifying the library for the RFID shield readPassiveTargetID() function to be non blocking (changed a while loop to an if statement so it didn't get stuck).

Could you post the exact code, how it should look ?
I'm not so good in c, so it would be great help, cause i got same problem.