// pins int led = 13, sound = 12; int trig = 11, echo = 10; // people count int count; float prev; void setup() { // serial monitor setup Serial.begin(9600); // pins setup pinMode(led, OUTPUT); pinMode(sound, OUTPUT); pinMode(echo, INPUT); pinMode(trig, OUTPUT); // default states digitalWrite(led, LOW); digitalWrite(sound, LOW); digitalWrite(echo, LOW); count = 0; prev = 0; } void on_detect() { // incrementing people count // and printing count += 1; Serial.print("Count: "); Serial.println(count); // led blink and sound play digitalWrite(led, HIGH); tone(sound, 1024); delay(500); // setting to original state digitalWrite(led, LOW); noTone(sound); void loop() { // sending echo digitalWrite(trig, HIGH); delay(1); digitalWrite(trig, LOW); // recieving trig float input = 0.17 * pulseIn(echo, HIGH); if (input < 1000) { if (!(prev != 0 && prev - 10 <= input && input <= prev + 10)) { prev = input; on_detect(); delay(1000); } } }