HolyC0w

lab4_parkingsystem1

Apr 11th, 2023
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int LEDpin = 13;
  2. int obstaclePin = 7;
  3. int hasObstacle = LOW;
  4.  
  5. const int trigPin = 9;
  6. const int echoPin = 10;
  7.  
  8. const int motorPin1  = 5;  
  9. const int motorPin2  = 6;
  10.  
  11. int gate = 0; //0 is closed 1 is open
  12. int parking = 0; //0 is empty and 1 is parked
  13.  
  14. long duration;
  15. int distance;
  16. void gate_close(){
  17.   Serial.println("Gate Closed");
  18.   gate = 0;
  19.   digitalWrite(motorPin1, LOW);
  20.   digitalWrite(motorPin2, HIGH);
  21.   delay(2000);
  22.   digitalWrite(motorPin1, LOW);
  23.   digitalWrite(motorPin2, LOW);
  24. }
  25.  
  26. void gate_open(){
  27.   Serial.println("Gate Opened");
  28.   gate = 1;
  29.   digitalWrite(motorPin1, HIGH);
  30.   digitalWrite(motorPin2, LOW);
  31.   delay(2000);
  32.   digitalWrite(motorPin1, LOW);
  33.   digitalWrite(motorPin2, LOW);
  34. }
  35.  
  36.  
  37. void setup() {
  38.   pinMode(LEDpin, OUTPUT);
  39.   pinMode(obstaclePin, INPUT);
  40.   pinMode(trigPin,OUTPUT);
  41.   pinMode(echoPin,INPUT);
  42.   pinMode(motorPin1, OUTPUT);
  43.   pinMode(motorPin2, OUTPUT);
  44.  
  45.   digitalWrite(motorPin1, LOW);
  46.   digitalWrite(motorPin2, LOW);
  47.  
  48.   Serial.begin(9600);
  49. }
  50. void loop() {
  51.  
  52.   digitalWrite(trigPin,LOW);
  53.   delayMicroseconds(2);
  54.   digitalWrite(trigPin,HIGH);
  55.   delayMicroseconds(10);
  56.   digitalWrite(trigPin,LOW);
  57.   duration = pulseIn(echoPin,HIGH);
  58.   distance = duration * 0.034/2;
  59.   Serial.print("Distance");
  60.   Serial.println(distance);
  61.  
  62.  
  63.   if (distance<10) {
  64.     if(gate!=1){
  65.       Serial.println("Car arrived");
  66.       gate_open();
  67.     }
  68.   }
  69.   else
  70.   {
  71.     if(gate==1){
  72.       gate_close();
  73.     }
  74.   }
  75.   hasObstacle = digitalRead(obstaclePin);
  76.  
  77.   if (hasObstacle == LOW) {
  78.     if(parking!=1){
  79.       parking = 1;
  80.       gate_close();
  81.       Serial.println("Car is Parked");
  82.       digitalWrite(LEDpin, HIGH);
  83.     }
  84.   }  
  85.   else{
  86.     digitalWrite(LEDpin, LOW);
  87.     parking = 0;
  88.   }  
  89.  
  90.   delay(2000);
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment