#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
#define ir_enter 2
#define ir_back 4
#define ir_car1 8
#define ir_car2 9
int S1 = 0, S2 = 0;
int flag1 = 0, flag2 = 0;
int slot = 2;
int RedLed=11;
int GreenLed=12;
void setup() {
Serial.begin(9600);
pinMode(ir_car1, INPUT);
pinMode(ir_car2, INPUT);
pinMode(ir_enter, INPUT);
pinMode(ir_back, INPUT);
pinMode(RedLed, OUTPUT);
pinMode(GreenLed, OUTPUT);
myservo.attach(3);
myservo.write(90);
lcd.begin();
lcd.setCursor(2, 0);
lcd.print("A Final Year");
lcd.setCursor(0, 1);
lcd.print("Project By P.M");
delay(3000);
lcd.clear();
delay(1000);
Read_Sensor();
int total = S1 + S2;
slot = slot - total;
}
void loop() {
Read_Sensor();
lcd.setCursor(0, 0);
lcd.print("Available slot:");
lcd.print(slot);
lcd.print(" ");
lcd.setCursor (0,1);
if (S1 == 1) {
lcd.print("S1:Fill ");
digitalWrite(GreenLed, LOW);
digitalWrite(RedLed, HIGH);
}
else {
lcd.print("S1:Empty");
digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, LOW);
}
lcd.setCursor (8,1);
if (S2 == 1) {
lcd.print("S2:Fill ");
digitalWrite(GreenLed, LOW);
digitalWrite(RedLed, HIGH);
}
else {
lcd.print("S2:Empty");
digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, LOW);
if (S1==1 && S2==1) {
digitalWrite(GreenLed, LOW);
digitalWrite(RedLed, HIGH);
}
else {
digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, LOW);
}
}
if (digitalRead (ir_enter) == 0 && flag1 == 0) {
if (slot > 0) {
flag1 = 1;
if (flag2 == 0) {
myservo.write(90);
slot = slot - 1;
}
} else {
lcd.setCursor (0,0);
lcd.print(" Sorry No Space! ");
delay(1500);
}
}
if (digitalRead (ir_back) == 0 && flag2 == 0) {
flag2 = 1;
if (flag1 == 0) {
myservo.write(90);
slot = slot + 1;
}
}
if (flag1 == 1 && flag2 == 1) {
delay(1000);
myservo.write(90);
flag1 = 0, flag2 = 0;
}
delay(1);
}
void Read_Sensor() {
S1 = 0, S2 = 0;
if (digitalRead(ir_car1) == 0) {
S1 = 1;
}
if (digitalRead(ir_car2) == 0) {
S2 = 1;
}
}
Good day to you all. Please, someone should help me solve this problem with my coding. When the parking slot1 and slot2 are empty LED (green) will be high. But when the slot1 and slot2 are occupied LED (red) will be high and the green LED will be low. But in my own case, when the slot1 or slot2 is occupied, both the LED's (red and green) will be high.
Abstract the problem by calling slots empty or full, and LEDs on or off, then describe the desired functionality. For example, problem restatement:
if both slots are empty, turn on the green LED, turn off the red LED
if both slots are full, turn on the red LED, turn off the green LED
if one slot is full, one is empty, turn on both LEDS
Is that the desired result? Or something else?
No, it is not the desire result. What we want to achieve is, if slot one or slot two is full, only green LED will be ON, unless both the slots are full, then red LED will ON and green LED goes OFF.
That’s easy use an if(1 and 2) red, else green/ to make the choice.
Yes. Only red if both slots occupied, otherwise green.
But what means this?
There are only four conditions total. You can write an if statement for each of the four that says what LEDs are what, complete flexibility.
a7
The formatting of your code is hurting your effort. You missed a close brace...
lcd.setCursor (8, 1);
if (S2 == 1) {
lcd.print("S2:Fill ");
digitalWrite(GreenLed, LOW);
digitalWrite(RedLed, HIGH);
}
else {
lcd.print("S2:Empty");
digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, LOW);
// <-- YOU SHOULD HAVE A CLOSE BRACE HERE
if (S1 == 1 && S2 == 1) {
digitalWrite(GreenLed, LOW);
digitalWrite(RedLed, HIGH);
}
else {
digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, LOW);
}
} // <--- BUT THEN YOU NEED TO REMOVE THE LAST OF THESE CLOSE BRACES
(the "slot" logic is upside down)
Hi, @badisaidriss
Can you please post a schematic?
What are you using to detect the presence of a car?
Link to specs/data.
Can you please post some of images of your project?
Thanks... Tom....
![]()
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
