Advertisement
safwan092

Untitled

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