Problem with code looping unnecessary

Good day, can anyone tell why this code is not working. The display kept looping "Welcome all parking full " and "Welcome All Slots left "

// Viral Science www.viralsciencecreativity.com Viral Science - The home of Creativity - YouTube
// Arduino Car Parking System

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); //Change the HEX address
#include <Servo.h>

Servo myservo1;

int IR1 = 2;
int IR2 = 4;

int Slot = 4; //Enter Total number of parking Slots

int flag1 = 0;
int flag2 = 0;

void setup() {
lcd.begin();
lcd.backlight();
pinMode(IR1, INPUT);
pinMode(IR2, INPUT);

myservo1.attach(3);
myservo1.write(100);

lcd.setCursor (0,0);
lcd.print(" ARDUINO'S ");
lcd.setCursor (0,1);
lcd.print(" PARKING SYSTEM ");
delay (3000);
lcd.clear();
}

void loop(){

if(digitalRead (IR1) == LOW && flag1==0){
if(Slot>0){flag1=1;
if(flag2==0){myservo1.write(0); Slot = Slot-1;}
}else{
lcd.setCursor (0,0);
lcd.print("W");
lcd.setCursor (0,1);
lcd.print(" PARKING FULL ");
delay (3000);
lcd.clear();
}
}

if(digitalRead (IR2) == LOW && flag2==0){flag2=1;
if(flag1==0){myservo1.write(0); Slot = Slot+1;}
}

if(flag1==1 && flag2==1){
delay (1000);
myservo1.write(100);
flag1=0, flag2=0;
}

lcd.setCursor (0,0);
lcd.print("WELCOME ALL!");
lcd.setCursor (0,1);
lcd.print("Slot Left: ");
lcd.print(Slot);
}

Kindly edit your code so it is in code tags using the </>

Please unravel this dense mess and put each statement on a separate line. Then use ctrl-T to auto format your code, before posting it.

Have you tested your hardware inputs with simple sketches that, for example, only read an input and report the output? Or did you just jump into this and hope that the sensors are connected and working?

Car_Parking_System_works_viral_science.ino (1.2 KB)

Car_Parking_System_works_viral_science.ino (1.2 KB)

When I hooked up the hrdware, nothing works

Gamal

its printing "WELCOME ..."

But it keeps fluctuating and it does not respond to the hardware.

are the IR sensors active? those conditional statements depend on the IR sensors being LOW, are they?

But it keeps fluctuating and it does not respond to the hardware, so the code might be incorrect. I saw it work on another computer.

The IR sensors are an exit and entrance, so they are always low until a vehicle passes

there are conditional statements that depend on those sensors being LOW. do you believe the code expects them to be LOW "until a vehicle passes"?

This condition should keep them low.

if(digitalRead (IR1) == LOW && flag1==0){
if(Slot>0){flag1=1;
if(flag2==0){myservo1.write(0); Slot = Slot-1;}

the condition executes if the sensor is LOW and flag1 is 0.
the hardware makes it LOW or HIGH.

does your hardware make it LOW when a "vehicle passes"?

Have you tested your hardware inputs with simple sketches that, for example, only read an input and report the output?

The hardware makes it high when a vehicle passes

then the software was designed for different hardware and you'll need to modify the code for your hardware

Thank you , I will look into that.

Or change the hardware to make it LOW when a vehicle passes.

Which you think is easyier.

Do you have more experience with hardware, or software? :slight_smile: