Advertisement
safwan092

final-working-RFIDAttendanceSystem_Data-Logger_RTC_with_16x2

Dec 10th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.62 KB | None | 0 0
  1. #include <SD.h>
  2. #include <SPI.h>
  3. #include <Wire.h>
  4. #include <MFRC522.h>
  5. #include <LiquidCrystal.h>
  6. #include "RTClib.h"
  7.  
  8. #define SS_PIN 8
  9. #define RST_PIN 9
  10. #define ECHO_TO_SERIAL   1
  11. #define WAIT_TO_START    0
  12. #define LOG_INTERVAL  1000
  13. #define SYNC_INTERVAL 1000
  14.  
  15.  
  16. uint32_t syncTime = 0;
  17. int buttonReading = 1;
  18. const int chipSelect = 10;
  19. MFRC522 mfrc522(SS_PIN, RST_PIN);
  20. LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
  21.  
  22.  
  23.  
  24. String user001ID = "64 EC 6A 85";
  25. boolean user001Status = true;
  26.  
  27.  
  28. String user002ID = "66 51 06 12";
  29. boolean user002Status = true;
  30.  
  31.  
  32. RTC_DS1307 RTC;
  33.  
  34.  
  35. File logfile;
  36.  
  37. void error(char *str)
  38. {
  39.   Serial.print("error: ");
  40.   Serial.println(str);
  41.  
  42.   while (1);
  43. }
  44. ///////////////////////////////////////////////////////////// Setup() Start /////////////
  45. void setup(void)
  46. {
  47.   Serial.begin(9600);
  48.  
  49.   lcd.begin(16, 2);
  50.  
  51.   scanning();
  52.  
  53.   Serial.println();
  54.   SPI.begin();      // Initiate  SPI bus
  55.   mfrc522.PCD_Init();   // Initiate MFRC522
  56.  
  57. #if WAIT_TO_START
  58.   Serial.println("Type any character to start");
  59.   while (!Serial.available());
  60. #endif //WAIT_TO_START
  61.  
  62.   // initialize the SD card
  63.   Serial.print("Initializing SD card...");
  64.   // make sure that the default chip select pin is set to
  65.   // output, even if you don't use it:
  66.   pinMode(10, OUTPUT);
  67.   //  pinMode(buttonPin, INPUT);
  68.   // see if the card is present and can be initialized:
  69.   if (!SD.begin(chipSelect)) {
  70.     error("Card failed, or not present");
  71.   }
  72.   Serial.println("card initialized.");
  73.  
  74.   // create a new file
  75.   char filename[] = "LOGGER00.CSV";
  76.   for (uint8_t i = 0; i < 100; i++) {
  77.     filename[6] = i / 10 + '0';
  78.     filename[7] = i % 10 + '0';
  79.     if (! SD.exists(filename)) {
  80.       // only open a new file if it doesn't exist
  81.       logfile = SD.open(filename, FILE_WRITE);
  82.       break;  // leave the loop!
  83.     }
  84.   }
  85.  
  86.   if (! logfile) {
  87.     error("couldnt create file");
  88.   }
  89.  
  90.   Serial.print("Logging to: ");
  91.   Serial.println(filename);
  92.  
  93.   // connect to RTC
  94.   Wire.begin();
  95.   if (!RTC.begin()) {
  96.     logfile.println("RTC failed");
  97. #if ECHO_TO_SERIAL
  98.     Serial.println("RTC failed");
  99. #endif  //ECHO_TO_SERIAL
  100.   }
  101.  
  102.  
  103.   logfile.println("Date-Time,Status");
  104. #if ECHO_TO_SERIAL
  105.   Serial.println("Date-Time,Status");
  106. #endif //ECHO_TO_SERIAL
  107.  
  108. }
  109. ///////////////////////////////////////////////////////////// Setup END /////////////
  110.  
  111. ///////////////////////////////////////////////////////////// Loop() Start //////////
  112. void loop()
  113. {
  114.   // Look for new cards
  115.   if ( ! mfrc522.PICC_IsNewCardPresent())
  116.   {
  117.     return;
  118.   }
  119.   // Select one of the cards
  120.   if ( ! mfrc522.PICC_ReadCardSerial())
  121.   {
  122.     return;
  123.   }
  124.   //Show UID on serial monitor
  125.   String content = "";
  126.   byte letter;
  127.   for (byte i = 0; i < mfrc522.uid.size; i++)
  128.   {
  129.     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  130.     Serial.print(mfrc522.uid.uidByte[i], HEX);
  131.     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  132.     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  133.   }
  134.   Serial.println();
  135.   content.toUpperCase();
  136.  
  137.   ///////////////////////////////////////////////////////////////////////// User #001 /start
  138.   if (content.substring(1) == user001ID)
  139.   {
  140.     if (user001Status == true) {
  141.       user001Status = false;
  142.       user001IN();
  143.       dateAndTime();
  144.       logUser001IN();
  145.       delay(3000);
  146.       scanning();
  147.     }
  148.     else if (user001Status == false) {
  149.       user001Status = true;
  150.       user001OUT();
  151.       dateAndTime();
  152.       logUser001OUT();
  153.       delay(3000);
  154.       scanning();
  155.     }
  156.   }
  157.   ///////////////////////////////////////////////////////////////////////// User #001 /end
  158.   ///////////////////////////////////////////////////////////////////////// User #002 /start
  159.   if (content.substring(1) == user002ID)
  160.   {
  161.     if (user002Status == true) {
  162.       user002Status = false;
  163.       Serial.println("User 002 Just Signed IN");
  164.       delay(1000);
  165.     }
  166.     else if (user002Status == false) {
  167.       user002Status = true;
  168.       Serial.println("User 002 Just Signed OUT");
  169.       delay(1000);
  170.     }
  171.   }
  172.   ///////////////////////////////////////////////////////////////////////// User #002 /end
  173.  
  174. }
  175. ///////////////////////////////////////////////////////////// Loop() END /////////////////////
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. ///////////////////////////////////////////////////////////////////////// User #001 Show on LCD Screen + Save data in Log file
  185. void user001IN() {
  186.   lcd.setCursor(0, 0);
  187.   lcd.print("(Card Found)    ");
  188.   lcd.setCursor(0, 1);
  189.   lcd.print("User 001 SIGN IN");
  190. }
  191. void user001OUT() {
  192.   lcd.setCursor(0, 0);
  193.   lcd.print("(Card Found)    ");
  194.   lcd.setCursor(0, 1);
  195.   lcd.print("User 001 is OUT");
  196. }
  197. void logUser001IN() {
  198.   logfile.print(", ");
  199.   logfile.print("User 001 Just Signed IN");
  200. #if ECHO_TO_SERIAL
  201.   Serial.print(", ");
  202.   Serial.print("User 001 Just Signed IN");
  203. #endif //ECHO_TO_SERIAL
  204.   logfile.println();
  205. #if ECHO_TO_SERIAL
  206.   Serial.println();
  207. #endif // ECHO_TO_SERIAL
  208.   if ((millis() - syncTime) < SYNC_INTERVAL) return;
  209.   syncTime = millis();
  210.   logfile.flush();
  211. }
  212. void logUser001OUT() {
  213.   logfile.print(", ");
  214.   logfile.print("User 001 Just Signed OUT");
  215. #if ECHO_TO_SERIAL
  216.   Serial.print(", ");
  217.   Serial.print("User 001 Just Signed OUT");
  218. #endif //ECHO_TO_SERIAL
  219.   logfile.println();
  220. #if ECHO_TO_SERIAL
  221.   Serial.println();
  222. #endif // ECHO_TO_SERIAL
  223.   if ((millis() - syncTime) < SYNC_INTERVAL) return;
  224.   syncTime = millis();
  225.   logfile.flush();
  226. }
  227. ///////////////////////////////////////////////////////////////////////// User #001 Show on LCD Screen + Save data in Log file /end
  228.  
  229.  
  230. void scanning() {
  231.  
  232.   lcd.setCursor(0, 0);
  233.   lcd.print("Scanning...     ");
  234.   lcd.setCursor(0, 1);
  235.   lcd.print("                ");
  236.  
  237. }
  238.  
  239. void dateAndTime() {
  240.  
  241.   //Serial.println("User 001 Just Signed OUT");
  242.   DateTime now;
  243.   // fetch the time
  244.   now = RTC.now();
  245.   // log time
  246.   logfile.print('"');
  247.   logfile.print(now.year(), DEC);
  248.   logfile.print("/");
  249.   logfile.print(now.month(), DEC);
  250.   logfile.print("/");
  251.   logfile.print(now.day(), DEC);
  252.   logfile.print(" ");
  253.   logfile.print(now.hour(), DEC);
  254.   logfile.print(":");
  255.   logfile.print(now.minute(), DEC);
  256.   logfile.print(":");
  257.   logfile.print(now.second(), DEC);
  258.   logfile.print('"');
  259. #if ECHO_TO_SERIAL
  260.   Serial.print('"');
  261.   Serial.print(now.year(), DEC);
  262.   Serial.print("/");
  263.   Serial.print(now.month(), DEC);
  264.   Serial.print("/");
  265.   Serial.print(now.day(), DEC);
  266.   Serial.print(" ");
  267.   Serial.print(now.hour(), DEC);
  268.   Serial.print(":");
  269.   Serial.print(now.minute(), DEC);
  270.   Serial.print(":");
  271.   Serial.print(now.second(), DEC);
  272.   Serial.print('"');
  273. #endif //ECHO_TO_SERIAL
  274.  
  275.   delay(10);
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement