Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h> //
- #include <MFRC522.h> // Library for RFID Reader
- #include <Wire.h> //
- #include <OnewireKeypad.h> // Library for Keypad
- #include <LiquidCrystal_I2C.h> // Library for LCD I2C
- #include <Servo.h> // Library for Servo Motor
- Servo servo;
- int servoPos = 30;
- #define innerSensorPin 2
- #define buzzerPin 6
- int gateSensorPin = 3; //IR sensor on gate to arduino pin 3
- int innerSensor = 0;
- int outerSensor = 0;
- #define RST_PIN 9
- #define SS_PIN 10
- int card1Balance = 5000;
- int card2Balance = 300;
- #define num 7
- char Data[num];
- byte data_count = 0;
- String num1, num2, card, card2;
- int a, b;
- char Key;
- bool recharge = false; // default recharge false
- bool paid = false; // to tell that the default is not paid
- int slot1 = 5; //#1 slot parking
- int slot2 = 4; //#2 Slot parking
- int slot1_LED = A0; // SLot 1 led
- int slot2_LED = A1; // slot 2 led
- int gate_grn = A2;
- int gate_red = A3;
- char KEYS[] = {
- '1', '2', '3', 'A',
- '4', '5', '6', 'B',
- '7', '8', '9', 'C',
- '*', '0', '#', 'D',
- };
- OnewireKeypad <Print, 16 > KP2(Serial, KEYS, 4, 4, 7, 4700, 1000, ExtremePrec );
- LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- MFRC522 mfrc522(SS_PIN, RST_PIN);
- void setup () {
- servo.attach(8);
- servo.write(30);
- pinMode(slot1,INPUT); //setting slot pins & gate IR sensor as input to arduino
- pinMode(slot2,INPUT);
- pinMode(gateSensorPin,INPUT);
- pinMode(slot1_LED,OUTPUT);
- pinMode(slot2_LED,OUTPUT);
- pinMode(gate_grn,OUTPUT);
- pinMode(gate_red,OUTPUT);
- Serial.begin(9600); //initialzing Serial monitor
- lcd.begin(16, 2);
- pinMode(innerSensorPin, INPUT);
- pinMode(buzzerPin, OUTPUT);
- KP2.SetKeypadVoltage(5.0);
- SPI.begin();
- mfrc522.PCD_Init();
- lcd.setCursor(0, 0);
- lcd.print("Automatic Toll");
- lcd.setCursor(0, 1);
- lcd.print("colection system");
- delay(3000);
- }
- void loop()
- {
- //the car arrives and sensor goes LOW
- if(!(digitalRead(gateSensorPin)) && digitalRead(slot1) && digitalRead(slot2)) //slot1 & slot2 empty
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Available:"); //print slot1 and slo2 available
- lcd.setCursor(0,1);
- lcd.print("Slot1, Slot2"); //print slot1 and slo2 available
- analogWrite(slot1_LED,222);
- analogWrite(slot2_LED,222);
- delay(1000);
- analogWrite(gate_grn,222);
- //gate.write(75); //gate will open after the dealy of 1 second
- }
- else if( !(digitalRead(gateSensorPin)) && !(digitalRead(slot1)) && digitalRead(slot2)) //car on slot1,slot2 free
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Available:"); //print slot1 and slo2 available
- lcd.setCursor(0,1);
- lcd.print("Slot2"); //print slot1 and slo2 available
- analogWrite(slot1_LED,0);
- analogWrite(slot2_LED,222);
- delay(1000);
- analogWrite(gate_grn,222);
- //gate.write(75); //gate will open after the dealy of 1 second
- }
- else if( !(digitalRead(gateSensorPin)) && digitalRead(slot1) && !(digitalRead(slot2))) //car on slot2,slot1 free
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Available:"); //print slot1 and slo2 available
- lcd.setCursor(0,1);
- lcd.print("Slot1"); //print slot1 and slo2 available
- analogWrite(slot1_LED,222);
- analogWrite(slot2_LED,0);
- delay(1000);
- analogWrite(gate_grn,222);
- //gate.write(75);
- //delay(100); //gate will open after the dealy of 1 second
- }
- else if( !(digitalRead(gateSensorPin)) && !(digitalRead(slot1)) && !(digitalRead(slot2)))
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Available:"); //print slot1 and slo2 available
- lcd.setCursor(0,1);
- lcd.print("Parking is Full"); //print slot1 and slo2 available
- analogWrite(slot1_LED,0);
- analogWrite(slot2_LED,0);
- delay(1000);
- analogWrite(gate_red,222);
- delay(100);
- analogWrite(gate_red,0);
- delay(100);
- analogWrite(gate_red,222);
- delay(100);
- analogWrite(gate_red,0);
- }
- else if( digitalRead(gateSensorPin)) //no input detected
- {
- lcd.clear();
- Serial.println("Welcome");
- //gate.write(5); //gate close
- analogWrite(slot1_LED,0);
- analogWrite(slot2_LED,0);
- analogWrite(gate_red,0);
- analogWrite(gate_grn,222); //yellow/green light will blink from here
- delay(100);
- analogWrite(gate_grn,0);
- delay(100);
- }
- if (recharge)
- {
- reCharge();
- }
- else{
- if((digitalRead(slot1)) || (digitalRead(slot2))){
- sensorRead();
- check_RFID();
- KeyPad();
- if (innerSensor == 0) // if there's a vehicle inside
- {
- //servoDown();
- servo.attach(8);
- servo.write(30);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Vehicle detected");
- delay(1000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Put your card to");
- lcd.setCursor(0, 1);
- lcd.print("the reader......");
- delay(2000);
- lcd.clear();
- }
- else if (outerSensor == 0 && paid) // if there's a vehicle at the gate and paid
- {
- //servoUp();
- servo.attach(8);
- servo.write(120);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Have a safe");
- lcd.setCursor(0, 1);
- lcd.print("journey");
- delay(1000);
- lcd.clear();
- paid = false;
- }
- }
- }
- }
- void servoDown()
- {
- servo.attach(8);
- for (int servoPosTemp=servoPos; servoPosTemp <= 120; servoPosTemp += 1)
- {
- servo.write(servoPosTemp);
- delay(5);
- servoPos=servoPosTemp;
- }
- }
- void servoUp()
- {
- servo.attach(8);
- for (int servoPosTemp=servoPos; servoPosTemp >= 30; servoPosTemp -= 1)
- {
- servo.write(servoPosTemp);
- delay(5);
- servoPos=servoPosTemp;
- }
- }
- void sensorRead()
- {
- innerSensor = digitalRead(innerSensorPin);
- outerSensor = digitalRead(gateSensorPin);
- }
- void check_RFID()
- {
- if ( ! mfrc522.PICC_IsNewCardPresent())
- {
- return;
- }
- if ( ! mfrc522.PICC_ReadCardSerial())
- {
- return;
- }
- String content = "";
- for (byte i = 0; i < mfrc522.uid.size; i++)
- {
- content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
- content.concat(String(mfrc522.uid.uidByte[i], HEX));
- }
- content.toUpperCase();
- if (content.substring(1) == "D3 D0 E4 24")
- {
- if (card1Balance >= 500)
- {
- successfullyPaid();
- card1Balance = card1Balance - 500;
- lcd.setCursor(9, 1);
- lcd.print(card1Balance);
- delay(2000);
- lcd.clear();
- paid = true;
- }
- else
- {
- card = content.substring(1);
- failToPay();
- lcd.setCursor(9, 1);
- lcd.print(card1Balance);
- lcd.print(" Tk");
- delay(2000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Please Recharge");
- delay(1000);
- lcd.clear();
- paid = false;
- }
- }
- else if (content.substring(1) == "E3 06 19 D3")
- {
- if (card2Balance >= 500)
- {
- successfullyPaid();
- card2Balance = card2Balance - 500;
- lcd.setCursor(9, 1);
- lcd.print(card2Balance);
- delay(2000);
- lcd.clear();
- paid = true;
- }
- else
- {
- card = content.substring(1);
- failToPay();
- lcd.setCursor(9, 1);
- lcd.print(card2Balance);
- lcd.print(" Tk");
- delay(2000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Please Recharge");
- lcd.clear();
- delay(1000);
- paid = false;
- }
- }
- else{
- digitalWrite(buzzerPin, HIGH);
- lcd.setCursor(0, 0);
- lcd.print("Unknown Vehicle");
- lcd.setCursor(0, 1);
- lcd.print("Access denied");
- delay(1500);
- lcd.clear();
- digitalWrite(buzzerPin, LOW);
- }
- }
- void KeyPad()
- {
- byte KState = KP2.Key_State();
- if (KState == PRESSED)
- {
- Key = KP2.Getkey();
- if (Key)
- {
- if (Key == 'A')
- {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Recharging Mode.");
- lcd.setCursor(0, 1);
- lcd.print("................");
- delay(1500);
- lcd.clear();
- recharge = true;
- }
- }
- }
- }
- void clearData()
- {
- while (data_count != 0)
- {
- Data[data_count--] = 0;
- }
- return;
- }
- void reCharge()
- {
- lcd.setCursor(0, 0);
- lcd.print ("Enter the amount");
- byte KState = KP2.Key_State();
- if (KState == PRESSED)
- {
- Key = KP2.Getkey();
- if (Key)
- {
- if (Key == '#')
- {
- if (card == "D3 D0 E4 24")
- {
- num1 = Data;
- card1Balance = num1.toInt() + card1Balance;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Your current");
- lcd.setCursor(0, 1);
- lcd.print("balance: ");
- lcd.setCursor(9, 1);
- lcd.print (card1Balance);
- lcd.print(" Tk");
- delay(3000);
- clearData();
- lcd.clear();
- recharge = 1;
- }
- else if (card == "E3 06 19 D3")
- {
- num2 = Data;
- card2Balance = num2.toInt() + card2Balance;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Your current");
- lcd.setCursor(0, 1);
- lcd.print("balance: ");
- lcd.setCursor(9, 1);
- lcd.print (card2Balance);
- lcd.print(" Tk");
- delay(3000);
- clearData();
- lcd.clear();
- recharge = 1;
- }
- }
- else
- {
- Data[data_count] = Key;
- lcd.setCursor(data_count, 1);
- lcd.print(Data[data_count]);
- data_count++;
- }
- }
- }
- }
- void successfullyPaid()
- {
- digitalWrite(buzzerPin, HIGH);
- delay(200);
- digitalWrite(buzzerPin, LOW);
- delay(100);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(" Successfully");
- lcd.setCursor(0, 1);
- lcd.print(" paid your bill");
- delay(1500);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Your Remaining");
- lcd.setCursor(0, 1);
- lcd.print("balance: ");
- }
- void failToPay()
- {
- digitalWrite(buzzerPin, HIGH);
- delay(200);
- digitalWrite(buzzerPin, LOW);
- delay(100);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(" Your balance");
- lcd.setCursor(0, 1);
- lcd.print(" is insufficent");
- delay(1500);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Your Remaining");
- lcd.setCursor(0, 1);
- lcd.print("balance: ");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement