made the recommended changes (the updated code below)
but now the parking sensors wont sense the object and display on the lcd.
also for some reason the motors which are used for entry and exit gates sometime behave abnormally by working together even though the car is at entry gate only.
before posting gave it a second try its printed some abnormal characters with "are free".
third try
not printing and entry exit sensors stopped detecting.
#include <ST_HW_HC_SR04.h>
#include <LiquidCrystal.h>
#include <Servo.h>
Servo servoEnter;
Servo servoExit;
LiquidCrystal lcd(12,11,5,4,3,2); // set the LCD address to 0x27 for a 16x2 display
//Trig and Echo pins for sonar sensors//
int trigPin1 = 22;
int echoPin1 = 24;
int trigPin2 = 26;
int echoPin2 = 28;
int trigPin3 = 30;
int echoPin3 = 32;
int trigPin4 = 34;
int echoPin4 = 36;
int trigPin5 = 38;
int echoPin5 = 40;
int trigPin6 = 42;
int echoPin6 = 44;
int distm[2]; //used to store distances for motor/gate sensors
int dist[4]; // for parking
int dist_threshold = 8; // if distance<15cm, the slot is occupied. Otherwise, it is free.
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(trigPin2, OUTPUT);
pinMode(trigPin3, OUTPUT);
pinMode(trigPin4, OUTPUT);
pinMode(trigPin5, OUTPUT);
pinMode(trigPin6, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(echoPin2, INPUT);
pinMode(echoPin3, INPUT);
pinMode(echoPin4, INPUT);
pinMode(echoPin5, INPUT);
pinMode(echoPin6, INPUT);
servoEnter.write(85); //stop servo twitch on startup
servoExit.write(85);
servoEnter.write(0);
servoExit.write(0);
servoEnter.attach(6);
servoExit.attach(7);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
lcd.setCursor(1,0);
lcd.print("Smart Parking");
delay (100);
}
void loop() {
// get the distances from sonar sensors for the gates
dist[0] = readsensor(trigPin1, echoPin1);
if (distm[0] < dist_threshold)
// check the entry gate
{
servoEnter.write(90);
delay(3000);
servoEnter.write(0);
delay(15);
}
distm[1] = readsensor(trigPin2, echoPin2);
//check the exit gate
if (distm[1] < dist_threshold ) {
servoExit.write(90);
delay(3000);
servoExit.write(0);
delay(15);
}
// parking sensors
dist[0] = readsensor(trigPin3, echoPin3); // parking distance for p1
dist[1] = readsensor(trigPin4, echoPin4); // p2
dist[2] = readsensor(trigPin5, echoPin5);
dist[3] = readsensor(trigPin6, echoPin6);
bool slot[4];
int i=0,j=0;
for(i=0;i<4;i++){
if (dist[i]>8){
slot[i]=false;
}
else{
slot[i]=true;
}
}
char str[5];
for(i = 0; i < 4 ;i++)
{
if(slot[i])
{
char spot[4];
sprintf(spot, "P%d,", i);
strcat(str, spot);
delay(200);
}
}
strcat(str,"Are Free");
lcd.setCursor(1,0);
lcd.print(str);
delay(500); // wait for a little time before the next cycle
}
int readsensor(int trigPin, int echoPin) {
//send a pulse to the sensor//
digitalWrite(trigPin, LOW);
delayMicroseconds(1000);
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH); // measure the time taken to hear the echo
long cm = duration / 29 / 2; //calculate the distance in cm
return cm;
}