Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 KB | None | 0 0
  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. servo.write(30);
  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. servo.write(120);
  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 sensorRead()
  201. {
  202. innerSensor = digitalRead(innerSensorPin);
  203. outerSensor = digitalRead(gateSensorPin);
  204. }
  205.  
  206. void check_RFID()
  207. {
  208. if ( ! mfrc522.PICC_IsNewCardPresent())
  209. {
  210. return;
  211. }
  212. if ( ! mfrc522.PICC_ReadCardSerial())
  213. {
  214. return;
  215. }
  216.  
  217. String content = "";
  218. for (byte i = 0; i < mfrc522.uid.size; i++)
  219. {
  220. content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  221. content.concat(String(mfrc522.uid.uidByte[i], HEX));
  222. }
  223. content.toUpperCase();
  224.  
  225. if (content.substring(1) == "D3 D0 E4 24")
  226. {
  227. if (card1Balance >= 500)
  228. {
  229. successfullyPaid();
  230. card1Balance = card1Balance - 500;
  231. lcd.setCursor(9, 1);
  232. lcd.print(card1Balance);
  233. delay(2000);
  234. lcd.clear();
  235. paid = true;
  236. }
  237. else
  238. {
  239. card = content.substring(1);
  240. failToPay();
  241. lcd.setCursor(9, 1);
  242. lcd.print(card1Balance);
  243. lcd.print(" Tk");
  244. delay(2000);
  245. lcd.clear();
  246. lcd.setCursor(0, 0);
  247. lcd.print("Please Recharge");
  248. delay(1000);
  249. lcd.clear();
  250. paid = false;
  251. }
  252. }
  253. else if (content.substring(1) == "E3 06 19 D3")
  254. {
  255. if (card2Balance >= 500)
  256. {
  257. successfullyPaid();
  258. card2Balance = card2Balance - 500;
  259. lcd.setCursor(9, 1);
  260. lcd.print(card2Balance);
  261. delay(2000);
  262. lcd.clear();
  263. paid = true;
  264. }
  265. else
  266. {
  267. card = content.substring(1);
  268. failToPay();
  269. lcd.setCursor(9, 1);
  270. lcd.print(card2Balance);
  271. lcd.print(" Tk");
  272. delay(2000);
  273. lcd.clear();
  274. lcd.setCursor(0, 0);
  275. lcd.print("Please Recharge");
  276. lcd.clear();
  277. delay(1000);
  278. paid = false;
  279. }
  280. }
  281. else{
  282. digitalWrite(buzzerPin, HIGH);
  283. lcd.setCursor(0, 0);
  284. lcd.print("Unknown Vehicle");
  285. lcd.setCursor(0, 1);
  286. lcd.print("Access denied");
  287. delay(1500);
  288. lcd.clear();
  289. digitalWrite(buzzerPin, LOW);
  290. }
  291. }
  292.  
  293. void KeyPad()
  294. {
  295. byte KState = KP2.Key_State();
  296.  
  297. if (KState == PRESSED)
  298. {
  299. Key = KP2.Getkey();
  300. if (Key)
  301. {
  302. if (Key == 'A')
  303. {
  304. lcd.clear();
  305. lcd.setCursor(0, 0);
  306. lcd.print("Recharging Mode.");
  307. lcd.setCursor(0, 1);
  308. lcd.print("................");
  309. delay(1500);
  310. lcd.clear();
  311. recharge = true;
  312. }
  313. }
  314. }
  315. }
  316.  
  317. void clearData()
  318. {
  319. while (data_count != 0)
  320. {
  321. Data[data_count--] = 0;
  322. }
  323. return;
  324. }
  325.  
  326. void reCharge()
  327. {
  328.  
  329. lcd.setCursor(0, 0);
  330. lcd.print ("Enter the amount");
  331.  
  332. byte KState = KP2.Key_State();
  333.  
  334. if (KState == PRESSED)
  335. {
  336. Key = KP2.Getkey();
  337. if (Key)
  338. {
  339. if (Key == '#')
  340. {
  341. if (card == "D3 D0 E4 24")
  342. {
  343. num1 = Data;
  344. card1Balance = num1.toInt() + card1Balance;
  345. lcd.clear();
  346. lcd.setCursor(0, 0);
  347. lcd.print("Your current");
  348. lcd.setCursor(0, 1);
  349. lcd.print("balance: ");
  350. lcd.setCursor(9, 1);
  351. lcd.print (card1Balance);
  352. lcd.print(" Tk");
  353. delay(3000);
  354. clearData();
  355. lcd.clear();
  356. recharge = 1;
  357. }
  358. else if (card == "E3 06 19 D3")
  359. {
  360. num2 = Data;
  361. card2Balance = num2.toInt() + card2Balance;
  362. lcd.clear();
  363. lcd.setCursor(0, 0);
  364. lcd.print("Your current");
  365. lcd.setCursor(0, 1);
  366. lcd.print("balance: ");
  367. lcd.setCursor(9, 1);
  368. lcd.print (card2Balance);
  369. lcd.print(" Tk");
  370. delay(3000);
  371. clearData();
  372. lcd.clear();
  373. recharge = 1;
  374. }
  375. }
  376. else
  377. {
  378. Data[data_count] = Key;
  379. lcd.setCursor(data_count, 1);
  380. lcd.print(Data[data_count]);
  381. data_count++;
  382. }
  383. }
  384. }
  385. }
  386.  
  387. void successfullyPaid()
  388. {
  389. digitalWrite(buzzerPin, HIGH);
  390. delay(200);
  391. digitalWrite(buzzerPin, LOW);
  392. delay(100);
  393. lcd.clear();
  394. lcd.setCursor(0, 0);
  395. lcd.print(" Successfully");
  396. lcd.setCursor(0, 1);
  397. lcd.print(" paid your bill");
  398. delay(1500);
  399. lcd.clear();
  400. lcd.setCursor(0, 0);
  401. lcd.print("Your Remaining");
  402. lcd.setCursor(0, 1);
  403. lcd.print("balance: ");
  404. }
  405.  
  406. void failToPay()
  407. {
  408. digitalWrite(buzzerPin, HIGH);
  409. delay(200);
  410. digitalWrite(buzzerPin, LOW);
  411. delay(100);
  412. lcd.clear();
  413. lcd.setCursor(0, 0);
  414. lcd.print(" Your balance");
  415. lcd.setCursor(0, 1);
  416. lcd.print(" is insufficent");
  417. delay(1500);
  418. lcd.clear();
  419. lcd.setCursor(0, 0);
  420. lcd.print("Your Remaining");
  421. lcd.setCursor(0, 1);
  422. lcd.print("balance: ");
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement