How to ping for a certain amount of times with the HC-SR04

I’ve been working on this LED dimmer using an ultrasonic sensor. Pretty cool, i can use my hands distance from the sensor to dim my LEDstrips.

Now i have a sound detection sensor and i want to make a claplight (i dont know if thats a thing but you know what i mean) that turns on and enables the dimmer when a sound is detected and shutdowns the LED when a sound is heard.
The turning on part worked for me, but to shut the LED down i had to make a sound for a longer time and it was stil reading the distance sensor and dimming the lights if anything came into its measuring path.

Even without any programming knowledge i think the reason why i have to make a sound for a longer time to shut it off is because of the distance sensor ping’ing 20 times a second.

so what if just made the dimmer active for 10 seconds after the sound input and then it would stop measuring and leave the sound sensor open? That would be a good idea but i dont know how to implement it in my code. Any suggestions?

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

int soundDetectedPin = 7; // Use Pin 7 as our Input
int soundDetectedVal = HIGH; // This is where we record our Sound Measurement
boolean bAlarm = false; //
#define LED 11

#include <NewPing.h>

#define TRIGGER_PIN  3  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     2  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 60 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {   pinMode (soundDetectedPin, INPUT) ; 
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

void loop(){
  soundDetectedVal = digitalRead (soundDetectedPin);
  if(soundDetectedVal == LOW)
  if (bAlarm==true)
  //the following i just want to run 200 times (i.e for 10 seconds)
 int afstand = (sonar.ping_cm());                        //afstand means distance in danish... The more you know
 int lysstyrke = map(afstand, 0, MAX_DISTANCE, 0, 255);  //lysstyrke means brightness in danish
  delay(50);                                             // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  if (lysstyrke > 0)
    analogWrite(LED, lysstyrke);   
if (bAlarm==false) //now i want to turn off the LED 
  analogWrite(LED, 0);

Your pb is that if the sound is low you keep switching the value of bAlarm at every loop - and loops are fast!

The way you want to architect your main loop is

Start state timer not running

if there is a sound and timer not running then start a timer (record the start time) and set a flag your timer is running

If timer is running and has not timed out

  • do your ping check stuff

If timer has timed out reset the timer flag

Ahh okay that makes a lot of sense. I’ll try to learn how to use timers then. Thanks

Don’t use a real timer

Just record start time and then to know if you are within your delay do

unsigned long startTime;
#define TIMEOUTDELAY 10000L

If (millis()-startTime < TIMEOUTDELAY) {
// here I know I'm still within my timeout