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,
- if the last time you checked the spot was empty, then if it's filled now increment the filled spots counter;
- 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();
}