Advertisement
safwan092

Untitled

Aug 15th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.28 KB | None | 0 0
  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 = "19 DD E3 B8";
  16. boolean user001Status = true;
  17. //-------------------------------------------------------------------------
  18.  
  19. //-------------------------------------------------------------------------USER # 2
  20. String user002ID = "02 A6 B1 34";
  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.backlight();
  61. lcd.print("Connecting...");
  62. Serial.begin(9600);
  63. Serial.println();
  64. SPI.begin(); // Initiate SPI bus
  65. mfrc522.PCD_Init(); // Initiate MFRC522
  66. pinMode(7, OUTPUT);
  67. #if WAIT_TO_START
  68. Serial.println("Type any character to start");
  69. while (!Serial.available());
  70. #endif //WAIT_TO_START
  71.  
  72. // initialize the SD card
  73. Serial.print("Initializing SD card...");
  74. // make sure that the default chip select pin is set to
  75. // output, even if you don't use it:
  76. pinMode(10, OUTPUT);
  77. // pinMode(buttonPin, INPUT);
  78. // see if the card is present and can be initialized:
  79. if (!SD.begin(chipSelect)) {
  80. error("Card failed, or not present");
  81. }
  82. Serial.println("card initialized.");
  83.  
  84. // create a new file
  85. char filename[] = "LOGGER00.CSV";
  86. for (uint8_t i = 0; i < 100; i++) {
  87. filename[6] = i / 10 + '0';
  88. filename[7] = i % 10 + '0';
  89. if (! SD.exists(filename)) {
  90. // only open a new file if it doesn't exist
  91. logfile = SD.open(filename, FILE_WRITE);
  92. break; // leave the loop!
  93. }
  94. }
  95.  
  96. if (! logfile) {
  97. error("couldnt create file");
  98. }
  99.  
  100. Serial.print("Logging to: ");
  101. Serial.println(filename);
  102.  
  103. // connect to RTC
  104. Wire.begin();
  105. if (!RTC.begin()) {
  106. logfile.println("RTC failed");
  107. #if ECHO_TO_SERIAL
  108. Serial.println("RTC failed");
  109. #endif //ECHO_TO_SERIAL
  110. }
  111.  
  112.  
  113. logfile.println("Date,Time,Log");
  114. #if ECHO_TO_SERIAL
  115. Serial.println("Date-Time,Status");
  116. #endif //ECHO_TO_SERIAL
  117.  
  118. delay(2000);
  119. lcd.clear();
  120. lcd.print("System Ready");
  121. delay(2500);
  122. lcd.clear();
  123. lcd.setCursor(0, 0);
  124. lcd.print("Log:");
  125. }
  126.  
  127. void loop()
  128. {
  129. // Look for new cards
  130. if ( ! mfrc522.PICC_IsNewCardPresent())
  131. {
  132. return;
  133. }
  134. // Select one of the cards
  135. if ( ! mfrc522.PICC_ReadCardSerial())
  136. {
  137. return;
  138. }
  139. //Show UID on serial monitor
  140. String content = "";
  141. byte letter;
  142. buzz();
  143. for (byte i = 0; i < mfrc522.uid.size; i++)
  144. {
  145. Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  146. Serial.print(mfrc522.uid.uidByte[i], HEX);
  147. content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  148. content.concat(String(mfrc522.uid.uidByte[i], HEX));
  149. }
  150. Serial.println();
  151. content.toUpperCase();
  152. //Serial.println(content.substring(1));
  153. ///////////////////////////////////////////////////////////////////////// User #001 /start
  154. if (content.substring(1) == user001ID)
  155. {
  156. if (user001Status == true) {
  157. user001Status = false;
  158. //Serial.println("User 001 Just Signed IN");
  159. DateTime now;
  160. // fetch the time
  161. now = RTC.now();
  162. // log time
  163. logfile.print('"');
  164. logfile.print(now.year(), DEC);
  165. logfile.print("/");
  166. logfile.print(now.month(), DEC);
  167. logfile.print("/");
  168. logfile.print(now.day(), DEC);
  169. logfile.print('"');
  170. logfile.print(",");
  171. logfile.print(now.hour(), DEC);
  172. logfile.print(":");
  173. logfile.print(now.minute(), DEC);
  174. logfile.print(":");
  175. logfile.print(now.second(), DEC);
  176. #if ECHO_TO_SERIAL
  177. Serial.print('"');
  178. Serial.print(now.year(), DEC);
  179. Serial.print("/");
  180. Serial.print(now.month(), DEC);
  181. Serial.print("/");
  182. Serial.print(now.day(), DEC);
  183. Serial.print(" ");
  184. Serial.print(now.hour(), DEC);
  185. Serial.print(":");
  186. Serial.print(now.minute(), DEC);
  187. Serial.print(":");
  188. Serial.print(now.second(), DEC);
  189. Serial.print('"');
  190. #endif //ECHO_TO_SERIAL
  191.  
  192. delay(10);
  193.  
  194. logfile.print(", ");
  195. logfile.print("User 001 Just Signed IN");
  196. #if ECHO_TO_SERIAL
  197. Serial.print(", ");
  198. Serial.print("User 001 Just Signed IN");
  199. lcd.setCursor(0, 1);
  200. lcd.print("User001-Sign IN");
  201. delay(3000);
  202. lcd.setCursor(0, 1);
  203. lcd.print(" ");
  204. #endif //ECHO_TO_SERIAL
  205. logfile.println();
  206. #if ECHO_TO_SERIAL
  207. Serial.println();
  208. #endif // ECHO_TO_SERIAL
  209. // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  210. // which uses a bunch of power and takes time
  211. if ((millis() - syncTime) < SYNC_INTERVAL) return;
  212. syncTime = millis();
  213. // blink LED to show we are syncing data to the card & updating FAT!
  214. logfile.flush();
  215. delay(1000);
  216. }
  217. else if (user001Status == false) {
  218. user001Status = true;
  219. //Serial.println("User 001 Just Signed OUT");
  220. DateTime now;
  221. // fetch the time
  222. now = RTC.now();
  223. // log time
  224. logfile.print('"');
  225. logfile.print(now.year(), DEC);
  226. logfile.print("/");
  227. logfile.print(now.month(), DEC);
  228. logfile.print("/");
  229. logfile.print(now.day(), DEC);
  230. logfile.print('"');
  231. logfile.print(",");
  232. logfile.print(now.hour(), DEC);
  233. logfile.print(":");
  234. logfile.print(now.minute(), DEC);
  235. logfile.print(":");
  236. logfile.print(now.second(), DEC);
  237. #if ECHO_TO_SERIAL
  238. Serial.print('"');
  239. Serial.print(now.year(), DEC);
  240. Serial.print("/");
  241. Serial.print(now.month(), DEC);
  242. Serial.print("/");
  243. Serial.print(now.day(), DEC);
  244. Serial.print(" ");
  245. Serial.print(now.hour(), DEC);
  246. Serial.print(":");
  247. Serial.print(now.minute(), DEC);
  248. Serial.print(":");
  249. Serial.print(now.second(), DEC);
  250. Serial.print('"');
  251. #endif //ECHO_TO_SERIAL
  252.  
  253. delay(10);
  254.  
  255. logfile.print(", ");
  256. logfile.print("User 001 Just Signed OUT");
  257. #if ECHO_TO_SERIAL
  258. Serial.print(", ");
  259. Serial.print("User 001 Just Signed OUT");
  260. lcd.setCursor(0, 1);
  261. lcd.print("User001-Sign OUT");
  262. delay(3000);
  263. lcd.setCursor(0, 1);
  264. lcd.print(" ");
  265. #endif //ECHO_TO_SERIAL
  266. logfile.println();
  267. #if ECHO_TO_SERIAL
  268. Serial.println();
  269. #endif // ECHO_TO_SERIAL
  270. // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  271. // which uses a bunch of power and takes time
  272. if ((millis() - syncTime) < SYNC_INTERVAL) return;
  273. syncTime = millis();
  274. // blink LED to show we are syncing data to the card & updating FAT!
  275. logfile.flush();
  276. delay(1000);
  277. }
  278. }
  279. ///////////////////////////////////////////////////////////////////////// User #001 /end
  280.  
  281. ///////////////////////////////////////////////////////////////////////// User #002 /start //*** [1]
  282.  
  283. if (content.substring(1) == user002ID) //*** [2]
  284. {
  285. if (user002Status == true) { //*** [3]
  286. user002Status = false; //*** [4]
  287.  
  288. DateTime now;
  289. now = RTC.now();
  290. logfile.print('"');
  291. logfile.print(now.year(), DEC);
  292. logfile.print("/");
  293. logfile.print(now.month(), DEC);
  294. logfile.print("/");
  295. logfile.print(now.day(), DEC);
  296. logfile.print('"');
  297. logfile.print(",");
  298. logfile.print(now.hour(), DEC);
  299. logfile.print(":");
  300. logfile.print(now.minute(), DEC);
  301. logfile.print(":");
  302. logfile.print(now.second(), DEC);
  303. #if ECHO_TO_SERIAL
  304. Serial.print('"');
  305. Serial.print(now.year(), DEC);
  306. Serial.print("/");
  307. Serial.print(now.month(), DEC);
  308. Serial.print("/");
  309. Serial.print(now.day(), DEC);
  310. Serial.print(" ");
  311. Serial.print(now.hour(), DEC);
  312. Serial.print(":");
  313. Serial.print(now.minute(), DEC);
  314. Serial.print(":");
  315. Serial.print(now.second(), DEC);
  316. Serial.print('"');
  317. #endif //ECHO_TO_SERIAL
  318. delay(10);
  319. logfile.print(", ");
  320. logfile.print("User 002 Just Signed IN"); //*** [5]
  321. #if ECHO_TO_SERIAL
  322. Serial.print(", ");
  323. Serial.print("User 002 Just Signed IN"); //*** [6]
  324. lcd.setCursor(0, 1);
  325. // "1234567891234567" 16x characters only #######
  326. lcd.print("User002-Sign IN "); //*** [7]
  327. delay(3000);
  328. lcd.setCursor(0, 1);
  329. lcd.print(" ");
  330. #endif //ECHO_TO_SERIAL
  331. logfile.println();
  332. #if ECHO_TO_SERIAL
  333. Serial.println();
  334. #endif // ECHO_TO_SERIAL
  335. if ((millis() - syncTime) < SYNC_INTERVAL) return;
  336. syncTime = millis();
  337. logfile.flush();
  338. delay(1000);
  339. }
  340. else if (user002Status == false) { //*** [8]
  341. user002Status = true; //*** [9]
  342. DateTime now;
  343. now = RTC.now();
  344. logfile.print('"');
  345. logfile.print(now.year(), DEC);
  346. logfile.print("/");
  347. logfile.print(now.month(), DEC);
  348. logfile.print("/");
  349. logfile.print(now.day(), DEC);
  350. logfile.print('"');
  351. logfile.print(",");
  352. logfile.print(now.hour(), DEC);
  353. logfile.print(":");
  354. logfile.print(now.minute(), DEC);
  355. logfile.print(":");
  356. logfile.print(now.second(), DEC);
  357. #if ECHO_TO_SERIAL
  358. Serial.print('"');
  359. Serial.print(now.year(), DEC);
  360. Serial.print("/");
  361. Serial.print(now.month(), DEC);
  362. Serial.print("/");
  363. Serial.print(now.day(), DEC);
  364. Serial.print(" ");
  365. Serial.print(now.hour(), DEC);
  366. Serial.print(":");
  367. Serial.print(now.minute(), DEC);
  368. Serial.print(":");
  369. Serial.print(now.second(), DEC);
  370. Serial.print('"');
  371. #endif //ECHO_TO_SERIAL
  372. delay(10);
  373. logfile.print(", ");
  374. logfile.print("User 002 Just Signed OUT"); //*** [10]
  375. #if ECHO_TO_SERIAL
  376. Serial.print(", ");
  377. Serial.print("User 002 Just Signed OUT"); //*** [11]
  378. lcd.setCursor(0, 1);
  379. // "1234567891234567" 16x characters only #######
  380. lcd.print("User002-Sign OUT"); //*** [12]
  381. delay(3000);
  382. lcd.setCursor(0, 1);
  383. lcd.print(" ");
  384. #endif //ECHO_TO_SERIAL
  385. logfile.println();
  386. #if ECHO_TO_SERIAL
  387. Serial.println();
  388. #endif // ECHO_TO_SERIAL
  389. if ((millis() - syncTime) < SYNC_INTERVAL) return;
  390. syncTime = millis();
  391. logfile.flush();
  392. delay(1000);
  393. }
  394. }
  395. ///////////////////////////////////////////////////////////////////////// User #002 /end //*** [13]
  396.  
  397. }// end of LOOP
  398.  
  399.  
  400. void buzz() {
  401. digitalWrite(7, 1);
  402. delay(100);
  403. digitalWrite(7, 0);
  404. delay(50);
  405.  
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement