Guest User

Untitled

a guest
Jun 11th, 2018
38
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.49 KB | None
  1. [code]
  2.  
  3. #include <SD.h> //load the SD library
  4. #include <SPI.h> //load the SPI communication library
  5. #include <RTClib.h>
  6. #include "Wire.h"    // imports the wire library for talking over I2C
  7. #include "Adafruit_PN532.h"  // import the RFID sensor Library
  8.  
  9. #define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)
  10. #define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
  11. uint32_t syncTime = 0;     // time of last sync()
  12.  
  13. #define ECHO_TO_SERIAL   1 // echo data to serial port
  14. #define WAIT_TO_START    0 // Wait for serial input in setup()
  15.  
  16. // the digital pins that connect to the LEDs
  17. #define redLEDpin 4
  18. #define greenLEDpin 5
  19.  
  20. #define PN532_IRQ   (2)  //defines pins connected to the IRQ and reset lines.
  21. #define PN532_RESET (3)
  22.  
  23. Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);;  // create sensor object
  24. RTC_PCF8523 rtc; // define the Real Time Clock object
  25.  
  26. const int chipSelect = 10; //For data logging shield, digital pin 10 used for SD cs line
  27. File logfile; //Variable for working with our file object
  28.  
  29. void error(char *str)
  30. {
  31.   Serial.print("error: ");
  32.   Serial.println(str);
  33.  
  34.   // red LED indicates error
  35.   digitalWrite(redLEDpin, HIGH);
  36.  
  37.   while(1);
  38. }
  39.  
  40.  
  41. void setup(void){
  42.  
  43.   Serial.begin(115200);
  44.   Serial.println("Hello!");
  45.  
  46.   //RFID Setup
  47.  
  48.   nfc.begin();
  49.  
  50.   uint32_t versiondata = nfc.getFirmwareVersion();
  51.   if (! versiondata) {
  52.     Serial.print("Didn't find PN53x board");
  53.     while (1); // halt
  54.   }
  55.   // Got ok data, print it out!
  56.   Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  57.   Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  58.   Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  59.  
  60.   // configure board to read RFID tags
  61.   nfc.SAMConfig();
  62.  
  63.   Serial.println("Waiting for an ISO14443A Card ...");
  64.  
  65.  
  66.   // initialize the SD card
  67.   Serial.print("Initializing SD card...");
  68.   // make sure that the default chip select pin is set to
  69.   // output, even if you don't use it:
  70.   pinMode(10, OUTPUT);
  71.  
  72.   // see if the card is present and can be initialized:
  73.   if (!SD.begin(chipSelect)) {
  74.     error("Card failed, or not present");
  75.   }
  76.   Serial.println("card initialized.");
  77.  
  78.   // create a new file
  79.   char filename[] = "LOGGER00.CSV";
  80.   for (uint8_t i = 0; i < 100; i++) {
  81.     filename[6] = i/10 + '0';
  82.     filename[7] = i%10 + '0';
  83.     if (! SD.exists(filename)) {
  84.       // only open a new file if it doesn't exist
  85.       logfile = SD.open(filename, FILE_WRITE);
  86.       break;  // leave the loop!
  87.     }
  88.   }
  89.  
  90.   if (! logfile) {
  91.     error("couldnt create file");
  92.   }
  93.  
  94.   Serial.print("Logging to: ");
  95.   Serial.println(filename);
  96.   Serial.println("");
  97.  
  98.   // connect to RTC
  99.   Wire.begin();  
  100.   if (!rtc.begin()) {
  101.     logfile.println("RTC failed");
  102. #if ECHO_TO_SERIAL
  103.     Serial.println("RTC failed");
  104. #endif  //ECHO_TO_SERIAL
  105.   }
  106. }
  107. void loop(void)
  108. {
  109.   int motor = 7;
  110.   uint8_t success;
  111.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  112.   uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  113.   int i = 0;
  114.  
  115. //Wait for an ISO14443A type cards (Mifare, etc.)
  116. success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  117.  
  118.   if (success) {
  119.     // Display some basic information about the card
  120.     Serial.println("Found an ISO14443A card");
  121.     Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
  122.     Serial.print("  UID Value: ");
  123.     nfc.PrintHex(uid, uidLength);
  124.     Serial.println("");
  125.  
  126.     if (uidLength == 4)
  127.     {
  128.       // We probably have a Mifare Classic card ...
  129.       //Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
  130.    
  131.       // Now we need to try to authenticate it for read/write access
  132.      // Serial.println("Trying to authenticate block 4 with default KEYA value");
  133.       uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  134.  
  135.       success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
  136.  
  137.       if (success)
  138.       {
  139.         //Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
  140.         uint8_t data[16];
  141.  
  142.                 success = nfc.mifareclassic_ReadDataBlock(4, data);
  143.    
  144.         if (success)
  145.         {
  146.           // Data seems to have been read ... spit it out
  147.           //Serial.println("Reading Block 4:");
  148.           //nfc.PrintHexChar(data, 16);
  149.           //Serial.println("");
  150.      
  151.           // Wait a bit before reading the card again
  152.           delay(1000);
  153.         }
  154.         else
  155.         {
  156.           Serial.println("Ooops ... unable to read the requested block.  Try another key?");
  157.         }
  158.       }
  159.       else
  160.       {
  161.         Serial.println("Ooops ... authentication failed: Try another key?");
  162.       }
  163.     }
  164.  
  165.   {
  166.  
  167.  logfile.println("date, time, IDnumber");    
  168. #if ECHO_TO_SERIAL
  169.   Serial.println("date, time, IDnumber");
  170. #endif //ECHO_TO_SERIAL
  171.    
  172.   DateTime now;
  173.  
  174.   // delay for the amount of time we want between readings
  175.   delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
  176.  
  177.   digitalWrite(greenLEDpin, HIGH);
  178.  
  179.   // log milliseconds since starting
  180.   uint32_t m = millis();
  181.   //logfile.print(m);           // milliseconds since start
  182.   //logfile.print(", ");    
  183. //#if ECHO_TO_SERIAL
  184.   //Serial.print(m);         // milliseconds since start
  185.   //Serial.print(", ");  
  186. //#endif
  187.  
  188.   // fetch the time
  189.   now = rtc.now();
  190.   // log time
  191.   //logfile.print(now.unixtime()); // seconds since 1/1/1970
  192.   //logfile.print(", ");
  193.   //logfile.print('"');
  194.   logfile.print(now.year(), DEC);
  195.   logfile.print("/");
  196.   logfile.print(now.month(), DEC);
  197.   logfile.print("/");
  198.   logfile.print(now.day(), DEC);
  199.   logfile.print(",");
  200. #if ECHO_TO_SERIAL
  201.   Serial.print(now.year(), DEC);
  202.   Serial.print("/");
  203.   Serial.print(now.month(), DEC);
  204.   Serial.print("/");
  205.   Serial.print(now.day(), DEC);
  206.   Serial.print(", ");
  207. #endif //ECHO_TO_SERIAL
  208.  
  209.   logfile.print(now.hour(), DEC);
  210.   logfile.print(":");
  211.   logfile.print(now.minute(), DEC);
  212.   logfile.print(":");
  213.   logfile.print(now.second(), DEC);
  214.   logfile.print(",");
  215. #if ECHO_TO_SERIAL
  216.   //Serial.print(now.unixtime()); // seconds since 1/1/1970
  217.   //Serial.print(", ");
  218.   //Serial.print('"');
  219.   Serial.print(now.hour(), DEC);
  220.   Serial.print(":");
  221.   Serial.print(now.minute(), DEC);
  222.   Serial.print(":");
  223.   Serial.print(now.second(), DEC);
  224. #endif //ECHO_TO_SERIAL
  225.  
  226. for (uint8_t i=0; i < uidLength; i++)
  227. {
  228.     if(uid[i]<0x10);              
  229. }
  230.    
  231. logfile.print(uid[i], HEX);  // Write id number to the SD card
  232. logfile.print(",");
  233. //logfile.close(); //Close the file  
  234. #if ECHO_TO_SERIAL
  235.   Serial.print(", ");
  236.   Serial.print(F(" 0x")); Serial.print(uid[i], HEX);
  237.   //nfc.PrintHex(uid, uidLength);
  238.   Serial.println("");
  239. #endif //ECHO_TO_SERIAL
  240.  
  241.  logfile.println();
  242. #if ECHO_TO_SERIAL
  243.   Serial.println();
  244. #endif // ECHO_TO_SERIAL
  245.  
  246.  
  247.  
  248. if ((millis() - syncTime) < SYNC_INTERVAL) return;
  249.   syncTime = millis();
  250.  
  251.   // blink LED to show we are syncing data to the card & updating FAT!
  252.   digitalWrite(redLEDpin, HIGH);
  253.   logfile.flush();
  254.   digitalWrite(redLEDpin, LOW);
  255.  
  256.   // code for vibrating motor disc
  257.   pinMode(success, INPUT);
  258.   pinMode(motor, OUTPUT);
  259.  
  260.   if(digitalRead(success) == HIGH) { //If RFID card is detected, turn motor on
  261.     digitalWrite(motor, HIGH);
  262.     delay(500);                       //Hold vibration
  263.     digitalWrite(motor, LOW);        //Turn motor off
  264.     }
  265.    }
  266.   }
  267. }
  268. [/code]
RAW Paste Data Copied