buzzer is not getting turned off after 3 seconds

please someone help me please..

i made a ultrasonic water level indicator with 5 Led & Buzzer....The problem i am facing is that when the water level exceeds his limits the beep did not get turned off..it not get stopped/turned off after 3 seconds.

the code is that

// Arduino programming

#define trigpin 13
#define echopin 12

int led1 = 7;
int led2 = 6;
int led3 = 5;
int led4 = 4;
int led5 = 3;
int buzz6 = 2;

void setup()
{
Serial.begin(9600);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(buzz6, OUTPUT);

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(buzz6, LOW);

}

void loop()
{
int duration, distance;
digitalWrite(trigpin, HIGH);

delayMicroseconds(1000);
digitalWrite(trigpin, LOW);

duration = pulseIn(echopin,HIGH);

distance = ( duration / 2) / 29.1;
Serial.println("cm:");
Serial.println(distance);

if( (distance > 0) && (distance <= 10) )
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(buzz6, HIGH);
delay(3000);
digitalWrite(buzz6, LOW);
delay(3000);
} else
if( (distance > 10) && (distance <= 20) )
{

digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(buzz6, LOW);

} else

if( (distance > 20) && (distance <= 30) )
{

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(buzz6, LOW);
} else

if( (distance > 30) && (distance <= 40) )
{

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(buzz6, LOW);
} else

if( (distance > 50) && (distance <= 60) )
{

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
digitalWrite(buzz6, LOW);
} else

if( distance > 60 )
{

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(buzz6, LOW);
}

}

this is the full code i was uploaded but the problem is that when all the leds are high the buzzer have to sound just for 3 seconds and then it will go turned off completely even water is 100 percent the buzzer dont have to sound,please help me somone and let me know if any mistakes in coding please make it correct

When you switch the buzzer off the program will go back to the top of loop() and if the distance is still <10 it will instantly switch the buzzer back on. If that's not what you want you'll need a to set a flag to tell you you've already heard the buzzer so you don't switch it on again.

Steve

Thank you for you reply brother..
Brother how can i set buzzer just for 3 second's when the all leds will turned on the buzzer will sound just for 3 second's and then turned off automatically..

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

Use CTRL T to format your code.
Attach your sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]


Do you realize as long as this ‘if’ evaluates this will repeat ?

If you only want it to occur once, set a Flag to true; use that flag as disable:

if(Flag == false && distance > 0 && distance <= 10)
{
Flag = true;
. . .
}

The Flag can be reset somewhere else, perhaps:

else if(. . .)
{
Flag = false;
. . .
}

 if(  (distance > 0) && (distance <= 10)   )
{
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(buzz6, HIGH);
  delay(3000);
  digitalWrite(buzz6, LOW);
  delay(3000);

Reposting is not allowed on this site, please follow the forums rules.

Duplicate topics merged

Why did you start a second one ?

Dear all i am sorry for posting again...i am new here.. I don't know about it..

Please tell me how can stopped buzzer after sounds for 3 seconds

I did not understand still.

digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(buzz6, HIGH);
delay(3000);
digitalWrite(buzz6, LOW);
delay(3000);

That is the code...the buzzer sound for 3 second's and then it turned off for 3 second's...i want to permanently turned off the buzzer after 3 second's.. please help

the buzzer sound for 3 second's and then it turned off for 3 second's...i want to permanently turned off the buzzer after 3 second's.

The buzzer sounds again if the distance remains between 0 and 10 so stays on. If you only want it to sound once, then control whether it sounds using a boolean variable and only sound if the boolean is true. Once it has sounded set the boolean to false and it will not sound again. Remember to set the boolean to true again once the distance goes above 10

What EXACTLY don't you understand? People get frustrated when they write long helpful explanations of things and you ignore everything they said and just repeat your question.

Several of us have told you what you need to do. We've done everything except edit your program directly and that's your job.

Steve

“ i want to permanently turned off the buzzer after 3 second's.. please help”

if(Flag == false && distance > 0 && distance <= 10)
{
Flag = true;
. . .
}

The Flag can be reset somewhere else as needed.


What are you not understanding with the above suggestion ?