Stop buzzer after some seconds ... how ?

The following is a sketch I use to water level using an untrasonic sensor.
It works very well, but when the water level is @ 100% the buzzer starts and I dont want it to beep all the time even if the water level stays @ 100%.

How to stop buzzer after some seconds/beeps.

 #define trigPin 8
#define echoPin 9
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int BUZZER = 10 ;

void setup(){
      int duration,distance,percentage,heightTank;
      Serial.begin (9600);
      pinMode(trigPin,OUTPUT);
      pinMode(echoPin,INPUT);
      lcd.begin(16,2);
      lcd.print("HELLO");
        pinMode(BUZZER,OUTPUT);
}

void loop(){
      int duration,distance,percentage,heightTank,deviation;
      //You'll probably want to change the next 2 lines.
      // The first one is the max. level of the water.
      // The next one is how high the sensor is above that max. level.
      heightTank=65;
      deviation=4;
  
      digitalWrite(trigPin,HIGH);
      delayMicroseconds(1000);
      digitalWrite(trigPin,LOW);
      duration=pulseIn(echoPin,HIGH);
      distance=(duration/2)/29.1;
      percentage=100-(((distance-deviation)*100)/heightTank);
      Serial.println(distance);
      Serial.println(percentage);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Current tank");
      //lcd.print(distance);
      //lcd.print(" cm");
      lcd.setCursor(0,1);
      lcd.print("level: ");
      lcd.print(percentage);
      lcd.print(" %");
      delay(1000);

      digitalWrite(BUZZER,HIGH);
  if (percentage > 99) {digitalWrite(BUZZER,HIGH); delay(100); digitalWrite(BUZZER,LOW); delay(100);}
  else  {digitalWrite(BUZZER,LOW);}
  }

You could count the number of times that the buzzer went on/off.

byte counter = 0;

if (percentage > 99)
{
  if (counter <= 50)
  {
    digitalWrite(BUZZER, HIGH);
    delay(100);
    digitalWrite(BUZZER, LOW);
    delay(100);
    counter++;
  }
}
else counter = 0;

Leo…

Wawa:
You could count the number of times that the buzzer went on/off.

byte counter = 0;

if (percentage > 99)
{
  if (counter <= 50)
  {
    digitalWrite(BUZZER, HIGH);
    delay(100);
    digitalWrite(BUZZER, LOW);
    delay(100);
    counter++;
  }
}
else counter = 0;



Leo..

Thanks for the reply, but it didn’t work.
I tried by changing the values of “byte counter = 0;” and " if (counter <= 50)", but the buzzer was not stopping in the percentage is above the level.

You only posted a code snippet, so I don’t know if you have added what I wrote properly.

That buzzer part should work, and it turns the buzzer off after (100ms+100ms) * 50 = 10seconds.
if… “percentage” stays above 99.

100ms on/off times is a bit weird. More like purring. I used the numbers from your first example.
500ms for on and off could be better, with a counter limit of 10 (still 10seconds).
if… you use an active buzzer.

What is the datatype you have used for “percentage”.
Are you sure the value “percentage” stays above 99.
A little fluctuation between 99 and 100 will instantly fire the buzzer sequence again.
Maybe better to replace that else with
if(percentage < 99) counter = 0; // 1% hysteresis

Post the full sketch if you want proper help.
Leo…

Thanks again for the reply.
I have added the full sketch in the question.
100ms on/off time seems to be working perfect, there a beep and a short break. I am suing a larger buzzer, not the small type.
I am new to coding in Arduino, so thanks for helping.

Most of the time the 100% water level stays for few minutes, and even if it gets to 99%, sometimes, it will jump back to 100% and the buzzer will sound again, is there any way to stop this ?
For example, if the level goes to 100%, the buzzer should shound for 15 seconds.
if it goes back to 99% , it might come back again to 100%, the buzzer should not sound then.
May be we can add another timing function, like the buzzer should only reactivate after 30 minutes or… something like that… ?

I am sorry my English is not that good.

#define trigPin 8
#define echoPin 9
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int BUZZER = 10 ;

void setup(){
      int duration,distance,percentage,heightTank;
      Serial.begin (9600);
      pinMode(trigPin,OUTPUT);
      pinMode(echoPin,INPUT);
      lcd.begin(16,2);
      lcd.print("HELLO");
        pinMode(BUZZER,OUTPUT);
}

void loop(){
      int duration,distance,percentage,heightTank,deviation;
      //You'll probably want to change the next 2 lines.
      // The first one is the max. level of the water.
      // The next one is how high the sensor is above that max. level.
      heightTank=65;
      deviation=4;
  
      digitalWrite(trigPin,HIGH);
      delayMicroseconds(1000);
      digitalWrite(trigPin,LOW);
      duration=pulseIn(echoPin,HIGH);
      distance=(duration/2)/29.1;
      percentage=100-(((distance-deviation)*100)/heightTank);
      Serial.println(distance);
      Serial.println(percentage);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Current tank");
      //lcd.print(distance);
      //lcd.print(" cm");
      lcd.setCursor(0,1);
      lcd.print("level: ");
      lcd.print(percentage);
      lcd.print(" %");
      delay(1000);

      digitalWrite(BUZZER,HIGH);
  if (percentage > 99) {digitalWrite(BUZZER,HIGH); delay(100); digitalWrite(BUZZER,LOW); delay(100);}
  else  {digitalWrite(BUZZER,LOW);}
  }

shijil: Most of the time the 100% water level stays for few minutes, and even if it gets to 99%, sometimes, it will jump back to 100% and the buzzer will sound again, is there any way to stop this ?

It always helps if you read the answers provided to you. In this case, #3.

Maybe better to set the hysteresis to 95% but it depends on your actual use what value makes sense there.

In setup() I see: int duration,distance,percentage,heightTank; If you declare them there (locally), then loop() can't use those variables. You don't use those varables anyway in setup(), so remove them.

You have declared them in loop() again. Ok, but I would move them to above setup(), to declare them globally. One on each line could make things more readable.

That delay(1000) is going to upset your buzzer routine It will add one second to the 100ms (buzzer, LOW) with each loop.

The digitalWrite(BUZZER,HIGH); that is outside the if() condition should go. It will only turn the buzzer on with each loop, regardless of 'percentage'.

Look at the "BlinkWithoutDelay" sketch in the examples of the IDE. That sketch shows you how to use millis() timing to replace that delay(1000);

Let us know if you have problems implementing those changes. Leo..