KRITSADA

Data Logger

Feb 24th, 2021
643
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h> //
  2. #include <SD.h> //
  3. #include <Wire.h> //
  4. #include "RTClib.h" //
  5. #include <MFRC522.h> //
  6. #include <LiquidCrystal_I2C.h>
  7.  
  8. LiquidCrystal_I2C lcd(0x27,16,2);
  9. #define SS_PIN 8 // 10 for shield // 8 for normal small module
  10. #define RST_PIN 9 // FOR RFID READER
  11. MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
  12.  
  13.  
  14. //-------------------------------------------------------------------------USER # 1
  15. String user001ID = "09 2A 13 A3";
  16. boolean user001Status = true;
  17. //-------------------------------------------------------------------------
  18.  
  19. //-------------------------------------------------------------------------USER # 2
  20. String user002ID = "D4 A0 30 2A";
  21. boolean user002Status = true;
  22. //-------------------------------------------------------------------------
  23.  
  24.  
  25. // A simple data logger for the Arduino analog pins
  26.  
  27. // how many milliseconds between grabbing data and logging it. 1000 ms is once a second
  28. #define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)
  29.  
  30. // how many milliseconds before writing the logged data permanently to disk
  31. // set it to the LOG_INTERVAL to write each time (safest)
  32. // set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
  33. // the last 10 reads if power is lost but it uses less power and is much faster!
  34. #define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
  35. uint32_t syncTime = 0; // time of last sync()
  36. int buttonReading = 1;
  37. #define ECHO_TO_SERIAL   1 // echo data to serial port
  38. #define WAIT_TO_START    0 // Wait for serial input in setup()
  39.  
  40.  
  41. RTC_DS1307 RTC; // define the Real Time Clock object
  42.  
  43. // for the data logging shield, we use digital pin 10 for the SD cs line
  44. const int chipSelect = 10;
  45.  
  46. // the logging file
  47. File logfile;
  48.  
  49. void error(char *str)
  50. {
  51.   Serial.print("error: ");
  52.   Serial.println(str);
  53.  
  54.   while (1);
  55. }
  56.  
  57. void setup(void)
  58. {
  59.   lcd.init();
  60.   lcd.init();
  61.   lcd.backlight();
  62.   lcd.print("Connecting...");
  63.   Serial.begin(9600);
  64.   Serial.println();
  65.   SPI.begin();      // Initiate  SPI bus
  66.   mfrc522.PCD_Init();   // Initiate MFRC522
  67.   pinMode(7, OUTPUT);
  68. #if WAIT_TO_START
  69.   Serial.println("Type any character to start");
  70.   while (!Serial.available());
  71. #endif //WAIT_TO_START
  72.  
  73.   // initialize the SD card
  74.   Serial.print("Initializing SD card...");
  75.   // make sure that the default chip select pin is set to
  76.   // output, even if you don't use it:
  77.   pinMode(10, OUTPUT);
  78.   //  pinMode(buttonPin, INPUT);
  79.   // see if the card is present and can be initialized:
  80.   if (!SD.begin(chipSelect)) {
  81.     error("Card failed, or not present");
  82.   }
  83.   Serial.println("card initialized.");
  84.  
  85.   // create a new file
  86.   char filename[] = "LOGGER00.CSV";
  87.   for (uint8_t i = 0; i < 100; i++) {
  88.     filename[6] = i / 10 + '0';
  89.     filename[7] = i % 10 + '0';
  90.     if (! SD.exists(filename)) {
  91.       // only open a new file if it doesn't exist
  92.       logfile = SD.open(filename, FILE_WRITE);
  93.       break;  // leave the loop!
  94.     }
  95.   }
  96.  
  97.   if (! logfile) {
  98.     error("couldnt create file");
  99.   }
  100.  
  101.   Serial.print("Logging to: ");
  102.   Serial.println(filename);
  103.  
  104.   // connect to RTC
  105.   Wire.begin();
  106.   if (!RTC.begin()) {
  107.     logfile.println("RTC failed");
  108. #if ECHO_TO_SERIAL
  109.     Serial.println("RTC failed");
  110. #endif  //ECHO_TO_SERIAL
  111.   }
  112.  
  113.  
  114.   logfile.println("Date,Time,Log");
  115. #if ECHO_TO_SERIAL
  116.   Serial.println("Date-Time,Status");
  117. #endif //ECHO_TO_SERIAL
  118.  
  119.   delay(2000);
  120.   lcd.clear();
  121.   lcd.print("System Ready");
  122.   delay(2500);
  123.   lcd.clear();
  124.   lcd.setCursor(0, 0);
  125.   lcd.print("Log:");
  126. }
  127.  
  128. void loop()
  129. {
  130.   // Look for new cards
  131.   if ( ! mfrc522.PICC_IsNewCardPresent())
  132.   {
  133.     return;
  134.   }
  135.   // Select one of the cards
  136.   if ( ! mfrc522.PICC_ReadCardSerial())
  137.   {
  138.     return;
  139.   }
  140.   //Show UID on serial monitor
  141.   String content = "";
  142.   byte letter;
  143.   buzz();
  144.   for (byte i = 0; i < mfrc522.uid.size; i++)
  145.   {
  146.     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  147.     Serial.print(mfrc522.uid.uidByte[i], HEX);
  148.     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  149.     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  150.   }
  151.   Serial.println();
  152.   content.toUpperCase();
  153.  
  154.   ///////////////////////////////////////////////////////////////////////// User #001 /start
  155.   if (content.substring(1) == user001ID)
  156.   {
  157.     if (user001Status == true) {
  158.       user001Status = false;
  159.       //Serial.println("Shahad Just Signed IN");
  160.       DateTime now;
  161.       // fetch the time
  162.       now = RTC.now();
  163.       // log time
  164.       logfile.print('"');
  165.       logfile.print(now.year(), DEC);
  166.       logfile.print("/");
  167.       logfile.print(now.month(), DEC);
  168.       logfile.print("/");
  169.       logfile.print(now.day(), DEC);
  170.       logfile.print('"');
  171.       logfile.print(",");
  172.       logfile.print(now.hour(), DEC);
  173.       logfile.print(":");
  174.       logfile.print(now.minute(), DEC);
  175.       logfile.print(":");
  176.       logfile.print(now.second(), DEC);
  177. #if ECHO_TO_SERIAL
  178.       Serial.print('"');
  179.       Serial.print(now.year(), DEC);
  180.       Serial.print("/");
  181.       Serial.print(now.month(), DEC);
  182.       Serial.print("/");
  183.       Serial.print(now.day(), DEC);
  184.       Serial.print(" ");
  185.       Serial.print(now.hour(), DEC);
  186.       Serial.print(":");
  187.       Serial.print(now.minute(), DEC);
  188.       Serial.print(":");
  189.       Serial.print(now.second(), DEC);
  190.       Serial.print('"');
  191. #endif //ECHO_TO_SERIAL
  192.  
  193.       delay(10);
  194.  
  195.       logfile.print(", ");
  196.       logfile.print("Shahad Just Signed IN");
  197. #if ECHO_TO_SERIAL
  198.       Serial.print(", ");
  199.       Serial.print("Shahad Just Signed IN");
  200.       lcd.setCursor(0, 1);
  201.       lcd.print("Shahad-Sign IN");
  202.       delay(3000);
  203.       lcd.setCursor(0, 1);
  204.       lcd.print("                ");
  205. #endif //ECHO_TO_SERIAL
  206.       logfile.println();
  207. #if ECHO_TO_SERIAL
  208.       Serial.println();
  209. #endif // ECHO_TO_SERIAL
  210.       // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  211.       // which uses a bunch of power and takes time
  212.       if ((millis() - syncTime) < SYNC_INTERVAL) return;
  213.       syncTime = millis();
  214.       // blink LED to show we are syncing data to the card & updating FAT!
  215.       logfile.flush();
  216.       delay(1000);
  217.     }
  218.     else if (user001Status == false) {
  219.       user001Status = true;
  220.       //Serial.println("User 001 Just Signed OUT");
  221.       DateTime now;
  222.       // fetch the time
  223.       now = RTC.now();
  224.       // log time
  225.       logfile.print('"');
  226.       logfile.print(now.year(), DEC);
  227.       logfile.print("/");
  228.       logfile.print(now.month(), DEC);
  229.       logfile.print("/");
  230.       logfile.print(now.day(), DEC);
  231.       logfile.print('"');
  232.       logfile.print(",");
  233.       logfile.print(now.hour(), DEC);
  234.       logfile.print(":");
  235.       logfile.print(now.minute(), DEC);
  236.       logfile.print(":");
  237.       logfile.print(now.second(), DEC);
  238. #if ECHO_TO_SERIAL
  239.       Serial.print('"');
  240.       Serial.print(now.year(), DEC);
  241.       Serial.print("/");
  242.       Serial.print(now.month(), DEC);
  243.       Serial.print("/");
  244.       Serial.print(now.day(), DEC);
  245.       Serial.print(" ");
  246.       Serial.print(now.hour(), DEC);
  247.       Serial.print(":");
  248.       Serial.print(now.minute(), DEC);
  249.       Serial.print(":");
  250.       Serial.print(now.second(), DEC);
  251.       Serial.print('"');
  252. #endif //ECHO_TO_SERIAL
  253.  
  254.       delay(10);
  255.  
  256.       logfile.print(", ");
  257.       logfile.print("Shahad Just Signed OUT");
  258. #if ECHO_TO_SERIAL
  259.       Serial.print(", ");
  260.       Serial.print("Shahad Just Signed OUT");
  261.       lcd.setCursor(0, 1);
  262.       lcd.print("Shahad-Sign OUT");
  263.       delay(3000);
  264.       lcd.setCursor(0, 1);
  265.       lcd.print("                ");
  266. #endif //ECHO_TO_SERIAL
  267.       logfile.println();
  268. #if ECHO_TO_SERIAL
  269.       Serial.println();
  270. #endif // ECHO_TO_SERIAL
  271.       // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  272.       // which uses a bunch of power and takes time
  273.       if ((millis() - syncTime) < SYNC_INTERVAL) return;
  274.       syncTime = millis();
  275.       // blink LED to show we are syncing data to the card & updating FAT!
  276.       logfile.flush();
  277.       delay(1000);
  278.     }
  279.   }
  280.   ///////////////////////////////////////////////////////////////////////// User #001 /end
  281.  
  282.   ///////////////////////////////////////////////////////////////////////// User #002 /start       //***** [1]
  283.  
  284.   if (content.substring(1) == user002ID)                              //***** [2]
  285.   {
  286.     if (user002Status == true) {                                      //***** [3]
  287.       user002Status = false;                                          //***** [4]
  288.  
  289.       DateTime now;
  290.       now = RTC.now();
  291.       logfile.print('"');
  292.       logfile.print(now.year(), DEC);
  293.       logfile.print("/");
  294.       logfile.print(now.month(), DEC);
  295.       logfile.print("/");
  296.       logfile.print(now.day(), DEC);
  297.       logfile.print('"');
  298.       logfile.print(",");
  299.       logfile.print(now.hour(), DEC);
  300.       logfile.print(":");
  301.       logfile.print(now.minute(), DEC);
  302.       logfile.print(":");
  303.       logfile.print(now.second(), DEC);
  304. #if ECHO_TO_SERIAL
  305.       Serial.print('"');
  306.       Serial.print(now.year(), DEC);
  307.       Serial.print("/");
  308.       Serial.print(now.month(), DEC);
  309.       Serial.print("/");
  310.       Serial.print(now.day(), DEC);
  311.       Serial.print(" ");
  312.       Serial.print(now.hour(), DEC);
  313.       Serial.print(":");
  314.       Serial.print(now.minute(), DEC);
  315.       Serial.print(":");
  316.       Serial.print(now.second(), DEC);
  317.       Serial.print('"');
  318. #endif //ECHO_TO_SERIAL
  319.       delay(10);
  320.       logfile.print(", ");
  321.       logfile.print("Ayat Alshaabani Just Signed IN");                           //***** [5]
  322. #if ECHO_TO_SERIAL
  323.       Serial.print(", ");
  324.       Serial.print("Ayat Just Signed IN");                            //***** [6]
  325.       lcd.setCursor(0, 1);
  326.       //        "1234567891234567"    16x characters only #######
  327.       lcd.print("Ayat-Sign IN ");                                      //***** [7]
  328.       delay(3000);
  329.       lcd.setCursor(0, 1);
  330.       lcd.print("                ");
  331. #endif //ECHO_TO_SERIAL
  332.       logfile.println();
  333. #if ECHO_TO_SERIAL
  334.       Serial.println();
  335. #endif // ECHO_TO_SERIAL
  336.       if ((millis() - syncTime) < SYNC_INTERVAL) return;
  337.       syncTime = millis();
  338.       logfile.flush();
  339.       delay(1000);
  340.     }
  341.     else if (user002Status == false) {                                //***** [8]
  342.       user002Status = true;                                           //***** [9]
  343.       DateTime now;
  344.       now = RTC.now();
  345.       logfile.print('"');
  346.       logfile.print(now.year(), DEC);
  347.       logfile.print("/");
  348.       logfile.print(now.month(), DEC);
  349.       logfile.print("/");
  350.       logfile.print(now.day(), DEC);
  351.       logfile.print('"');
  352.       logfile.print(",");
  353.       logfile.print(now.hour(), DEC);
  354.       logfile.print(":");
  355.       logfile.print(now.minute(), DEC);
  356.       logfile.print(":");
  357.       logfile.print(now.second(), DEC);
  358. #if ECHO_TO_SERIAL
  359.       Serial.print('"');
  360.       Serial.print(now.year(), DEC);
  361.       Serial.print("/");
  362.       Serial.print(now.month(), DEC);
  363.       Serial.print("/");
  364.       Serial.print(now.day(), DEC);
  365.       Serial.print(" ");
  366.       Serial.print(now.hour(), DEC);
  367.       Serial.print(":");
  368.       Serial.print(now.minute(), DEC);
  369.       Serial.print(":");
  370.       Serial.print(now.second(), DEC);
  371.       Serial.print('"');
  372. #endif //ECHO_TO_SERIAL
  373.       delay(10);
  374.       logfile.print(", ");
  375.       logfile.print("Ayat Alshaabani Just Signed OUT");                          //***** [10]
  376. #if ECHO_TO_SERIAL
  377.       Serial.print(", ");
  378.       Serial.print("Ayat Just Signed OUT");                           //***** [11]
  379.       lcd.setCursor(0, 1);
  380.       //        "1234567891234567"    16x characters only #######
  381.       lcd.print("Ayat-Sign OUT");                                      //***** [12]
  382.       delay(3000);
  383.       lcd.setCursor(0, 1);
  384.       lcd.print("                ");
  385. #endif //ECHO_TO_SERIAL
  386.       logfile.println();
  387. #if ECHO_TO_SERIAL
  388.       Serial.println();
  389. #endif // ECHO_TO_SERIAL
  390.       if ((millis() - syncTime) < SYNC_INTERVAL) return;
  391.       syncTime = millis();
  392.       logfile.flush();
  393.       delay(1000);
  394.     }
  395.   }
  396.   ///////////////////////////////////////////////////////////////////////// User #002 /end      //***** [13]
  397.  
  398. }// end of LOOP
  399.  
  400.  
  401. void buzz() {
  402.   digitalWrite(7, 1);
  403.   delay(100);
  404.   digitalWrite(7, 0);
  405.   delay(50);
  406.  
  407. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×