//+228 98464143

#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
int distanceIN, distanceOUT, distance1, distance2, distance3, distance4, distance5;
Servo porteIN;
Servo porteOUT;

int v1=38;
int r1=39;
int v2=40;
int r2=41;
int v3=42;
int r3=43;
int v4=44;
int r4=45;
int v5=46;
int r5=47;

int led=50;
int ldr=49;
int etat;

int trigIN=22;
int echoIN=23;

int trigOUT=24;
int echoOUT=25;

int trig1=26;
int echo1=27;

int trig2=28;
int echo2=29;

int trig3=30;
int echo3=31;

int trig4=32;
int echo4=33;

int trig5=34;
int echo5=35;


int total, slot;

int S1 = 0, S2 = 0, S3 = 0, S4 = 0, S5=0;
//int flag1 = 0, flag2 = 0;
int place = 5;

void setup() {
  Serial.begin(9600);
  pinMode(trigIN, OUTPUT);
  pinMode(trigOUT, OUTPUT);
  pinMode(trig1, OUTPUT);
  pinMode(trig2, OUTPUT);
  pinMode(trig3, OUTPUT);
  pinMode(trig4, OUTPUT);
  pinMode(trig5, OUTPUT);
  for(int i=38; i<48; i++){pinMode(i, OUTPUT);}
  
  pinMode(echoIN, INPUT);
  pinMode(echoOUT, INPUT);
  pinMode(echo1, INPUT);
  pinMode(echo2, INPUT);
  pinMode(echo3, INPUT);
  pinMode(echo4, INPUT);
  pinMode(echo5, INPUT);

  pinMode(ldr, INPUT);
  pinMode(led, OUTPUT);


  porteIN.attach(7);
  porteOUT.attach(8);
  porteIN.write(90);
  porteOUT.write(90);


  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 1);
  lcd.print("      BIENVENUE ");
  lcd.setCursor(0, 2);
  lcd.print(" A NOTRE SOUTENANCE");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" Notre projet est le");
  lcd.setCursor(0, 1);
  lcd.print(" systeme de Parking ");
  lcd.setCursor(0, 2);
  lcd.print("   Automatique et  ");
  lcd.setCursor(0, 3);
  lcd.print("    Intelligent  ");
  delay(3000);
  lcd.clear(); 
  
  Read_Sensor();

  
}
void loop() {

  Read_Sensor();
  crepuscule();

  int total = S1 + S2 + S3 + S4 + S5;
  slot = place - total;

  lcd.setCursor(0, 0);
  lcd.print("Place disponible: ");
  lcd.print(slot);
  lcd.print("    ");

  lcd.setCursor(0, 1);
  if (S1 == 1) {
    lcd.print("P1:Occupe ");
    digitalWrite(v1, LOW);
    digitalWrite(r1, HIGH);
  } else {
    lcd.print("P1:Libre ");
    digitalWrite(v1, HIGH);
    digitalWrite(r1, LOW);
  }

  lcd.setCursor(11, 1);
  if (S2 == 1) {
    lcd.print("P2:Occupe ");
    digitalWrite(v2, LOW);
    digitalWrite(r2, HIGH);
  } else {
    lcd.print("P2:Libre ");
    digitalWrite(v2, HIGH);
    digitalWrite(r2, LOW);
  }

  lcd.setCursor(0, 2);
  if (S3 == 1) {
    lcd.print("P3:Occupe ");
    digitalWrite(v3, LOW);
    digitalWrite(r3, HIGH);
  } else {
    lcd.print("P3:Libre ");
    digitalWrite(v3, HIGH);
    digitalWrite(r3, LOW);
  }

  lcd.setCursor(11, 2);
  if (S4 == 1) {
    lcd.print("P4:Occupe ");
    digitalWrite(v4, LOW);
    digitalWrite(r4, HIGH);
  } else {
    lcd.print("P4:Libre ");
    digitalWrite(v4, HIGH);
    digitalWrite(r4, LOW);
  }

  lcd.setCursor(0, 3);
  if (S5 == 1) {
    lcd.print("P5:Occupe ");
    digitalWrite(v5, LOW);
    digitalWrite(r5, HIGH);
  } else {
    lcd.print("P5:Libre ");
    digitalWrite(v5, HIGH);
    digitalWrite(r5, LOW);
  }
  

    if (slot > 0) {
      if (distanceIN<15) {
      porteIN.write(180);
      }
      else {
        porteIN.write(90);
      }
    }
    else if(slot==0) {
      if (distanceIN<15) {
      //porteIN.write(90);
      lcd.setCursor(0, 0);
      lcd.print(" Svp Parking plein ");
      delay(2000);
      }
      //else{
      //  porteIN.write(90);
      //}
  }

  if (distanceOUT<15) {
    porteOUT.write(180);
  }
  else{
    porteOUT.write(90);
  }
  delay(1);
}

void crepuscule(){
  etat=digitalRead(ldr);
  if(etat==HIGH) digitalWrite(led,HIGH);
  else digitalWrite(led,LOW);
}


void Read_Sensor() {

  digitalWrite(trigIN,LOW);
  delayMicroseconds(2);
  digitalWrite(trigIN,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigIN,LOW);
  distanceIN=pulseIn(echoIN,HIGH)/58.0;

  digitalWrite(trigOUT,LOW);
  delayMicroseconds(2);
  digitalWrite(trigOUT,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigOUT,LOW);
  distanceOUT=pulseIn(echoOUT,HIGH)/58.0;

  digitalWrite(trig1,LOW);
  delayMicroseconds(2);
  digitalWrite(trig1,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig1,LOW);
  distance1=pulseIn(echo1,HIGH)/58.0;

  digitalWrite(trig2,LOW);
  delayMicroseconds(2);
  digitalWrite(trig2,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig2,LOW);
  distance2=pulseIn(echo2,HIGH)/58.0;

  digitalWrite(trig3,LOW);
  delayMicroseconds(2);
  digitalWrite(trig3,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig3,LOW);
  distance3=pulseIn(echo3,HIGH)/58.0;

  digitalWrite(trig4,LOW);
  delayMicroseconds(2);
  digitalWrite(trig4,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig4,LOW);
  distance4=pulseIn(echo4,HIGH)/58.0;

  digitalWrite(trig5,LOW);
  delayMicroseconds(2);
  digitalWrite(trig5,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig5,LOW);
  distance5=pulseIn(echo5,HIGH)/58.0;

  
  S1 = 0, S2 = 0, S3 = 0, S4 = 0, S5 = 0;

  if (distance1<10) {
    S1 = 1;
  }
  if (distance2<10) {
    S2 = 1;
  }
  if (distance3<10) {
    S3 = 1;
  }
  if (distance4<10) {
    S4 = 1;
  }
  if (distance5<10) {
    S5 = 1;
  }
}