IR object counter(total count,# of vacant, occupied)

Hi, I'm creating a project for my pre-oral defense in a thesis. What I'm trying to do is using an IR sensor, I'm going to count the total number of the vehicle that parked in the parking lot also the number of vacant and occupied space. for my prototype, I'm using 2 sensors but in my code shown below is only one.

int pin=4;
int Occupied=0;
int detected=false;
int Vacant=0;

void setup() {
  Serial.begin(9600);
  pinMode(pin,INPUT);
}


void loop(){
  count();
}
void count() {

    int val=digitalRead(pin);
  if( (val == 0) && (detected == false )  ){
          Occupied++;
          detected = true;
          delay(2000);
         
          Serial.print("occupied =");
          Serial.println(Occupied);
      }
       if( (val == 1) && (detected == true ) ){

          Vacant-Occupied;
          detected =false;
          Serial.print("Vacant =");
          Serial.println(Vacant);
          
          
  
}

}

my problem is first, the total number of vacant is not detected, second, when I put my hands in the sensor(to det. detection) it adds one but when i pull out my hands out the sensor it's still count number of detected not the vacant one that's the problem. and lastly, i don't know how to put the other sensor and do what the existing sensor does.

anyone can help me about this? I appreciate alot

Are you counting the vehicles entering and exiting, or trying to detect if a vehicle is in a space (requires 1 sensor per parking spot)?

yes, i'm trying to detect if the vehicle is in a space. for now i'm working for 2 sensors only that means 2 space.

I think your bigger problem
Will be managing all the sensors. How many “spaces” are you trying to monitor? If it’s a typical parking lot or garage, you’ll probably need several Arduinos (dozens?), which means you’ll need to find some way to network them together.

What are your thoughts?

yes that's right i'll be using 12 sensors. but for now because it's only a prototype i'm only using 2 sensors.
my problem is how can i code that 2 sensors

When are your orals? When do you need this?

i really need it now because my oral is on saturday jan.26, i'm working hard on making the code and search some examples. i didn't find any

What do you hope to demonstrate? A working prototype with one Arduino, 2 sensors and a connected PC showing the serial console or what ?
What IR sensors are you using for your test ? Post a link to a site which has these sensors.

You have implied (post #2) that you are interested only in the presence (or absence) of a vehicle in a specific space so you will have one sensor per parking bay in the end solution.

The problem should not be that difficult to solve if you have to demonstrate only a simple prototype (apart from the closeness of the deadline). If you have to scale it up to a real parking lot with multiple parking bays, then the real issues begin like reliability detection, interconnection of the sensors and controlling nodes etc.

yes that's right i'm going to demonstrate a simply prototype using 2 sensors. i hope you would help me.

Hi Alexis27,

Try this. You'll need to change the pin assignments to match your setup. Also - I assume that your sensor will pull up its output unless an object is detected, then it will drop its output. This is how your code appeared to operate.

Let me know what you think.

// Parking Lot Attendant

#define NumberOfSensors 2
#define ParkingLotCapacity 12

int SensorArray[NumberOfSensors] = {A4, A5}; // list of sensor pins
int NumVehiclesForDay;
int NumSpacesOccupied;

void PrintStatistics(void) {
 Serial.print(NumVehiclesForDay); Serial.print("\t");
 Serial.print(NumSpacesOccupied); Serial.print("\t");
 Serial.print(ParkingLotCapacity - NumSpacesOccupied); Serial.println();
}

void setup() {
 Serial.begin(9600); while (!Serial); // start serial comms and wait for it to initialize
 for (int i = 0; i < NumberOfSensors; i++) {
   pinMode(SensorArray[i], INPUT);
 }
 NumVehiclesForDay = 0;
 NumSpacesOccupied = 0;
 Serial.println("\nDay\tIn Use\tOpen");
 PrintStatistics();
}

void loop() {

 int OldNumSpacesOccupied = NumSpacesOccupied;
 NumSpacesOccupied = 0;

 // count the number of occupied spots
 for (int i = 0; i < NumberOfSensors; i++) {
   if (digitalRead(SensorArray[i]) == LOW) {
     NumSpacesOccupied++;
   }
 }

 // if the number of occupied spots has increased, add the new car to the count
 if (NumSpacesOccupied > OldNumSpacesOccupied) {
   NumVehiclesForDay += NumSpacesOccupied - OldNumSpacesOccupied;
 }

 // print statistics if something has changed
 if (NumSpacesOccupied != OldNumSpacesOccupied) {
   PrintStatistics();
 }

}

It looks like you are well on your way to implementing a full parking management system with the code you have been given !

I just looked up the sensor and found something similar but with a schematic diagram:

IR Proximity Sensor [4649] : Sunrom Electronics . It uses an LM393 comparator together with an 10K resistor on its (active low - open collector) output. No additional pull up resistor is therefore required.

For a "real" application, the range of that sensor (max 30cm) would probably be too small, but certainly enough for a simple prototype.

the data gathered needed to send in a phone using a sim800l module. that's the next thing i needed

It sounds like the requirements statement for this project is quie dynamic.
What do you want to see on the phone? The updated number of free park places available at each change or what ?
Post a link to a site where your sim800l module is listed.
Once that is done, do you see any new requirements coming along before the deadline of Jan 26 ?

yes the updated number of available space and the number of occupied also the total accumulated number of users(vehicles).

when i send a message like:"status" i'll receive the updated number of available and occupied space
when i send a message like:"total" i'll receive the total accumulated number of user.

after jan.26 I'll just be adding up the remaining sensors, maybe add some features like time consumed on each sensor. and make another system for another parking lot location.

I used the gsm system because of some areas in which I would apply it has poor internet connection so it's not practical for me to put the data in a cloud storage or to make it an IoT project.

https://www.lelong.com.my/sim800l-gprs-gsm-module-arduino-raspberry-littlecraft-175954380-2020-04-Sale-P.htm

OK. Have you got to the stage where you can send an SMS/text message from your arduino via this SIM800L module to an mobile (cell) phone ? And in the reverse direction, can you receive an SMS/text message from a mobile phone, via this SIM800L module and pass it to the arduino ?

If not, start googling for "arduino sim800L sms" to get a bit further.

yes it's working, i'll already tried to send and receive message from my phone to gsm module but only a character. not the data that i want like in the result of the code given above

If you post the code that you have created for SMS/Text message via Arduino --> GSM Module --> Cell (mobile) phone, (and in the reverse direction), someone may be able to help to solve the "only one character sent/received" problem.