Advertisement
bangnaga

Arduino RFID

May 19th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. // Modified by Worapoht K.
  2. #include <SoftwareSerial.h>
  3.  
  4. int  val = 0;
  5. char code[10];
  6. int bytesread = 0;
  7.  
  8. #define rxPin 8
  9. #define txPin 9
  10. // RFID reader SOUT pin connected to Serial RX pin at 2400bps to pin8
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(2400);  // Hardware serial for Monitor 2400bps
  15.  
  16.   pinMode(2,OUTPUT);       // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
  17.   digitalWrite(2, LOW);    // Activate the RFID reader
  18. }
  19.  
  20.  
  21. void loop()
  22. {
  23.   SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
  24.   RFID.begin(2400);
  25.  
  26.   if((val = RFID.read()) == 10)
  27.   {   // check for header
  28.     bytesread = 0;
  29.     while(bytesread<10)
  30.     {  // read 10 digit code
  31.       val = RFID.read();
  32.       if((val == 10)||(val == 13))
  33.       {  // if header or stop bytes before the 10 digit reading
  34.         break;                       // stop reading
  35.       }
  36.       code[bytesread] = val;         // add the digit          
  37.       bytesread++;                   // ready to read next digit  
  38.     }
  39.  
  40.     if(bytesread == 10)
  41.     {  // if 10 digit read is complete
  42.       Serial.print("TAG code is: ");   // possibly a good TAG
  43.       Serial.println(code);            // print the TAG code
  44.     }
  45.     bytesread = 0;
  46.     delay(500);                       // wait for a second
  47.   }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement