Advertisement
safwan092

مشروع سماهر RFID + I2C LCD تحضير وغياب

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