Need Help In Coding

I am making a smart parking system and the spots are detected by an ultrasonic senor, i wanted display of the number of slots free and the number of slots filled but i cannot think of it in the code here is the code attached.

UltraSonic_Prototype.ino (1.73 KB)

Hi mabdullahq,
Please use code tags when posting code.

Please describe what you expect your code to do, what your code actually does and how those two differ.

You need to give us more detail. However, in looking at your code, I have the following comments.

  1. spot_filled spot_empty are declared global, however you redefine them inside loop().

  2. When a spot is empty, I do not see you changing spot_empty. I would expect the following.
    a) If spot filled this should decrease spot_empty and increase spot_filled.
    b) if spot is empty, spot_filled should be decreaded and spot_empty filled.

  1. Inside your loop() you have the odd brackets that should not be there. While this should not be a major issue, please do not have brackets where they do not belong.

Please provided more detail as to what is not working and what is.

{
lcd.clear();
  lcd.setCursor(0,0); 
  lcd.print("Spots Empty:"); 
  lcd.print(spot_empty);
  lcd.setCursor(0,1);
  lcd.print("Spots Filled:");
  lcd.print(spot_filled); 
  
}

So basically my code is for a Parking system and i want the code to display the number of filled and empty spots that are in the lot....but unfortunately i cant create a logic here as to the number of spots empty on the LCD keeps on increasing as the code is in a loop and keeps on repeating what should I do to make the number of spots filled or empty be displayed accurately on the lcd....and sorry i dont know how to create this box for the code if you could help me out on this I will put the code live

mabdullahq:
I am making a smart parking system and the spots are detected by an ultrasonic senor, i wanted display of the number of slots free and the number of slots filled but i cannot think of it in the code here is the code attached.

Romonaga:
You need to give us more detail. However, in looking at your code, I have the following comments.

  1. spot_filled spot_empty are declared global, however you redefine them inside loop().

  2. When a spot is empty, I do not see you changing spot_empty. I would expect the following.
    a) If spot filled this should decrease spot_empty and increase spot_filled.
    b) if spot is empty, spot_filled should be decreaded and spot_empty filled.

  1. Inside your loop() you have the odd brackets that should not be there. While this should not be a major issue, please do not have brackets where they do not belong.

Please provided more detail as to what is not working and what is.

{

lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Spots Empty:");
 lcd.print(spot_empty);
 lcd.setCursor(0,1);
 lcd.print("Spots Filled:");
 lcd.print(spot_filled);
 
}

Can you help me out on how to increase those variable I am looking forward to your suggestions...

mabdullahq:
.and sorry i dont know how to create this box for the code if you could help me out on this I will put the code live

Go take a read through the sticky post named "How to use this forum - please read." that is at the top of this and every forum on the arduino site.

I can help, but you are not giving us much to go on. So lets try this again.

Please examine the moc code I provided based on your code.

  1. You need to know how many spots you have. As such I initialized spot_empty with 100. So now we know our parking lot can hold 100 cars.

  2. When a car parks we need to decrease spot_empty and increase spot_filled

The code snippit below shows how this would work.

//GLOBAL
int spot_filled = 0;
int spot_empty =100;

loop()
{
   if (distance < 8) 
  {
    digitalWrite(led1Pin, HIGH);
    digitalWrite(led2Pin, LOW);
    spot_filled +=  1;
    spot_empty -= 1;
  }
  else 
  {
    digitalWrite(led2Pin, HIGH);
    digitalWrite(led1Pin, LOW);
    spot_empty +=  1;
    spot_filled -= 1;

  }
}
   if (distance < 8)

This needs changing so that the counts are only adjusted when the distance becomes less than 8, ie a new car has been detected, rather than when distance is less than 8, ie a car is currently being detected which will happen thousands of times per second

Look at the StateChangeDetection example in the IDE to see how to do it

That is my point it happens so much times that it keeps on adding the amount as the loop repeats

mabdullahq:
That is my point it happens so much times that it keeps on adding the amount as the loop repeats

As I said

UKHeliBob:
Look at the StateChangeDetection example in the IDE to see how to do it

I will surely check it mate, Thanks alot.
Which library is this located in?“StateChangeDetection”

UKHeliBob:
Look at the StateChangeDetection example in the IDE to see how to do it

The first thing to do is to think carefully about what you want your program to do.

You have 2 parking spots.

For each spot,

  1. if the last time you checked the spot was empty, then if it’s filled now increment the filled spots counter;
  2. if the last time you checked the spot was filled, then if it’s empty now decrement the filled spots counter.

So for each spot you need a boolean static var that records if the spot was filled or not.

Now translate specs to code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // ??? my sketch does not compile with this constructor, different liquid crystal libraries ???

// LiquidCrystal_I2C lcd(0x3F, 16, 2); // this is my library constructor

const int led1Pin = 8;
const int led2Pin = 9;

const int led3Pin = 10;
const int led4Pin = 11;

const int trig1Pin = 2;
const int echo1Pin = 3;

const int trig2Pin = 6;
const int echo2Pin = 7;

#define NUM_SPOTS 2 // total number of parking spots

bool spot1_filled = false; // true if spot 1 is filled
bool spot2_filled = false; // true if spot 2 is filled

int ctFilled = 0; // filled spots counter

// update the display info
void updateDisplay()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Spots Empty:");
  lcd.print(NUM_SPOTS - ctFilled);
  lcd.setCursor(0, 1);
  lcd.print("Spots Filled:");
  lcd.print(ctFilled);
}

// check if spot is filled now
bool spotIsFilledNow(int triggerPin, int echoPin)
{
  unsigned long distance;
  
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(triggerPin, LOW);
  
  distance = pulseIn(echoPin, HIGH) / 58;

  return(distance < 8); 
} 

// check and update the assigned spot status
void updateSpotStatus(int triggerPin, int echoPin, int led1, int led2, bool *spotFilled)
{
  if (spotIsFilledNow(triggerPin, echoPin))
  { 
    // the spot is filled now
    
    if (!(*spotFilled))
    {
      // it was empty previously, so update status to filled
      
      *spotFilled = true;
      
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);      
      
      ctFilled++;
    }
  }
  
  else
  {
    // spot is empty now
    
    if (*spotFilled)
    {
      // it was filled previously, so update status to empty
      
      *spotFilled = false;
      
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
      
      ctFilled--;
    }   
  }  
}

// update all spots statuses
void updateSpots()
{
  updateSpotStatus(trig1Pin, echo1Pin, led1Pin, led2Pin, &spot1_filled);
  updateSpotStatus(trig2Pin, echo2Pin, led3Pin, led4Pin, &spot2_filled);
}

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.backlight();
  
  pinMode(trig1Pin, OUTPUT);
  pinMode(echo1Pin, INPUT);
  
  pinMode(trig2Pin, OUTPUT);
  pinMode(echo2Pin, INPUT);
  
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);

  // initialize the spots statuses
  updateSpots();

  // initialize display info
  updateDisplay();  
}

void loop()
{
  // save the old number of filled spots
  int oldFilled = ctFilled;

  // update all the parking spots statuses
  updateSpots();
  
  if (oldFilled != ctFilled)
    // some spot status is changed, update display info
    updateDisplay();  
}

Which library is this located in?"StateChangeDetection"

It is not in a library, it is one of the example programs installed with the IDE

Look under File/Examples/02.Digital

Or Google can find it:

vlc0617:
The first thing to do is to think carefully about what you want your program to do.

You have 2 parking spots.

For each spot,

  1. if the last time you checked the spot was empty, then if it’s filled now increment the filled spots counter;
  2. if the last time you checked the spot was filled, then if it’s empty now decrement the filled spots counter.

So for each spot you need a boolean static var that records if the spot was filled or not.

Now translate specs to code

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // ??? my sketch does not compile with this constructor, different liquid crystal libraries ???

// LiquidCrystal_I2C lcd(0x3F, 16, 2); // this is my library constructor

const int led1Pin = 8;
const int led2Pin = 9;

const int led3Pin = 10;
const int led4Pin = 11;

const int trig1Pin = 2;
const int echo1Pin = 3;

const int trig2Pin = 6;
const int echo2Pin = 7;

#define NUM_SPOTS 2 // total number of parking spots

bool spot1_filled = false; // true if spot 1 is filled
bool spot2_filled = false; // true if spot 2 is filled

int ctFilled = 0; // filled spots counter

// update the display info
void updateDisplay()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(“Spots Empty:”);
  lcd.print(NUM_SPOTS - ctFilled);
  lcd.setCursor(0, 1);
  lcd.print(“Spots Filled:”);
  lcd.print(ctFilled);
}

// check if spot is filled now
bool spotIsFilledNow(int triggerPin, int echoPin)
{
  unsigned long distance;
 
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(triggerPin, LOW);
 
  distance = pulseIn(echoPin, HIGH) / 58;

return(distance < 8);
}

// check and update the assigned spot status
void updateSpotStatus(int triggerPin, int echoPin, int led1, int led2, bool *spotFilled)
{
  if (spotIsFilledNow(triggerPin, echoPin))
  {
    // the spot is filled now
   
    if (!(*spotFilled))
    {
      // it was empty previously, so update status to filled
     
      *spotFilled = true;
     
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);     
     
      ctFilled++;
    }
  }
 
  else
  {
    // spot is empty now
   
    if (*spotFilled)
    {
      // it was filled previously, so update status to empty
     
      *spotFilled = false;
     
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
     
      ctFilled–;
    } 
  } 
}

// update all spots statuses
void updateSpots()
{
  updateSpotStatus(trig1Pin, echo1Pin, led1Pin, led2Pin, &spot1_filled);
  updateSpotStatus(trig2Pin, echo2Pin, led3Pin, led4Pin, &spot2_filled);
}

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.backlight();
 
  pinMode(trig1Pin, OUTPUT);
  pinMode(echo1Pin, INPUT);
 
  pinMode(trig2Pin, OUTPUT);
  pinMode(echo2Pin, INPUT);
 
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);

// initialize the spots statuses
  updateSpots();

// initialize display info
  updateDisplay(); 
}

void loop()
{
  // save the old number of filled spots
  int oldFilled = ctFilled;

// update all the parking spots statuses
  updateSpots();
 
  if (oldFilled != ctFilled)
    // some spot status is changed, update display info
    updateDisplay(); 
}

Actually i am using a I2C driver and this every driver has its certain adrress mine is 0X3F

Thank You so Much! Its Perfectly working and seems like some simple change in logic was needed!

vlc0617:
The first thing to do is to think carefully about what you want your program to do.

You have 2 parking spots.

For each spot,

  1. if the last time you checked the spot was empty, then if it’s filled now increment the filled spots counter;
  2. if the last time you checked the spot was filled, then if it’s empty now decrement the filled spots counter.

So for each spot you need a boolean static var that records if the spot was filled or not.

Now translate specs to code

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // ??? my sketch does not compile with this constructor, different liquid crystal libraries ???

// LiquidCrystal_I2C lcd(0x3F, 16, 2); // this is my library constructor

const int led1Pin = 8;
const int led2Pin = 9;

const int led3Pin = 10;
const int led4Pin = 11;

const int trig1Pin = 2;
const int echo1Pin = 3;

const int trig2Pin = 6;
const int echo2Pin = 7;

#define NUM_SPOTS 2 // total number of parking spots

bool spot1_filled = false; // true if spot 1 is filled
bool spot2_filled = false; // true if spot 2 is filled

int ctFilled = 0; // filled spots counter

// update the display info
void updateDisplay()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(“Spots Empty:”);
  lcd.print(NUM_SPOTS - ctFilled);
  lcd.setCursor(0, 1);
  lcd.print(“Spots Filled:”);
  lcd.print(ctFilled);
}

// check if spot is filled now
bool spotIsFilledNow(int triggerPin, int echoPin)
{
  unsigned long distance;
 
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(triggerPin, LOW);
 
  distance = pulseIn(echoPin, HIGH) / 58;

return(distance < 8);
}

// check and update the assigned spot status
void updateSpotStatus(int triggerPin, int echoPin, int led1, int led2, bool *spotFilled)
{
  if (spotIsFilledNow(triggerPin, echoPin))
  {
    // the spot is filled now
   
    if (!(*spotFilled))
    {
      // it was empty previously, so update status to filled
     
      *spotFilled = true;
     
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);     
     
      ctFilled++;
    }
  }
 
  else
  {
    // spot is empty now
   
    if (*spotFilled)
    {
      // it was filled previously, so update status to empty
     
      *spotFilled = false;
     
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
     
      ctFilled–;
    } 
  } 
}

// update all spots statuses
void updateSpots()
{
  updateSpotStatus(trig1Pin, echo1Pin, led1Pin, led2Pin, &spot1_filled);
  updateSpotStatus(trig2Pin, echo2Pin, led3Pin, led4Pin, &spot2_filled);
}

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.backlight();
 
  pinMode(trig1Pin, OUTPUT);
  pinMode(echo1Pin, INPUT);
 
  pinMode(trig2Pin, OUTPUT);
  pinMode(echo2Pin, INPUT);
 
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);

// initialize the spots statuses
  updateSpots();

// initialize display info
  updateDisplay(); 
}

void loop()
{
  // save the old number of filled spots
  int oldFilled = ctFilled;

// update all the parking spots statuses
  updateSpots();
 
  if (oldFilled != ctFilled)
    // some spot status is changed, update display info
    updateDisplay(); 
}