Buzzer Help

I am using buzzer with ultrasonic sensor and want the buzzer to stop aftrer 3 second when object is closer and stay there, I root followng program for buzzer,

if (distance < 5) {
digitalWrite(buzzer, LOW);
}
else {
digitalWrite(buzzer, HGH);
}
delay(500);
} // delay function is for whole loop.

If i add delay(3000) in above program the whole funtion blocked by this delay.

Kindly help me to stop buzzer automatically after 3 sec when the distance is still < 5cm.

Hi, Please use Code Tags and show us your entire code...

Take a look at Blink without delay.

Pieter

terryking228: Hi, Please use Code Tags and show us your entire code...

#define trigPin 7
#define echoPin 6
#define led 12
#define led2 11
#define led3 10
#define led4 9
#define led5 8
#define auto A1
#define pumpled 2
#define buzzer 3
#define relay 4
#define button A2

int relayState = HIGH;

int buttonState; // the current reading from the input pin
int lastButtonState = LOW;

unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(auto, OUTPUT);
pinMode(pumpled, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(button, INPUT);

}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(60);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
int reading = digitalRead(button);

if (reading != buttonState) {
buttonState = reading;

if (buttonState == HIGH || distance > 10)
{digitalWrite(relay, LOW);
digitalWrite(pumpled, HIGH);}}

if (distance <= 60) {
digitalWrite(led, HIGH);

}
else {
digitalWrite(led,LOW);

}
if (distance < 50) {
digitalWrite(led2, HIGH);

}
else {
digitalWrite(led2, LOW);
}
if (distance < 40) {
digitalWrite(led3, HIGH);
}
else {
digitalWrite(led3, LOW);
}
if (distance < 30) {
digitalWrite(led4, HIGH);

}
else {
digitalWrite(led4,LOW);
}
if (distance < 5) {
digitalWrite(led5, HIGH);

}
else {
digitalWrite(led5,LOW);

}
if (distance >= 40) {
digitalWrite(relay, LOW);
digitalWrite(auto, HIGH);
}
if (distance < 10) {
digitalWrite(relay, HIGH);
digitalWrite(pumpled, LOW);
digitalWrite(auto, LOW);

}
if (distance < 5) {
digitalWrite(buzzer, LOW); // this function i.e near to sensor
}
else {
digitalWrite(buzzer, HIGH); // this function
}

delay(500);
}

I want the buzzer to stop after 3 secs, when the (water level)distance < 5, and (water level)remains < then five until is > 5 and run buzzer for 3 sec when distance < 5 again. I would be thankful if anyone add the function in above code

You forgot the code tags terryking228 asked you to use. It’s the </> button on the toolbar. The reason this is important is because the forum software may mistake parts of your code for markup, this causes confusion, wastes our time, and makes it less likely for you to get help with your question. Code tags also make it easier to read and copy to the IDE or editor for testing.

Please always do Tools > Auto Format on your code before posting it on the forum.

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)