Here we are using an HC sr04 ultrasonic module with arduino. Refer Hc sr04 arduino interface.
The variable “count” counts when any object comes closer than 25cm. And count again only when that oject moves away amd comes again.
const int trig = 2, echo = 3;
// chnage set_distance = 0 to 400cm
int state = 0, count = 0, set_distance = 25;
long duration, Distanceincm;
void setup()
{
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig, HIGH);
delay(15);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
Distanceincm = (duration / 58);
if (state == 0 && (Distanceincm < set_distance) ) {
state = 1;
count++;
Serial.println(count);
}
if (Distanceincm > set_distance) {
state = 0;
}
delay(100);
}
Here we are using an HC sr04 ultrasonic module with arduino. Refer Hc sr04 arduino interface.
The variable “count” counts when any object comes closer than 25cm. And count again only when that oject moves away amd comes again.
thanks sir