count vehicle

hello i'm rod

i'm currently working on a project which involve counting vehicles
but first i'm trying to test it on a smaller scale prototype.
i'm using ultrasonic sensors (HC-SR04)

I got a code working to count, but i'm trying to simulated when ever the scenario involve a car park in front of the sensor, the count increment until the object move out of the sensor.
I was wondering if y'all know any kind of algorithm to try it on my code to cancel the increment of counts while a vehicle is stationary in front of the sensor, or any ideas would be help full

this is my code

#define trigPin 13

#define echoPin 12

int counter = 0; //Initialize the counter

int currentState = 0; //Initialize the current state

int previousState = 0; //Initialize the previous state

int parkedCar = 0;//Parked car variable

void setup() {

Serial.begin(9600);// Initialize serial communication

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

}

void loop()

{

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance <= 30){

currentState = 1;

}

else {

currentState = 0;

}

if (trigPin == LOW){

parkedCar = 1;

}

else {

parkedCar = 0;

}

delay(300); //Delay for counter

if(currentState != previousState){

if(currentState == 1 && (parkedCar == 0)){

counter = counter + 1;

Serial.println(counter);

}

}

/*if(currentState != previousState){

if(currentState = 1){

if(parkedCar = 1){

counter = counter + 1;

Serial.println(counter);

}

}

}

Thanks for y'all time

Your if() statements are wrong. They are doing an assignment '=' when they should be doing an equality comparison '=='

Look at the StateChangeDetection example.

A concept using a state machine.

(No attempt made to compile nor test. Just a basic framework to give you ideas.)

#define trigPin 13
#define echoPin 12

int counter = 0; //Initialize the counter

void setup() 
{
    Serial.begin(9600);// Initialize serial communication
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);

}//setup

//statemachine state names
#define WAIT_FOR_VEHICLE    0
#define WAIT_FOR_NO_VEHICLE 1
//
void loop()
{
    static byte
        state = WAIT_FOR_VEHICLE;   //start with waiting for a vehicle
    float
        dist;
        
    switch( state )
    {
        case    WAIT_FOR_VEHICLE:
            //get the distance...
            dist = MeasureDistance();
            //...if it's less than 30cm and greater than zero (pulseIn returns 0 if no pulse seen...)...
            if(  dist <= 30.0 && dist > 0.0 )
            {
                //bump the count then move to the state where we wait for the sensor to show
                //clear again
                counter++;
                Serial.print( F("Count is: ") );
                Serial.println( counter );
                state = WAIT_FOR_NO_VEHICLE;     
                
            }//if
            
        break;

        case    WAIT_FOR_NO_VEHICLE:
            //sensor must show something >40cm away (or show ~0.0) before we say the coast is clear
            //I used 0.01 instead of "==0.0" in case the math returns a float that's tiny but not
            //_equal_ to zero.
            dist = MeasureDistance();
            if(  dist > 40.0 || dist <= 0.01 )
                //when condition met, go back to waiting for vehicle again
                state = WAIT_FOR_VEHICLE;
        
        break;
        
    }//switch

}//loop

float MeasureDistance( void )
{
    unsigned long
        duration;
        
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);
    return( (float)duration/2.0) / 29.1;

}//MeasureDistance