safwan092

Untitled

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