Go Down

Topic: smart parking system coding help needed (Read 269 times) previous topic - next topic

shubhamjain28

My project is a smart parking system.
My project has 4 distance sensors which sense and object and calculate the distance of the object from the sensor.  [P1-distance of car from parking 1 sensor, similarly P2,P3,P4]
So, I have 4 sensors for 4 parking slots.
Now when P1<8cm , I want to print on lcd -P2,P3,P4 are free
P2<8cm , I want to print -P1,P3,P4 are free
P3<8cm , I want to print -P1,P2,P4 are free
P4<8cm , I want to print -P1,P2,P3 are free
P1<8cm && P2<8cm , I want to print -P3,P4 are free and so on for all combinations (such that when 3 parking are < 8cm , 1 is free and when all are < 8cm - the parking is full)

i need your help with this coding , i have tried coding it using if and else but i am sure thats not the way to code it nor does it work as the system continues executing every step even when there is no object for the sensors to sense.It displays all the prints on the lcd consecutively.

J-M-L

test P1, memorize status in an array at entry 0
test P2, memorize status in an array at entry 1
test P3, memorize status in an array at entry 2
test P4, memorize status in an array at entry 3

The just iterate over your array with a for loop and print or not the info, you'll need to add a bit of smartness if you want commas in the right places
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

shubhamjain28

check the attached coding, although this is only for display vacant parking when only one of them is occupied(i need to display the free parkings even if multiple parking are filled in any combinations).
Also in this coding only P4 ie dist[5] sensor is working , others are not working.I have checked the connections and the  sensors ,both are correct.
I am not an expert in coding so i need your help with that.

J-M-L

#3
Nov 19, 2017, 05:04 am Last Edit: Nov 19, 2017, 05:06 am by J-M-L
Post code in line, using code tags - won't download stuff, reading from my tablet (read how to use the forum before posting your answer)
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

shubhamjain28

#4
Nov 19, 2017, 05:09 am Last Edit: Nov 19, 2017, 05:59 am by shubhamjain28
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,
Code: [Select]
#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;
}



shubhamjain28

here is my code
Code: [Select]
#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 = 7.85; // 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;
}




J-M-L

#6
Nov 19, 2017, 05:29 am Last Edit: Nov 19, 2017, 05:30 am by J-M-L
int dist_threshold = 7.85; => does not really looks like an int to me....

int readsensor()
{
...
  long cm= duration / 29 / 2; //calculate the distance in cm
  return cm;
}

Same comment here, bad type

Your code does not meet what I asked in #1
Start with just that, get rid of everything else
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

shubhamjain28

yup changed it to 8 and i didnt get what you asked me to do in #1

dougp

Please contact a moderator to request that your two threads be merged.

Coding Badly


@shubhamjain28, do not cross-post.  Threads merged.


J-M-L

#10
Nov 19, 2017, 10:13 am Last Edit: Nov 19, 2017, 12:44 pm by J-M-L
yup changed it to 8 and i didnt get what you asked me to do in #1
Imagine you have an array with 5 boolean that can be either true or false

Code: [Select]
const boolean parkingIsAvailable[] = {false, true, true, false, false};

Exercise 1: Write a program which goes through the array and prints the index of only the entries which are true, one per line. Do all this in setup(), keep loop() empty. for the array above your console output should look like this (remember array index starts at 0)

Available Parking at index:
1
2


Exercise 2: now put the answer on only one line: improve the previous code by adding punctuation => a comma needs to be printed to separate the true indexes - bring careful you don't want a comma at the end of the list. Do all this in setup(), keep loop() empty

For the array above your console output should look like this (remember array index starts at 0)

Available Parking at index: 1, 2


try by changing the array to
Code: [Select]
const boolean parkingIsAvailable[] = {false, true, true, false, true};
and
Code: [Select]
const boolean parkingIsAvailable[] = {true, false, true, false, true};
 for example to ensure all works fine

Exercise 3: change the code from exercise 2 to improve a bit more because human don't usually count from 0 like computers but from 1 and add a letter in front of the digit, so array index 0 should appear as P1 the output to look like this (for the first array above)

Available Parking: P2, P3


Exercise 4: change the code from exercise 3 to dynamically build the array from your sensors (so array can't be const anymore). Add a function taking a trigger pin and sensor pin as parameter, returning a boolean.  fill up the array in setup(), keep loop() empty

Exercise 5: make this now the real thing, so that it keeps looking Every 5 seconds to the status of the parking and print out the results
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

shubhamjain28

Just saw your post,
Here's what i did yesterday.
I think it still has some issue with print.
Code: [Select]

  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;
    }
  }

  const char*str;
  for(i=0;i<4;i++){
    if(slot[i]){
      str=strcat("P",(char*)i);
    }
  }
  strcat(str,"Are Free");
  lcd.setCursor(0,1);
  lcd.print(str);

BulldogLowell

#12
Today at 10:26 pm Last Edit: Today at 10:29 pm by BulldogLowell
Code: [Select]
  const char*str;
  for(i=0;i<4;i++){
    if(slot[i]){
      str=strcat("P",(char*)i);
    }
  }
  strcat(str,"Are Free")


you cannot write a c string to a pointer, you need a buffer big enough to accept all of the chars:

Code: [Select]
 
  char str[SOME_BIG_ENOUGH_SIZE];
  for(i=0;i<4;i++){
    if(slot[i]){
      //str=strcat("P",(char*)i); don't know what you are trying to do there
    }
  }
  strcat(str,"Are Free");  // appends str with the literal "Are Free"

Go Up