示例#1
0
/**
 * Blocking function, waits for and gets the RFID tag.
 */
boolean getRFIDTag() {
  byte next_byte;
  
  if(Serial.available() <= 0) return false;
  
  if((next_byte = Serial.read()) == START_BYTE) {      
    byte bytesread = 0; 
    while(bytesread < CODE_LEN) {
      if(Serial.available() > 0) { //wait for the next byte
          if((next_byte = Serial.read()) == STOP_BYTE) break;
          tag[bytesread++] = next_byte;
          tag[bytesread+1] = '\0';
      }
    }
    boolean codeValid = isCodeValid();
    if(isCodeValid()) return true;
    else {
      clearCode();
      return false;
    }
  } else return false;    
}
void loop() {   //Start our main Arduino Loop
 enableRFID();   //Enable the RFID card
 getRFIDTag();   //Reads the tag
 if(isCodeValid()) {  //Validates that the tag is good
   disableRFID();  //Puts the RFID reader in to low power mode
   sendCode();     //Sends the code read to the serial port
   delay(ITERATION_LENGTH);  //Debounce?
 } 
 else {
   disableRFID();  //Got a incomplete code.. 
   Serial.println("Got some noise");  
 }
 Serial.flush();
 clearCode();
}