smart parking system coding help needed

My project is a smart parking system.
My project has 4 distance sensors which sense and object and calculate the distance of the object from the sensor. [P1-distance of car from parking 1 sensor, similarly P2,P3,P4]
So, I have 4 sensors for 4 parking slots.
Now when P1<8cm , I want to print on lcd -P2,P3,P4 are free
P2<8cm , I want to print -P1,P3,P4 are free
P3<8cm , I want to print -P1,P2,P4 are free
P4<8cm , I want to print -P1,P2,P3 are free
P1<8cm && P2<8cm , I want to print -P3,P4 are free and so on for all combinations (such that when 3 parking are < 8cm , 1 is free and when all are < 8cm - the parking is full)

i need your help with this coding , i have tried coding it using if and else but i am sure thats not the way to code it nor does it work as the system continues executing every step even when there is no object for the sensors to sense.It displays all the prints on the lcd consecutively.

repsmart.ino (4.9 KB)

test P1, memorize status in an array at entry 0
test P2, memorize status in an array at entry 1
test P3, memorize status in an array at entry 2
test P4, memorize status in an array at entry 3

The just iterate over your array with a for loop and print or not the info, you’ll need to add a bit of smartness if you want commas in the right places

check the attached coding, although this is only for display vacant parking when only one of them is occupied(i need to display the free parkings even if multiple parking are filled in any combinations).
Also in this coding only P4 ie dist[5] sensor is working , others are not working.I have checked the connections and the sensors ,both are correct.
I am not an expert in coding so i need your help with that.

updated_carpark.ino (3.62 KB)

Post code in line, using code tags - won’t download stuff, reading from my tablet (read how to use the forum before posting your answer)

The 4 parking sensors used give distances dist[2],dist[3],dist[4],dist[5].
Only the sensor 4 (dist[5]) senses the object and prints on the lcd while others who have the same code are not working.
Also,this code only prints when 1 parking is occupied but i need a code so that i can print the free parking even when multiple parkings are occupied in any combinations.
example:- p2,p3 are occupied so print p1,p2 free
p1,p3,p4 occupied so print p2 free and so on.

I dont have much knowledge in coding so I will need your help with this.

here is my code,

#include <ST_HW_HC_SR04.h>
#include <LiquidCrystal.h>
#include <Servo.h>

Servo servoEnter;
Servo servoExit;
LiquidCrystal lcd(12,11,5,4,3,2); // set the LCD address to 0x27 for a 16x2 display

//Trig and Echo pins for sonar sensors//
int trigPin1 = 22;
int echoPin1 = 24;
int trigPin2 = 26;
int echoPin2 = 28;
int trigPin3 = 30;
int echoPin3 = 32;
int trigPin4 = 34;
int echoPin4 = 36;
int trigPin5 = 38;
int echoPin5 = 40;
int trigPin6 = 42;
int echoPin6 = 44;





int dist[6]; //used to store distances

int dist_threshold = 8; // if distance<15cm, the slot is occupied. Otherwise, it is free.

 

void setup() {
  // initialize serial communication:
  Serial.begin(9600);


  initSensors();
 

  servoEnter.write(85); //stop servo twitch on startup
  servoExit.write(85);
  servoEnter.write(0);
  servoExit.write(0);

  servoEnter.attach(6);
  servoExit.attach(7);
// set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Smart Parking");
  
}

void loop() {
// get the distances from sonar sensors//
  dist[0] = readsensor(trigPin1, echoPin1);
 


  
  if (dist[0] < dist_threshold) 
  // check the entry  gate
  {
    servoEnter.write(90);
    delay(3000);
    servoEnter.write(0);
    delay(15);
   
  }
   dist[1] = readsensor(trigPin2, echoPin2);

   //check  the exit gate
  if (dist[1] < dist_threshold ) {
    servoExit.write(90);
    delay(3000);
    servoExit.write(0);
    delay(15);
   
  }

  dist[2] = readsensor(trigPin3, echoPin3);




  //parkiing sensors 
  if (dist[2] < dist_threshold) {
    Serial.print("11"); // slot 1 is occupied
    Serial.print('\n');
    lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("P2,P3,P4 free");
}
   
   else {
    Serial.print("10");
    Serial.print('\n');
           lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("All free");
   
  }
  dist[3] = readsensor(trigPin4, echoPin4);
   if (dist[3] < dist_threshold) {
    Serial.print("21");
    Serial.print('\n');
     lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("P1,P3,P4 free");
}
   
   else {
    Serial.print("20");
    Serial.print('\n');
           lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("All free");
   
  }
  
    dist[4] = readsensor(trigPin5, echoPin5);
   if (dist[4] < dist_threshold) {
   Serial.print("31");
   Serial.print('\n');
       lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("P1,P2,P4 free");
  }
  
  
  else {
  Serial.print("30");
   Serial.print('\n');
           lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("All free");
   
  }
  
   dist[5] = readsensor(trigPin6, echoPin6);
  
   if (dist[5] < dist_threshold) {
    Serial.print("41");
   Serial.print('\n');
       lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("P1,P2,P3 free");
  }

   
   
  
  else {
    Serial.print("40");
    Serial.print('\n');
           lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("All free");
   
  }
  
  
  delay(500); // wait for a little time before the next cycle 
}

void initSensors() {
  pinMode(trigPin1, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(trigPin5, OUTPUT);
  pinMode(trigPin6, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(echoPin2, INPUT);
  pinMode(echoPin3, INPUT);
  pinMode(echoPin4, INPUT);
  pinMode(echoPin5, INPUT);
  pinMode(echoPin6, INPUT);


}


int readsensor(int trigPin, int echoPin) {
  //send a pulse to the sensor//
  digitalWrite(trigPin, LOW);
  delayMicroseconds(1000);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  

  long duration = pulseIn(echoPin, HIGH); // measure the time taken to hear the echo
  long cm = duration / 29 / 2; //calculate the distance in cm
  return cm;
}

updated_carpark.ino (3.62 KB)

here is my code

#include <ST_HW_HC_SR04.h>
#include <LiquidCrystal.h>
#include <Servo.h>

Servo servoEnter;
Servo servoExit;
LiquidCrystal lcd(12,11,5,4,3,2); // set the LCD address to 0x27 for a 16x2 display

//Trig and Echo pins for sonar sensors//
int trigPin1 = 22;
int echoPin1 = 24;
int trigPin2 = 26;
int echoPin2 = 28;
int trigPin3 = 30;
int echoPin3 = 32;
int trigPin4 = 34;
int echoPin4 = 36;
int trigPin5 = 38;
int echoPin5 = 40;
int trigPin6 = 42;
int echoPin6 = 44;





int dist[6]; //used to store distances

int dist_threshold = 7.85; // if distance<15cm, the slot is occupied. Otherwise, it is free.

 

void setup() {
  // initialize serial communication:
  Serial.begin(9600);


  initSensors();
 

  servoEnter.write(85); //stop servo twitch on startup
  servoExit.write(85);
  servoEnter.write(0);
  servoExit.write(0);

  servoEnter.attach(6);
  servoExit.attach(7);
// set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Smart Parking");
  
}

void loop() {
// get the distances from sonar sensors//
  dist[0] = readsensor(trigPin1, echoPin1);
 


  
  if (dist[0] < dist_threshold) 
  // check the entry  gate
  {
    servoEnter.write(90);
    delay(3000);
    servoEnter.write(0);
    delay(15);
   
  }
   dist[1] = readsensor(trigPin2, echoPin2);

   //check  the exit gate
  if (dist[1] < dist_threshold ) {
    servoExit.write(90);
    delay(3000);
    servoExit.write(0);
    delay(15);
   
  }

  dist[2] = readsensor(trigPin3, echoPin3);




  //parkiing sensors 
  if (dist[2] < dist_threshold) {
    Serial.print("11"); // slot 1 is occupied
    Serial.print('\n');
    lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("P2,P3,P4 free");
}
   
   else {
    Serial.print("10");
    Serial.print('\n');
           lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("All free");
   
  }
  dist[3] = readsensor(trigPin4, echoPin4);
   if (dist[3] < dist_threshold) {
    Serial.print("21");
    Serial.print('\n');
     lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("P1,P3,P4 free");
}
   
   else {
    Serial.print("20");
    Serial.print('\n');
           lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("All free");
   
  }
  
    dist[4] = readsensor(trigPin5, echoPin5);
   if (dist[4] < dist_threshold) {
   Serial.print("31");
   Serial.print('\n');
       lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("P1,P2,P4 free");
  }
  
  
  else {
  Serial.print("30");
   Serial.print('\n');
           lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("All free");
   
  }
  
   dist[5] = readsensor(trigPin6, echoPin6);
  
   if (dist[5] < dist_threshold) {
    Serial.print("41");
   Serial.print('\n');
       lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("P1,P2,P3 free");
  }

   
   
  
  else {
    Serial.print("40");
    Serial.print('\n');
           lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("All free");
   
  }
  
  
  delay(500); // wait for a little time before the next cycle 
}

void initSensors() {
  pinMode(trigPin1, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(trigPin5, OUTPUT);
  pinMode(trigPin6, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(echoPin2, INPUT);
  pinMode(echoPin3, INPUT);
  pinMode(echoPin4, INPUT);
  pinMode(echoPin5, INPUT);
  pinMode(echoPin6, INPUT);


}


int readsensor(int trigPin, int echoPin) {
  //send a pulse to the sensor//
  digitalWrite(trigPin, LOW);
  delayMicroseconds(1000);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  

  long duration = pulseIn(echoPin, HIGH); // measure the time taken to hear the echo
  long cm = duration / 29 / 2; //calculate the distance in cm
  return cm;
}

int dist_threshold = [color=red]7.85[/color]; => does not really looks like an int to me....

[color=red]int[/color] readsensor()
{
...
  [color=red]long cm[/color]= duration / 29 / 2; //calculate the distance in cm
  return cm;
}

Same comment here, bad type

Your code does not meet what I asked in #1
Start with just that, get rid of everything else

yup changed it to 8 and i didnt get what you asked me to do in #1

Please contact a moderator to request that your two threads be merged.

@shubhamjain28, do not cross-post. Threads merged.

shubhamjain28:
yup changed it to 8 and i didnt get what you asked me to do in #1

Imagine you have an array with 5 boolean that can be either true or false

const boolean parkingIsAvailable[] = {false, true, true, false, false};

Exercise 1: Write a program which goes through the array and prints the index of only the entries which are true, one per line. Do all this in setup(), keep loop() empty. for the array above your console output should look like this (remember array index starts at 0)

[sub][color=blue]
Available Parking at index:
1
2
[/color][/sub]

Exercise 2: now put the answer on only one line: improve the previous code by adding punctuation => a comma needs to be printed to separate the true indexes - bring careful you don’t want a comma at the end of the list. Do all this in setup(), keep loop() empty

For the array above your console output should look like this (remember array index starts at 0)

[sub][color=blue]
Available Parking at index: 1, 2
[/color][/sub]

try by changing the array to

const boolean parkingIsAvailable[] = {false, true, true, false, true};

and

const boolean parkingIsAvailable[] = {true, false, true, false, true};

for example to ensure all works fine

Exercise 3: change the code from exercise 2 to improve a bit more because human don't usually count from 0 like computers but from 1 and add a letter in front of the digit, so array index 0 should appear as P1 the output to look like this (for the first array above)

[sub][color=blue]
Available Parking: P2, P3
[/color][/sub]

Exercise 4: change the code from exercise 3 to dynamically build the array from your sensors (so array can’t be const anymore). Add a function taking a trigger pin and sensor pin as parameter, returning a boolean. fill up the array in setup(), keep loop() empty

Exercise 5: make this now the real thing, so that it keeps looking Every 5 seconds to the status of the parking and print out the results

Just saw your post,
Here’s what i did yesterday.
I think it still has some issue with print.

  dist[0] = readsensor(trigPin3, echoPin3); // parking distance for p1

  dist[1] = readsensor(trigPin4, echoPin4); // p2
 
  dist[2] = readsensor(trigPin5, echoPin5);
 
  dist[3] = readsensor(trigPin6, echoPin6);

  bool slot[4];
  int i=0,j=0;
  for(i=0;i<4;i++){
    if (dist[i]>8){
    slot[i]=false;
    }
    else{
    slot[i]=true;
    }
  }

  const char*str;
  for(i=0;i<4;i++){
    if(slot[i]){
      str=strcat("P",(char*)i);
    }
  }
  strcat(str,"Are Free");
  lcd.setCursor(0,1);
  lcd.print(str);
  const char*str;
  for(i=0;i<4;i++){
    if(slot[i]){
      str=strcat("P",(char*)i);
    }
  }
  strcat(str,"Are Free")

you cannot write a c string to a pointer, you need a buffer big enough to accept all of the chars:

  char str[SOME_BIG_ENOUGH_SIZE];
  for(i=0;i<4;i++){
    if(slot[i]){
      //str=strcat("P",(char*)i); don't know what you are trying to do there
    }
  }
  strcat(str,"Are Free");  // appends str with the literal "Are Free"

str=strcat(“P”,(char*)i);
doing this to print P1 or P2 or P1,P3 …before “are free”
to display the free parkings.
eg:- if p1<8cm
// it means parking is occupied
so it should print
p2,p3,p4 are free

then

for(i = 0; i < 4 ;i++)
{
    if(slot[i])
  {
     char spot[4];
     sprintf(spot, "P%d,", i);
     strcat(str, spot);
  }
}

made the recommended changes (the updated code below)
but now the parking sensors wont sense the object and display on the lcd.
also for some reason the motors which are used for entry and exit gates sometime behave abnormally by working together even though the car is at entry gate only.

before posting gave it a second try its printed some abnormal characters with “are free”.
third try
not printing and entry exit sensors stopped detecting.

#include <ST_HW_HC_SR04.h>
#include <LiquidCrystal.h>
#include <Servo.h>

Servo servoEnter;
Servo servoExit;
LiquidCrystal lcd(12,11,5,4,3,2); // set the LCD address to 0x27 for a 16x2 display

//Trig and Echo pins for sonar sensors//
int trigPin1 = 22;
int echoPin1 = 24;
int trigPin2 = 26;
int echoPin2 = 28;
int trigPin3 = 30;
int echoPin3 = 32;
int trigPin4 = 34;
int echoPin4 = 36;
int trigPin5 = 38;
int echoPin5 = 40;
int trigPin6 = 42;
int echoPin6 = 44;




int distm[2]; //used to store distances for motor/gate sensors
int dist[4]; // for parking

int dist_threshold = 8; // if distance<15cm, the slot is occupied. Otherwise, it is free.





 

void setup() {
  // initialize serial communication:
  Serial.begin(9600);

  pinMode(trigPin1, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(trigPin5, OUTPUT);
  pinMode(trigPin6, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(echoPin2, INPUT);
  pinMode(echoPin3, INPUT);
  pinMode(echoPin4, INPUT);
  pinMode(echoPin5, INPUT);
  pinMode(echoPin6, INPUT);

 

  servoEnter.write(85); //stop servo twitch on startup
  servoExit.write(85);
  servoEnter.write(0);
  servoExit.write(0);

  servoEnter.attach(6);
  servoExit.attach(7);
// set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  lcd.setCursor(1,0);
  lcd.print("Smart Parking");
  delay (100);
  
}

void loop() {
// get the distances from sonar sensors for the gates
  dist[0] = readsensor(trigPin1, echoPin1);
 


  
  if (distm[0] < dist_threshold) 
  // check the entry  gate
  {
    servoEnter.write(90);
    delay(3000);
    servoEnter.write(0);
    delay(15);
   
  }
   distm[1] = readsensor(trigPin2, echoPin2);

   //check  the exit gate
  if (distm[1] < dist_threshold ) {
    servoExit.write(90);
    delay(3000);
    servoExit.write(0);
    delay(15);
   
  }
  // parking sensors

  dist[0] = readsensor(trigPin3, echoPin3); // parking distance for p1

  dist[1] = readsensor(trigPin4, echoPin4); // p2
 
  dist[2] = readsensor(trigPin5, echoPin5);
 
  dist[3] = readsensor(trigPin6, echoPin6);

  bool slot[4];
  int i=0,j=0;
  for(i=0;i<4;i++){
    if (dist[i]>8){
    slot[i]=false;
    }
    else{
    slot[i]=true;
    }
  }
  char str[5];

  for(i = 0; i < 4 ;i++)
{
    if(slot[i])
  {
     char spot[4];
     
     sprintf(spot, "P%d,", i);
     strcat(str, spot);
     delay(200);
  }
}
  
  strcat(str,"Are Free");
  lcd.setCursor(1,0);
  lcd.print(str);
  
 
 
  
  
  delay(500); // wait for a little time before the next cycle 
}



int readsensor(int trigPin, int echoPin) {
  //send a pulse to the sensor//
  digitalWrite(trigPin, LOW);
  delayMicroseconds(1000);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  

  long duration = pulseIn(echoPin, HIGH); // measure the time taken to hear the echo
  long cm = duration / 29 / 2; //calculate the distance in cm
  return cm;
}

What is going on

With all those white

Lines spaces in the code?

And why don’t you properly indent the code? press the ctrl-T keys from time to time, your code will read better and make it easier for us to read it.

Why don’t the servo sensors PINs have a small meaningful name ?

Why didn’t you fix the long versus int return value I mentioned in #6 for readsensor()?

I won’t answer anymore question if your code is not properly presented, and remarks not taken into account. I consider this lack of respect.

To the code itself:

In the first part of the loop():

dist[0] = readsensor(trigPin1, echoPin1);
  
  if (distm[0] < dist_threshold) 
  // check the entry  gate
  {
    servoEnter.write(90);
    delay(3000);
    servoEnter.write(0);
    delay(15);
   
  }
   distm[1] = readsensor(trigPin2, echoPin2);

   //check  the exit gate
  if (distm[1] < dist_threshold ) {
    servoExit.write(90);
    delay(3000);
    servoExit.write(0);
    delay(15);
   
  }

what are those supposed to do - letting cars in or out by opening and closing the gate I suppose?

—> the delay will kill the parallel nature of the rest of your code, no one can exit while someone enters for example or status won’t update if someone is parking whilst someone is exiting… not a very smart parking. You Need to build a state machine to handle that, you should have no call to significant delay() in the code.

This part

bool slot[4];
  int i=0,j=0;
  for(i=0;i<4;i++){
    if (dist[i]>8){
    slot[i]=false;
    }
    else{
    slot[i]=true;
    }

what is j used for?

Why is 8 not a constant with a meaningful name?

Why isn’t that done directly when acquiring the distance since you don’t use the distance besides building this status true/false table? If you had done exercise 4 above…

also slot[n] is true when sensor is <= 8 which to me means something is detected near the sensor right? (probably a car is on that spot) → so slot tells you if it is busy, not if it if free. The variable name should reflect that isParkingSlotOccupied would be a much better name (cf my exercise if you had given a bit of attention there, you would have seen the use of parkingIsAvailable as the array name and learnt a best practice about naming variable with some meaningful name…)

*** Now To your question for why it behaves weirdly ***

In this code

char str[5];

  for(i = 0; i < 4 ;i++)
{
    if(slot[i])
  {
     char spot[4];
     
     sprintf(spot, "P%d,", i);
     strcat(str, spot);
     delay(200);
  }
}

How many bytes did you set aside/allocate for the whole str?
how many bytes are you trying to copy into str?
is that a big problem?

Also once this is fixed, note that you list BUSY slots with this, not free ones as per the above comment - which seemed the opposite of your requirement

PS: I have seen none of the exercise I had asked in #10… you are rushing to the full thing and don’t take time to build up skills and structure thinking

First of all, I am an electrical engineering student with very basic knowledge of C.So,half of the things you ask me Like the boolean thing and long with int return value. I have no idea about them.I have trying my best searching here and there and trying to write the code.
Servo has 3000 delay so that it wait 3 sec before closing it.
The parallel operation of the entry and exit gates works fine.
Regarding the bytes required for str(same no clue) and I know it is listing the busy slots because i wasnt able to write it the other way round.
I greatly appreciate that you are trying to help but you I am trying my best here too trying to somehow understand your suggestions and then implementing them.If I had strong programming skills I would have understood them and since I am running out of time and stuck at the very end of this project, I am in a bit of hurry.

This forum is meant to help people progress, not write the project for you especially if this is a work you need to do for school. This would be cheating, unfair to your colleagues and helping lazy engineers get their diploma which will cause issue in our world future when such engineers will have to come up with solutions... (not saying this is who you are)

Clearly you are expected to build up programming skills as part of your curriculum, right? (otherwise you wouldn’t have such a project) and my feedback was that you should start with simple problems and serializing issues. In my opinion you are not spending time wisely acquiring fundamental skills and hoping that throwing code at this randomly will,work... it won’t...

The parallel operation of the entry and exit gates works fine.

Define fine?

=> If you have a car arriving at the entry gate it will open and the code does wait for 3 seconds. If you have a car arriving at the exit gate during those 3 seconds, I guarantee the gate won’t open until the entry is closed. If you have a car parking during those 3 seconds and a car parking at the same Time, the sensor won’t update. Note that 3 seconds to go through a barrier is super short... closing the gate whith the car under the barrier will be a costly liability...

Regarding the bytes required for str(same no clue)

So you writechar str[5];just randomly? no clue why 5 and not 567? What does this instruction do? Have you read about arrays and strings in c language?

and I know it is listing the busy slots because i wasnt able to write it the other way roun

Seriously? Did you write that code

if (dist[i]>8){
    slot[i]=false;
    }
    else{
    slot[i]=true;
    }

and you have no clue how to put true instead of false when the distance is less that 8 rather than higher than 8? Is that rocket science or you don’t understand what an if statement does?

since I am running out of time and stuck at the very end of this project, I am in a bit of hurry.

I Don’t think you are at the very end for a smart parking

The think is you should never jump to conclusions without knowing anything about a person.This project is a mixed group project my electrical part has already been completed by me and the part which i am asking in this forum was supposed to be done by the CS department student ,but clearly since he wasnt doing it ,I am trying my best so that i can complete it.

Fine means its working properly , since I have two seperate sensors and motors for the two gates, i can open them both simultaneously.But if you still say that its not possible due to the coding then even I dont know how its happening.
Regarding str[5], i know array starts from 0 and stores value in each position , char array stores a characted in each position.
And string can be used to store a sentence or even characters, but when i used to use C i remeber i used to use array without specifying limit example array, but this gave me an error in arduino ide
So I thought of keeping it 4 the same as slot but then gave it 5.

Regarding if statement, I saw some examples on boolean and concluded from them that they work only for the true condition
Under the true condition its < 8 so its means the parking slot is occupied which is what i wanted in the first place so that i can print that the other parkings are free.