The 4 parking sensors used give distances dist[2],dist[3],dist[4],dist[5].
Only the sensor 4 (dist[5]) senses the object and prints on the lcd while others who have the same code are not working.
Also,this code only prints when 1 parking is occupied but i need a code so that i can print the free parking even when multiple parkings are occupied in any combinations.
example:- p2,p3 are occupied so print p1,p2 free
p1,p3,p4 occupied so print p2 free and so on.
I dont have much knowledge in coding so I will need your help with this.
here is my code,
#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 dist[6]; //used to store distances
int dist_threshold = 8; // if distance<15cm, the slot is occupied. Otherwise, it is free.
void setup() {
// initialize serial communication:
Serial.begin(9600);
initSensors();
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.
lcd.print("Smart Parking");
}
void loop() {
// get the distances from sonar sensors//
dist[0] = readsensor(trigPin1, echoPin1);
if (dist[0] < dist_threshold)
// check the entry gate
{
servoEnter.write(90);
delay(3000);
servoEnter.write(0);
delay(15);
}
dist[1] = readsensor(trigPin2, echoPin2);
//check the exit gate
if (dist[1] < dist_threshold ) {
servoExit.write(90);
delay(3000);
servoExit.write(0);
delay(15);
}
dist[2] = readsensor(trigPin3, echoPin3);
//parkiing sensors
if (dist[2] < dist_threshold) {
Serial.print("11"); // slot 1 is occupied
Serial.print('\n');
lcd.clear();
lcd.setCursor(1,0);
lcd.print("P2,P3,P4 free");
}
else {
Serial.print("10");
Serial.print('\n');
lcd.clear();
lcd.setCursor(1,0);
lcd.print("All free");
}
dist[3] = readsensor(trigPin4, echoPin4);
if (dist[3] < dist_threshold) {
Serial.print("21");
Serial.print('\n');
lcd.clear();
lcd.setCursor(1,0);
lcd.print("P1,P3,P4 free");
}
else {
Serial.print("20");
Serial.print('\n');
lcd.clear();
lcd.setCursor(1,0);
lcd.print("All free");
}
dist[4] = readsensor(trigPin5, echoPin5);
if (dist[4] < dist_threshold) {
Serial.print("31");
Serial.print('\n');
lcd.clear();
lcd.setCursor(1,0);
lcd.print("P1,P2,P4 free");
}
else {
Serial.print("30");
Serial.print('\n');
lcd.clear();
lcd.setCursor(1,0);
lcd.print("All free");
}
dist[5] = readsensor(trigPin6, echoPin6);
if (dist[5] < dist_threshold) {
Serial.print("41");
Serial.print('\n');
lcd.clear();
lcd.setCursor(1,0);
lcd.print("P1,P2,P3 free");
}
else {
Serial.print("40");
Serial.print('\n');
lcd.clear();
lcd.setCursor(1,0);
lcd.print("All free");
}
delay(500); // wait for a little time before the next cycle
}
void initSensors() {
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);
}
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;
}
updated_carpark.ino (3.62 KB)