geo_18
Newbie level 5
- Joined
- Sep 22, 2013
- Messages
- 8
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 59
This program is just to control a car parking lot using ping sensors and servo motor.
Servo motor acts as the gate. Here when the first ping sensor detects the car the gate opens and when the car crosses the second ping sensor the gate closes and correspondingly the number of parking lots left are shown on a lcd screen. That is how the program is programmed. But now i have encountered a situation where the car cuts the first ping sensor and the gate opens, but the car decides not to enter the parking lot and goes back. So I want my gate to close. Could anyone suggest me the correct logical modification to this program to handle such a situation.
This is the program :
Servo motor acts as the gate. Here when the first ping sensor detects the car the gate opens and when the car crosses the second ping sensor the gate closes and correspondingly the number of parking lots left are shown on a lcd screen. That is how the program is programmed. But now i have encountered a situation where the car cuts the first ping sensor and the gate opens, but the car decides not to enter the parking lot and goes back. So I want my gate to close. Could anyone suggest me the correct logical modification to this program to handle such a situation.
This is the program :
#include<Servo.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int pingPinTRI1 = 7;
int pingPinECHO1 = 8;
int pingPinTRI2 = 9;
int pingPinECHO2 = 10;
Servo myservo;
Servo myservo1;
Servo myservo2;
int avlb=15;
void setup()
{
myservo.attach(6);
myservo1.attach(9);
myservo2.attach(7);
pinMode(pingPinTRI1, OUTPUT);
pinMode(pingPinECHO1, INPUT);
pinMode(pingPinTRI2, OUTPUT);
pinMode(pingPinECHO2, INPUT);
lcd.begin(16,2);
Serial.begin(9600);
}
void loop()
{
display(avlb);
long duration1, cm1 , duration2 , cm2 ;
digitalWrite(pingPinTRI1, LOW);
delayMicroseconds(2);
digitalWrite(pingPinTRI1, HIGH);
delayMicroseconds(5);
digitalWrite(pingPinTRI1, LOW);
digitalWrite(pingPinTRI2, LOW);
delayMicroseconds(2);
digitalWrite(pingPinTRI2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPinTRI2, LOW);
duration1=pulseIn(pingPinECHO1, HIGH);
cm1=microsecondsToCentimeters(duration1);
if(cm1>1 && cm1<5)
{
if(avlb!=0)
{
myservo.write(177);
myservo1.write(177);
jump1:
duration2=pulseIn(pingPinECHO2, HIGH);
cm2=microsecondsToCentimeters(duration2);
if(cm2>1 && cm2<5)
{
myservo.write(110);
avlb--;
display(avlb);
}
else
{
goto jump1;
}
}
delay(5000);
}
duration2=pulseIn(pingPinECHO2, HIGH);
cm2=microsecondsToCentimeters(duration2);
if(cm2>1 && cm2<5)
{
if(avlb!=15)
{
myservo.write(177);
myservo2.write(177);
jump2:
duration1=pulseIn(pingPinECHO1, HIGH);
cm1=microsecondsToCentimeters(duration1);
if(cm1>1 && cm1<5)
{
myservo.write(110);
avlb++;
display(avlb);
}
else
{
goto jump2;
}
delay(5000);
}
}
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
void display(int lots)
{
lcd.print("no.of lots ");
lcd.setCursor(0,1);
lcd.print("available : ");
lcd.print(lots);
}