Problem with LDR sensors

Hello guys , I'm newbie in Arduino and Programming , I've problem with my project, my project is Parking lot , i'm using LDRs sensor with servo motor to open & close (2 gates) , my parking lot have 5 lots i want my servo cannot open when my parking are full.

I got a problem at [Available--] , when car go in front of gate my LDR not minus only 1 Available , it's minus my Available to zero. (Sorry i'm bad at English hope you guys can help me as soon as possible) Thanks.

Here my code :

#include <Servo.h>
Servo GateIn;
Servo GateOut;

int Available = 5; // create avaliable parking
int LdrIn; //LDR Sensor
int LdrOut;

#define BarUp 120
#define BarLow 45
#define Max 5
#define Min 0

void setup() {
Serial.begin(9600);
GateIn.attach(7);
GateOut.attach(6);

}

void loop() {

LdrIn = analogRead(A0);
LdrOut= analogRead(A1);

Serial.println(Available);

if(LdrIn < 50){
if(Available != Min){
Available--;
GateIn.write(BarUp);
delay(3000);
GateIn.write(BarLow);
}
}
if(LdrOut < 50)
{
if(Available != Max){
Available++;
GateOut.write(BarUp);
delay(3000);
GateOut.write(BarLow);
}
}
}

If the car stays in from of the detector for more than 3 seconds then Available will be decremented again.

You need to act when a car becomes detected not when a car is detected. Have a look at the StateChangeDetection example in the IDE. It uses digital inputs but the principle is the same. Detect when a car is present and was not previously present rather than the fact that a car is present.

I don't quite understand that part that you said, "Detect when a car is present and was not previously present rather than the fact that a car is present." Could you talk me through with more detail?

Have a look at the state change example in the IDE

Please get into the habit of using code tags when posting code.

Could you talk me through with more detail?

What's difficult to understand about UKHeliBob's statement?

Suppose you walk through a real parking lot. There is one car parked in the lot, which can hold 5 cars. You count the one car, and determine that there is plenty of room.

5 minutes later, you walk through the lot. The one car is still there. Do you count it again?

If not, why did you write your Arduino code so that it WILL count the car again?

Kakaroto:
I don't quite understand that part that you said, "Detect when a car is present and was not previously present rather than the fact that a car is present." Could you talk me through with more detail?

Your current code reacts when it detects that a car is present in front of the detector and then takes actions, including decrementing the count of available places. Next time it looks to see whether a car is present in front of the detector it decrements the available count again. What happens though if the car has not moved between the checks ?

What you need to do is to check whether a car is present and whether it was not present the previous time you checked. If not then a new car has been detected and you decrement the count of available spaces. Otherwise it is the same car as the first time you checked so the number of available spaces must not be decremented. There are things to consider when doing this, such as how often you check. You should check as often as possible, ie each time through the loop() function and nothing should delay this. If you delay() between checks then there is a chance that a second car has arrived and should be counted.

To keep the barrier open without using delay(), which stops the program in its tracks, you will need to use millis() for timing. Save the millis() value at the time that the start action happens (gate opens). Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly (close the gate). If not, then go round loop() again, perhaps taking other actions such as looking for the next car, but don't block the free running of loop().