safwan092

Untitled

Jun 3rd, 2023
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.30 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 = "E4 F5 0A 2A";
  20. String userID_NUMBER = "44000066";
  21. boolean userIDStatus = true;
  22. //-------------------------------------------------------------------------
  23.  
  24. //-------------------------------------------------------------------------USER # 2
  25. String user2ID = "C0 B5 D1 32";
  26. String user2ID_NUMBER = "44006716";
  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.init();
  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. //Serial.println("Aiman Just Signed IN");
  165. logTime();
  166. #if ECHO_TO_SERIAL
  167. logTimeSerial();
  168. #endif //ECHO_TO_SERIAL
  169.  
  170. delay(10);
  171.  
  172. logfile.print(", ");
  173. logfile.print("Dana Hammad Altalhi IN");
  174. logfile.print(", ");
  175. logfile.print(userID_NUMBER);
  176. lcd.setCursor(5, 0);
  177. lcd.print(userID_NUMBER);
  178. lcd.setCursor(0, 1);
  179. lcd.print("Dana-Sign IN");
  180. delay(3000);
  181. clearLCD();
  182. #if ECHO_TO_SERIAL
  183. Serial.print(", ");
  184. Serial.print("Dana Hammad Altalhi Just Signed IN");
  185.  
  186. #endif //ECHO_TO_SERIAL
  187. logfile.println();
  188. #if ECHO_TO_SERIAL
  189. Serial.println();
  190. #endif // ECHO_TO_SERIAL
  191. // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  192. // which uses a bunch of power and takes time
  193. if ((millis() - syncTime) < SYNC_INTERVAL) return;
  194. syncTime = millis();
  195. // blink LED to show we are syncing data to the card & updating FAT!
  196. logfile.flush();
  197. delay(1000);
  198. }
  199. else if (userIDStatus == false) {
  200. userIDStatus = true;
  201. //Serial.println("User 001 Just Signed OUT");
  202. logTime();
  203. #if ECHO_TO_SERIAL
  204. logTimeSerial();
  205. #endif //ECHO_TO_SERIAL
  206.  
  207. delay(10);
  208.  
  209. logfile.print(", ");
  210. logfile.print("Dana Hammad Altalhi OUT");
  211. logfile.print(", ");
  212. logfile.print("3720559");
  213. lcd.setCursor(5, 0);
  214. lcd.print("3720559");
  215. lcd.setCursor(0, 1);
  216. lcd.print("Dana-Sign OUT");
  217. delay(3000);
  218. clearLCD();
  219. #if ECHO_TO_SERIAL
  220. Serial.print(", ");
  221. Serial.print("Dana Hammad Altalhi Just Signed OUT");
  222. #endif //ECHO_TO_SERIAL
  223. logfile.println();
  224. #if ECHO_TO_SERIAL
  225. Serial.println();
  226. #endif // ECHO_TO_SERIAL
  227. // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  228. // which uses a bunch of power and takes time
  229. if ((millis() - syncTime) < SYNC_INTERVAL) return;
  230. syncTime = millis();
  231. // blink LED to show we are syncing data to the card & updating FAT!
  232. logfile.flush();
  233. delay(1000);
  234. }
  235. }
  236. ///////////////////////////////////////////////////////////////////////// User #001 /end
  237.  
  238. ///////////////////////////////////////////////////////////////////////// User #002 /start //* [1]
  239.  
  240. if (content.substring(1) == user2ID) //* [2]
  241. {
  242. if (user2Status == true) { //* [3]
  243. user2Status = false; //* [4]
  244. logTime();
  245. #if ECHO_TO_SERIAL
  246. logTimeSerial();
  247. #endif //ECHO_TO_SERIAL
  248. delay(10);
  249. logfile.print(", ");
  250. logfile.print("Atheer Abdullah Alkuhayli IN"); //* [5]
  251. logfile.print(", ");
  252. logfile.print(user2ID_NUMBER);
  253. lcd.setCursor(5, 0);
  254. lcd.print(user2ID_NUMBER);
  255. lcd.setCursor(0, 1);
  256. // "1234567891234567" 16x characters only #######
  257. lcd.print("Atheer-Sign IN "); //* [7]
  258. delay(3000);
  259. clearLCD();
  260. #if ECHO_TO_SERIAL
  261. Serial.print(", ");
  262. Serial.print("Atheer Abdullah Alkuhayli Just Signed IN"); //* [6]
  263. #endif //ECHO_TO_SERIAL
  264. logfile.println();
  265. #if ECHO_TO_SERIAL
  266. Serial.println();
  267. #endif // ECHO_TO_SERIAL
  268. if ((millis() - syncTime) < SYNC_INTERVAL) return;
  269. syncTime = millis();
  270. logfile.flush();
  271. delay(1000);
  272. }
  273. else if (user2Status == false) { //* [8]
  274. user2Status = true; //* [9]
  275. logTime();
  276. #if ECHO_TO_SERIAL
  277. logTimeSerial();
  278. #endif //ECHO_TO_SERIAL
  279. delay(10);
  280. logfile.print(", ");
  281. logfile.print("Atheer Abdullah Alkuhayli OUT"); //* [10]
  282. logfile.print(", ");
  283. logfile.print(user2ID_NUMBER);
  284. lcd.setCursor(5, 0);
  285. lcd.print(user2ID_NUMBER);
  286. lcd.setCursor(0, 1);
  287. // "1234567891234567" 16x characters only #######
  288. lcd.print("Atheer-Sign OUT"); //* [12]
  289. delay(3000);
  290. clearLCD();
  291. #if ECHO_TO_SERIAL
  292. Serial.print(", ");
  293. Serial.print("Atheer Abdullah Alkuhayli Just Signed OUT"); //* [11]
  294. #endif //ECHO_TO_SERIAL
  295. logfile.println();
  296. #if ECHO_TO_SERIAL
  297. Serial.println();
  298. #endif // ECHO_TO_SERIAL
  299. if ((millis() - syncTime) < SYNC_INTERVAL) return;
  300. syncTime = millis();
  301. logfile.flush();
  302. delay(1000);
  303. }
  304. }
  305. ///////////////////////////////////////////////////////////////////////// User #002 /end //* [13]
  306.  
  307. }// end of LOOP
  308.  
  309.  
  310. void buzz() {
  311. digitalWrite(7, 1);
  312. delay(100);
  313. digitalWrite(7, 0);
  314. delay(50);
  315.  
  316. }
  317.  
  318. void logTime() {
  319. now = RTC.now();
  320. logfile.print('"');
  321. logfile.print(now.year(), DEC);
  322. logfile.print("/");
  323. logfile.print(now.month(), DEC);
  324. logfile.print("/");
  325. logfile.print(now.day(), DEC);
  326. logfile.print('"');
  327. logfile.print(",");
  328. logfile.print(now.hour(), DEC);
  329. logfile.print(":");
  330. logfile.print(now.minute(), DEC);
  331. logfile.print(":");
  332. logfile.print(now.second(), DEC);
  333. }
  334.  
  335. void logTimeSerial() {
  336. Serial.print('"');
  337. Serial.print(now.year(), DEC);
  338. Serial.print("/");
  339. Serial.print(now.month(), DEC);
  340. Serial.print("/");
  341. Serial.print(now.day(), DEC);
  342. Serial.print(" ");
  343. Serial.print(now.hour(), DEC);
  344. Serial.print(":");
  345. Serial.print(now.minute(), DEC);
  346. Serial.print(":");
  347. Serial.print(now.second(), DEC);
  348. Serial.print('"');
  349. }
  350.  
  351. void clearLCD() {
  352. lcd.setCursor(0, 0);
  353. lcd.print("Log: ");
  354. lcd.setCursor(0, 1);
  355. lcd.print(" ");
  356. }
Advertisement
Add Comment
Please, Sign In to add comment