Advertisement
safwan092

Untitled

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