Guest User

Betlog

a guest
Apr 25th, 2019
78
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h> //
  2. #include <MFRC522.h> // Library for RFID Reader
  3. #include <Wire.h> //
  4. #include <OnewireKeypad.h> // Library for Keypad
  5. #include <LiquidCrystal_I2C.h> // Library for LCD I2C
  6. #include <Servo.h> // Library for Servo Motor
  7.  
  8. Servo servo;
  9. int servoPos = 30;
  10.  
  11. #define innerSensorPin 2
  12. #define buzzerPin 6
  13. int gateSensorPin = 3; //IR sensor on gate to arduino pin 3
  14.  
  15. int innerSensor = 0;
  16. int outerSensor = 0;
  17.  
  18. #define RST_PIN 9
  19. #define SS_PIN 10
  20.  
  21. int card1Balance = 5000;
  22. int card2Balance = 300;
  23.  
  24. #define num 7
  25. char Data[num];
  26. byte data_count = 0;
  27.  
  28. String num1, num2, card, card2;
  29. int a, b;
  30. char Key;
  31.  
  32. bool recharge = false; // default recharge false
  33. bool paid = false; // to tell that the default is not paid
  34.  
  35. int slot1 = 5; //#1 slot parking
  36. int slot2 = 4; //#2 Slot parking
  37.  
  38. int slot1_LED = A0; // SLot 1 led
  39. int slot2_LED = A1; // slot 2 led
  40. int gate_grn = A2;
  41. int gate_red = A3;
  42.  
  43.  
  44. char KEYS[] = {
  45. '1', '2', '3', 'A',
  46. '4', '5', '6', 'B',
  47. '7', '8', '9', 'C',
  48. '*', '0', '#', 'D',
  49. };
  50.  
  51. OnewireKeypad <Print, 16 > KP2(Serial, KEYS, 4, 4, 7, 4700, 1000, ExtremePrec );
  52. LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  53. MFRC522 mfrc522(SS_PIN, RST_PIN);
  54.  
  55. void setup () {
  56. servo.attach(8);
  57.  
  58. servo.write(30);
  59. pinMode(slot1,INPUT); //setting slot pins & gate IR sensor as input to arduino
  60. pinMode(slot2,INPUT);
  61. pinMode(gateSensorPin,INPUT);
  62. pinMode(slot1_LED,OUTPUT);
  63. pinMode(slot2_LED,OUTPUT);
  64. pinMode(gate_grn,OUTPUT);
  65. pinMode(gate_red,OUTPUT);
  66. Serial.begin(9600); //initialzing Serial monitor
  67.  
  68. lcd.begin(16, 2);
  69. pinMode(innerSensorPin, INPUT);
  70. pinMode(buzzerPin, OUTPUT);
  71.  
  72. KP2.SetKeypadVoltage(5.0);
  73.  
  74. SPI.begin();
  75. mfrc522.PCD_Init();
  76.  
  77. lcd.setCursor(0, 0);
  78. lcd.print("Automatic Toll");
  79. lcd.setCursor(0, 1);
  80. lcd.print("colection system");
  81. delay(3000);
  82. }
  83. void loop()
  84. {
  85. //the car arrives and sensor goes LOW
  86.  
  87. if(!(digitalRead(gateSensorPin)) && digitalRead(slot1) && digitalRead(slot2)) //slot1 & slot2 empty
  88. {
  89. lcd.clear();
  90. lcd.setCursor(0,0);
  91. lcd.print("Available:"); //print slot1 and slo2 available
  92. lcd.setCursor(0,1);
  93. lcd.print("Slot1, Slot2"); //print slot1 and slo2 available
  94. analogWrite(slot1_LED,222);
  95. analogWrite(slot2_LED,222);
  96. delay(1000);
  97. analogWrite(gate_grn,222);
  98. //gate.write(75); //gate will open after the dealy of 1 second
  99. }
  100. else if( !(digitalRead(gateSensorPin)) && !(digitalRead(slot1)) && digitalRead(slot2)) //car on slot1,slot2 free
  101. {
  102. lcd.clear();
  103. lcd.setCursor(0,0);
  104. lcd.print("Available:"); //print slot1 and slo2 available
  105. lcd.setCursor(0,1);
  106. lcd.print("Slot2"); //print slot1 and slo2 available
  107.  
  108. analogWrite(slot1_LED,0);
  109. analogWrite(slot2_LED,222);
  110. delay(1000);
  111. analogWrite(gate_grn,222);
  112. //gate.write(75); //gate will open after the dealy of 1 second
  113. }
  114. else if( !(digitalRead(gateSensorPin)) && digitalRead(slot1) && !(digitalRead(slot2))) //car on slot2,slot1 free
  115. {
  116. lcd.clear();
  117. lcd.setCursor(0,0);
  118. lcd.print("Available:"); //print slot1 and slo2 available
  119. lcd.setCursor(0,1);
  120. lcd.print("Slot1"); //print slot1 and slo2 available
  121. analogWrite(slot1_LED,222);
  122. analogWrite(slot2_LED,0);
  123. delay(1000);
  124. analogWrite(gate_grn,222);
  125. //gate.write(75);
  126. //delay(100); //gate will open after the dealy of 1 second
  127. }
  128. else if( !(digitalRead(gateSensorPin)) && !(digitalRead(slot1)) && !(digitalRead(slot2)))
  129. {
  130. lcd.clear();
  131. lcd.setCursor(0,0);
  132. lcd.print("Available:"); //print slot1 and slo2 available
  133. lcd.setCursor(0,1);
  134. lcd.print("Parking is Full"); //print slot1 and slo2 available
  135. analogWrite(slot1_LED,0);
  136. analogWrite(slot2_LED,0);
  137. delay(1000);
  138. analogWrite(gate_red,222);
  139. delay(100);
  140. analogWrite(gate_red,0);
  141. delay(100);
  142. analogWrite(gate_red,222);
  143. delay(100);
  144. analogWrite(gate_red,0);
  145. }
  146. else if( digitalRead(gateSensorPin)) //no input detected
  147. {
  148. lcd.clear();
  149. Serial.println("Welcome");
  150. gate.write(5); //gate close
  151. analogWrite(slot1_LED,0);
  152. analogWrite(slot2_LED,0);
  153. analogWrite(gate_red,0);
  154. analogWrite(gate_grn,222); //yellow/green light will blink from here
  155. delay(100);
  156. analogWrite(gate_grn,0);
  157. delay(100);
  158. }
  159. if (recharge)
  160. {
  161. reCharge();
  162. }
  163. else{
  164. if((digitalRead(slot1)) || (digitalRead(slot2))){
  165. sensorRead();
  166. check_RFID();
  167. KeyPad();
  168. if (innerSensor == 0) // if there's a vehicle inside
  169. {
  170. servoDown();
  171. lcd.clear();
  172. lcd.setCursor(0, 0);
  173. lcd.print("Vehicle detected");
  174. delay(1000);
  175. lcd.clear();
  176. lcd.setCursor(0, 0);
  177. lcd.print("Put your card to");
  178. lcd.setCursor(0, 1);
  179. lcd.print("the reader......");
  180. delay(2000);
  181. lcd.clear();
  182.  
  183. }
  184. else if (outerSensor == 0 && paid) // if there's a vehicle at the gate and paid
  185. {
  186. servoUp();
  187. lcd.clear();
  188. lcd.setCursor(0, 0);
  189. lcd.print("Have a safe");
  190. lcd.setCursor(0, 1);
  191. lcd.print("journey");
  192. delay(1000);
  193. lcd.clear();
  194. paid = false;
  195. }
  196. }
  197. }
  198. }
  199.  
  200. void servoDown()
  201. {
  202. servo.attach(8);
  203. for (; servoPos <= 120; servoPos += 1)
  204. {
  205. servo.write(servoPos);
  206. delay(5);
  207. }
  208. }
  209.  
  210. void servoUp()
  211. {
  212. servo.attach(8);
  213. for (; servoPos >= 30; servoPos -= 1)
  214. {
  215. servo.write(servoPos);
  216. delay(5);
  217. }
  218. }
  219.  
  220. void sensorRead()
  221. {
  222. innerSensor = digitalRead(innerSensorPin);
  223. outerSensor = digitalRead(gateSensorPin);
  224. }
  225.  
  226. void check_RFID()
  227. {
  228. if ( ! mfrc522.PICC_IsNewCardPresent())
  229. {
  230. return;
  231. }
  232. if ( ! mfrc522.PICC_ReadCardSerial())
  233. {
  234. return;
  235. }
  236.  
  237. String content = "";
  238. for (byte i = 0; i < mfrc522.uid.size; i++)
  239. {
  240. content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  241. content.concat(String(mfrc522.uid.uidByte[i], HEX));
  242. }
  243. content.toUpperCase();
  244.  
  245. if (content.substring(1) == "D3 D0 E4 24")
  246. {
  247. if (card1Balance >= 500)
  248. {
  249. successfullyPaid();
  250. card1Balance = card1Balance - 500;
  251. lcd.setCursor(9, 1);
  252. lcd.print(card1Balance);
  253. delay(2000);
  254. lcd.clear();
  255. paid = true;
  256. }
  257. else
  258. {
  259. card = content.substring(1);
  260. failToPay();
  261. lcd.setCursor(9, 1);
  262. lcd.print(card1Balance);
  263. lcd.print(" Tk");
  264. delay(2000);
  265. lcd.clear();
  266. lcd.setCursor(0, 0);
  267. lcd.print("Please Recharge");
  268. delay(1000);
  269. lcd.clear();
  270. paid = false;
  271. }
  272. }
  273. else if (content.substring(1) == "E3 06 19 D3")
  274. {
  275. if (card2Balance >= 500)
  276. {
  277. successfullyPaid();
  278. card2Balance = card2Balance - 500;
  279. lcd.setCursor(9, 1);
  280. lcd.print(card2Balance);
  281. delay(2000);
  282. lcd.clear();
  283. paid = true;
  284. }
  285. else
  286. {
  287. card = content.substring(1);
  288. failToPay();
  289. lcd.setCursor(9, 1);
  290. lcd.print(card2Balance);
  291. lcd.print(" Tk");
  292. delay(2000);
  293. lcd.clear();
  294. lcd.setCursor(0, 0);
  295. lcd.print("Please Recharge");
  296. lcd.clear();
  297. delay(1000);
  298. paid = false;
  299. }
  300. }
  301. else{
  302. digitalWrite(buzzerPin, HIGH);
  303. lcd.setCursor(0, 0);
  304. lcd.print("Unknown Vehicle");
  305. lcd.setCursor(0, 1);
  306. lcd.print("Access denied");
  307. delay(1500);
  308. lcd.clear();
  309. digitalWrite(buzzerPin, LOW);
  310. }
  311. }
  312.  
  313. void KeyPad()
  314. {
  315. byte KState = KP2.Key_State();
  316.  
  317. if (KState == PRESSED)
  318. {
  319. Key = KP2.Getkey();
  320. if (Key)
  321. {
  322. if (Key == 'A')
  323. {
  324. lcd.clear();
  325. lcd.setCursor(0, 0);
  326. lcd.print("Recharging Mode.");
  327. lcd.setCursor(0, 1);
  328. lcd.print("................");
  329. delay(1500);
  330. lcd.clear();
  331. recharge = true;
  332. }
  333. }
  334. }
  335. }
  336.  
  337. void clearData()
  338. {
  339. while (data_count != 0)
  340. {
  341. Data[data_count--] = 0;
  342. }
  343. return;
  344. }
  345.  
  346. void reCharge()
  347. {
  348.  
  349. lcd.setCursor(0, 0);
  350. lcd.print ("Enter the amount");
  351.  
  352. byte KState = KP2.Key_State();
  353.  
  354. if (KState == PRESSED)
  355. {
  356. Key = KP2.Getkey();
  357. if (Key)
  358. {
  359. if (Key == '#')
  360. {
  361. if (card == "D3 D0 E4 24")
  362. {
  363. num1 = Data;
  364. card1Balance = num1.toInt() + card1Balance;
  365. lcd.clear();
  366. lcd.setCursor(0, 0);
  367. lcd.print("Your current");
  368. lcd.setCursor(0, 1);
  369. lcd.print("balance: ");
  370. lcd.setCursor(9, 1);
  371. lcd.print (card1Balance);
  372. lcd.print(" Tk");
  373. delay(3000);
  374. clearData();
  375. lcd.clear();
  376. recharge = 1;
  377. }
  378. else if (card == "E3 06 19 D3")
  379. {
  380. num2 = Data;
  381. card2Balance = num2.toInt() + card2Balance;
  382. lcd.clear();
  383. lcd.setCursor(0, 0);
  384. lcd.print("Your current");
  385. lcd.setCursor(0, 1);
  386. lcd.print("balance: ");
  387. lcd.setCursor(9, 1);
  388. lcd.print (card2Balance);
  389. lcd.print(" Tk");
  390. delay(3000);
  391. clearData();
  392. lcd.clear();
  393. recharge = 1;
  394. }
  395. }
  396. else
  397. {
  398. Data[data_count] = Key;
  399. lcd.setCursor(data_count, 1);
  400. lcd.print(Data[data_count]);
  401. data_count++;
  402. }
  403. }
  404. }
  405. }
  406.  
  407. void successfullyPaid()
  408. {
  409. digitalWrite(buzzerPin, HIGH);
  410. delay(200);
  411. digitalWrite(buzzerPin, LOW);
  412. delay(100);
  413. lcd.clear();
  414. lcd.setCursor(0, 0);
  415. lcd.print(" Successfully");
  416. lcd.setCursor(0, 1);
  417. lcd.print(" paid your bill");
  418. delay(1500);
  419. lcd.clear();
  420. lcd.setCursor(0, 0);
  421. lcd.print("Your Remaining");
  422. lcd.setCursor(0, 1);
  423. lcd.print("balance: ");
  424. }
  425.  
  426. void failToPay()
  427. {
  428. digitalWrite(buzzerPin, HIGH);
  429. delay(200);
  430. digitalWrite(buzzerPin, LOW);
  431. delay(100);
  432. lcd.clear();
  433. lcd.setCursor(0, 0);
  434. lcd.print(" Your balance");
  435. lcd.setCursor(0, 1);
  436. lcd.print(" is insufficent");
  437. delay(1500);
  438. lcd.clear();
  439. lcd.setCursor(0, 0);
  440. lcd.print("Your Remaining");
  441. lcd.setCursor(0, 1);
  442. lcd.print("balance: ");
  443. }
RAW Paste Data