Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <MFRC522.h> // Library for RFID Reader
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h> // Library for LCD I2C
  5. #include <Servo.h> // Library for Servo Motor
  6. #include <Keypad.h>
  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. LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  39. MFRC522 mfrc522(SS_PIN, RST_PIN);
  40.  
  41. char keys[ROWS][COLS] = {
  42. {'1','2','3'},
  43. {'4','5','6'},
  44. {'7','8','9'},
  45. {'#','0','*'}
  46. };
  47. byte rowPins[4] = {A5, A4, A3, A2}; //connect to the row pinouts of the keypad
  48. byte colPins[3] = {A1, A0, 7}; //connect to the column pinouts of the keypad
  49.  
  50. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, 4, 3 );
  51.  
  52. void setup () {
  53. servo.attach(8);
  54.  
  55. servo.write(30);
  56. pinMode(slot1,INPUT); //setting slot pins & gate IR sensor as input to arduino
  57. pinMode(slot2,INPUT);
  58. pinMode(gateSensorPin,INPUT);
  59. Serial.begin(9600); //initialzing Serial monitor
  60.  
  61. lcd.begin(16, 2);
  62. pinMode(innerSensorPin, INPUT);
  63. pinMode(buzzerPin, OUTPUT);
  64.  
  65. SPI.begin();
  66. mfrc522.PCD_Init();
  67.  
  68. lcd.setCursor(0, 0);
  69. lcd.print("Automatic Toll");
  70. lcd.setCursor(0, 1);
  71. lcd.print("colection system");
  72. delay(3000);
  73. }
  74. void loop()
  75. {
  76. //the car arrives and sensor goes LOW
  77.  
  78. if(!(digitalRead(gateSensorPin)) && digitalRead(slot1) && digitalRead(slot2)) //slot1 & slot2 empty
  79. {
  80. lcd.clear();
  81. lcd.setCursor(0,0);
  82. lcd.print("Available:"); //print slot1 and slo2 available
  83. lcd.setCursor(0,1);
  84. lcd.print("Slot1, Slot2"); //print slot1 and slo2 available
  85. delay(1000);
  86. }
  87. else if( !(digitalRead(gateSensorPin)) && !(digitalRead(slot1)) && digitalRead(slot2)) //car on slot1,slot2 free
  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("Slot2"); //print slot1 and slo2 available
  94. delay(1000);
  95. }
  96. else if( !(digitalRead(gateSensorPin)) && digitalRead(slot1) && !(digitalRead(slot2))) //car on slot2,slot1 free
  97. {
  98. lcd.clear();
  99. lcd.setCursor(0,0);
  100. lcd.print("Available:"); //print slot1 and slo2 available
  101. lcd.setCursor(0,1);
  102. lcd.print("Slot1"); //print slot1 and slo2 available
  103. delay(1000);
  104. }
  105. else if( !(digitalRead(gateSensorPin)) && !(digitalRead(slot1)) && !(digitalRead(slot2)))
  106. {
  107. lcd.clear();
  108. lcd.setCursor(0,0);
  109. lcd.print("Available:"); //print slot1 and slo2 available
  110. lcd.setCursor(0,1);
  111. lcd.print("Parking is Full"); //print slot1 and slo2 available
  112. delay(1000);
  113. }
  114. else if( digitalRead(gateSensorPin)) //no input detected
  115. {
  116. lcd.clear();
  117. Serial.println("Welcome");
  118. delay(1000);
  119. }
  120. if (recharge)
  121. {
  122. reCharge();
  123. }
  124. else{
  125. if((digitalRead(slot1)) || (digitalRead(slot2))){
  126. sensorRead();
  127. check_RFID();
  128. KeyPad();
  129. if (innerSensor == 0) // if there's a vehicle inside
  130. {
  131. servo.write(30);
  132. lcd.clear();
  133. lcd.setCursor(0, 0);
  134. lcd.print("Vehicle detected");
  135. delay(1000);
  136. lcd.clear();
  137. lcd.setCursor(0, 0);
  138. lcd.print("Put your card to");
  139. lcd.setCursor(0, 1);
  140. lcd.print("the reader......");
  141. delay(2000);
  142. lcd.clear();
  143.  
  144. }
  145. else if (outerSensor == 0 && paid) // if there's a vehicle at the gate and paid
  146. {
  147. servo.write(120);
  148. lcd.clear();
  149. lcd.setCursor(0, 0);
  150. lcd.print("Have a safe");
  151. lcd.setCursor(0, 1);
  152. lcd.print("journey");
  153. delay(1000);
  154. lcd.clear();
  155. paid = false;
  156. }
  157. }
  158. }
  159. }
  160.  
  161. void sensorRead()
  162. {
  163. innerSensor = digitalRead(innerSensorPin);
  164. outerSensor = digitalRead(gateSensorPin);
  165. }
  166.  
  167. void check_RFID()
  168. {
  169. if ( ! mfrc522.PICC_IsNewCardPresent())
  170. {
  171. return;
  172. }
  173. if ( ! mfrc522.PICC_ReadCardSerial())
  174. {
  175. return;
  176. }
  177.  
  178. String content = "";
  179. for (byte i = 0; i < mfrc522.uid.size; i++)
  180. {
  181. content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  182. content.concat(String(mfrc522.uid.uidByte[i], HEX));
  183. }
  184. content.toUpperCase();
  185.  
  186. if (content.substring(1) == "D3 D0 E4 24")
  187. {
  188. if (card1Balance >= 500)
  189. {
  190. successfullyPaid();
  191. card1Balance = card1Balance - 500;
  192. lcd.setCursor(9, 1);
  193. lcd.print(card1Balance);
  194. delay(2000);
  195. lcd.clear();
  196. paid = true;
  197. }
  198. else
  199. {
  200. card = content.substring(1);
  201. failToPay();
  202. lcd.setCursor(9, 1);
  203. lcd.print(card1Balance);
  204. lcd.print(" Tk");
  205. delay(2000);
  206. lcd.clear();
  207. lcd.setCursor(0, 0);
  208. lcd.print("Please Recharge");
  209. delay(1000);
  210. lcd.clear();
  211. paid = false;
  212. }
  213. }
  214. else if (content.substring(1) == "E3 06 19 D3")
  215. {
  216. if (card2Balance >= 500)
  217. {
  218. successfullyPaid();
  219. card2Balance = card2Balance - 500;
  220. lcd.setCursor(9, 1);
  221. lcd.print(card2Balance);
  222. delay(2000);
  223. lcd.clear();
  224. paid = true;
  225. }
  226. else
  227. {
  228. card = content.substring(1);
  229. failToPay();
  230. lcd.setCursor(9, 1);
  231. lcd.print(card2Balance);
  232. lcd.print(" Tk");
  233. delay(2000);
  234. lcd.clear();
  235. lcd.setCursor(0, 0);
  236. lcd.print("Please Recharge");
  237. lcd.clear();
  238. delay(1000);
  239. paid = false;
  240. }
  241. }
  242. else{
  243. digitalWrite(buzzerPin, HIGH);
  244. lcd.setCursor(0, 0);
  245. lcd.print("Unknown Vehicle");
  246. lcd.setCursor(0, 1);
  247. lcd.print("Access denied");
  248. delay(1500);
  249. lcd.clear();
  250. digitalWrite(buzzerPin, LOW);
  251. }
  252. }
  253.  
  254. void KeyPad()
  255. {
  256. byte KState = Keypad.getState();
  257.  
  258. if (KState == PRESSED)
  259. {
  260. Key = Keypad.getKey();
  261. if (Key)
  262. {
  263. if (Key == '*')
  264. {
  265. lcd.clear();
  266. lcd.setCursor(0, 0);
  267. lcd.print("Recharging Mode.");
  268. lcd.setCursor(0, 1);
  269. lcd.print("................");
  270. delay(1500);
  271. lcd.clear();
  272. recharge = true;
  273. }
  274. }
  275. }
  276. }
  277.  
  278. void clearData()
  279. {
  280. while (data_count != 0)
  281. {
  282. Data[data_count--] = 0;
  283. }
  284. return;
  285. }
  286.  
  287. void reCharge()
  288. {
  289.  
  290. lcd.setCursor(0, 0);
  291. lcd.print ("Enter the amount");
  292.  
  293. byte KState = Keypad.getState();
  294.  
  295. if (KState == PRESSED)
  296. {
  297. Key = Keypad.getKey();
  298. if (Key)
  299. {
  300. if (Key == '#')
  301. {
  302. if (card == "D3 D0 E4 24")
  303. {
  304. num1 = Data;
  305. card1Balance = num1.toInt() + card1Balance;
  306. lcd.clear();
  307. lcd.setCursor(0, 0);
  308. lcd.print("Your current");
  309. lcd.setCursor(0, 1);
  310. lcd.print("balance: ");
  311. lcd.setCursor(9, 1);
  312. lcd.print (card1Balance);
  313. lcd.print(" Tk");
  314. delay(3000);
  315. clearData();
  316. lcd.clear();
  317. recharge = 1;
  318. }
  319. else if (card == "E3 06 19 D3")
  320. {
  321. num2 = Data;
  322. card2Balance = num2.toInt() + card2Balance;
  323. lcd.clear();
  324. lcd.setCursor(0, 0);
  325. lcd.print("Your current");
  326. lcd.setCursor(0, 1);
  327. lcd.print("balance: ");
  328. lcd.setCursor(9, 1);
  329. lcd.print (card2Balance);
  330. lcd.print(" Tk");
  331. delay(3000);
  332. clearData();
  333. lcd.clear();
  334. recharge = 1;
  335. }
  336. }
  337. else
  338. {
  339. Data[data_count] = Key;
  340. lcd.setCursor(data_count, 1);
  341. lcd.print(Data[data_count]);
  342. data_count++;
  343. }
  344. }
  345. }
  346. }
  347.  
  348. void successfullyPaid()
  349. {
  350. digitalWrite(buzzerPin, HIGH);
  351. delay(200);
  352. digitalWrite(buzzerPin, LOW);
  353. delay(100);
  354. lcd.clear();
  355. lcd.setCursor(0, 0);
  356. lcd.print(" Successfully");
  357. lcd.setCursor(0, 1);
  358. lcd.print(" paid your bill");
  359. delay(1500);
  360. lcd.clear();
  361. lcd.setCursor(0, 0);
  362. lcd.print("Your Remaining");
  363. lcd.setCursor(0, 1);
  364. lcd.print("balance: ");
  365. }
  366.  
  367. void failToPay()
  368. {
  369. digitalWrite(buzzerPin, HIGH);
  370. delay(200);
  371. digitalWrite(buzzerPin, LOW);
  372. delay(100);
  373. lcd.clear();
  374. lcd.setCursor(0, 0);
  375. lcd.print(" Your balance");
  376. lcd.setCursor(0, 1);
  377. lcd.print(" is insufficent");
  378. delay(1500);
  379. lcd.clear();
  380. lcd.setCursor(0, 0);
  381. lcd.print("Your Remaining");
  382. lcd.setCursor(0, 1);
  383. lcd.print("balance: ");
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement