Arduino Car Parking system

Hello, I have made a car parking system with arduino nano. It is not responding to any commands.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address (0x27) to match your LCD
Servo myservo1;

int ir_s1 = 17;
int ir_s2 = 16;

int Total = 6;
int Space;

int flag1 = 0;
int flag2 = 0;

int greenLed = 6; // Green LED connected to pin 6
int redLed = 7;   // Red LED connected to pin 7

void setup() {
  pinMode(ir_s1, INPUT);
  pinMode(ir_s2, INPUT);
  
  myservo1.attach(3);
  myservo1.write(100);

  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);

  lcd.begin(16, 2);
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("  Car  Parking  ");
  lcd.setCursor(0, 1);
  lcd.print("     System     ");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("       By       ");
  lcd.setCursor(0, 1);
  lcd.print("  Samar Gosalia  ");
  delay(2000);
  lcd.clear();

  Space = Total;
}

void loop() {
  if (digitalRead(ir_s1) == LOW && flag1 == 0) {
    if (Space > 0) {
      flag1 = 1;
      if (flag2 == 0) {
        myservo1.write(0);
        Space = Space - 1;
      }
    }
    else {
      lcd.setCursor(0, 0);
      lcd.print("Sorry No Space");
      lcd.setCursor(0, 1);
      lcd.print("    Available :( ");
      digitalWrite(redLed, HIGH);    // Turn on the red LED
      digitalWrite(greenLed, LOW);   // Turn off the green LED
      delay(1000);
      lcd.clear();
    }
  }

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

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

  lcd.setCursor(0, 0);
  lcd.print("Total Space: ");
  lcd.print(Total);

  lcd.setCursor(0, 1);
  lcd.print("Space Left: ");
  lcd.print(Space);

  if (Space > 0) {
    digitalWrite(greenLed, HIGH);   // Turn on the green LED
    digitalWrite(redLed, LOW);      // Turn off the red LED
  }
  else {
    digitalWrite(greenLed, LOW);    // Turn off the green LED
    digitalWrite(redLed, HIGH);     // Turn on the red LED
  }
}

Note: I am not using the LCD.

have a read of how-to-get-the-best-out-of-this-forum
in particular what does it do - what should it do?
post a schematic of the circuit

It should open the gate and then close it but its not doing that.

Please read #2 again.What is "IR sensor"? Datasheet please.

Datasheet of IR Sensor.pdf (1.1 MB)

Data Sheet

Are you using the Serial Monitor for output?

No. I am not using any output.

Rather than using "if"... use x = digitalRead(ir_s1)... then us if (x...... so you can print "x" to the Serial Monitor to see when/if it becomes 0 or 1.

in addition to writing to the LCD write to the Serial monitor

you could be overloading the power supply of the UNO
try without the servo connected

Refer below car detector guides :

##Arduino Speed Detector with IR Sensor on LCD Display

Car Speed Detector with Arduino and IR Sensors in Proteus

@microdigisoft - NO.
@horace - [edit] yes, a servo withuot a power source can cause problems.

@Samar2013 - Do not disassemble your projects before checking available indicators (using Serial Monitor).

Your schematic is incomplete - there is no power supply.

What do you use for power source and how is it connected to the rest of the circuit?

Your code is using the LCD but in #0 you say you're NOT using the LCD. So are you using an LCD or not?

(no LCD, no Serial Monitor, only observing servo movement)

Use the serial monitor so you can print the values of key variables and confirm that they are properly informing the flow through your process.

Just looking at the servos you are depriving yourself of all kinds of additional evidence or help to see why it does or doesn't do what you think it should.

This for one would let you "see" the sketch functioning even if you took the servo out of the picture to temporarily eliminate the issues that attaching a servo to an Arduino can cause.

a7

@Samar2013 - Just like your obstacle avoiding car, you need to make one device work before adding another device.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.