Hi,
I'm trying to save some cpu cycles here! I'm using the pretty much unmodified rfid lock sketch, except I've added code for a LED diode to show if it's armed or disarmed. Since this puppy will only be used when you leave or enter the house, 99% of the time it could sleep. Here is the loop:
void loop() {
gw.process(); // Process incomming messages
boolean success;
uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t currentKeyLength; // 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)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], ¤tKeyLength);
if (success) {
Serial.print("Found tag id: ");
for (uint8_t i=0; i < currentKeyLength; i++)
{
if (i>0) Serial.print(",");
Serial.print("0x");Serial.print(key[i], HEX);
}
for (uint8_t i=currentKeyLength; i < maxKeyLength; i++)
{
Serial.print(",0x00");
}
Serial.println("");
boolean valid = false;
// Compare this key to the valid once registered here in sketch
for (int i=0;i<keyCount && !valid;i++) {
for (int j=0;i<currentKeyLength && !valid;j++) {
if (key[j] != validKeys[i][j]) {
break;
}
if (j==currentKeyLength-1) {
valid = true;
}
}
}
if (valid) {
// Switch lock status
setLockState(!lockStatus, true);
}
// Wait for card/tag to leave reader
while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], ¤tKeyLength));
}
gw.sleep(2000);
}
Using this loop the arduino works great when using the rfid-tag to trigger a change in armed / disarmed, but the arduino does NOT respond to commands from my Vera (UI7).
I think I know what the problem is - basically the Arduino is sleeping when the command from the controller arrives, and thus it can't process it. Changing the last bit of code like this (moving the gw.sleep(2000) only to trigger if an rfid-tag has been read works:
// Wait for card/tag to leave reader
while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], ¤tKeyLength));
gw.sleep(2000);
}
}
But then again, using this code it doesn't sleep at all except for the 2 seconds 2 times a day when I trigger it.
So, how could I have the best of both worlds? I want to let it sleep until 1) there is an incoming command from my Vera or 2) an rfid-tag is recognized. Is it possible?
As always, thanks in advance for any input!