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