hi
im new on arduino and im trying to do a alarm with a led, a buzzer and a movement detector.
The problem i have is that when the movement detector detects something moving the led starts blinking and the buzzer ringing but when it stops moving it stops automatically. i need to make it sound for 10 secs but i already used delay in the loop and i cant use delay anymore because of the leds.
Heres the code:
The solution to the problem is to use a non-blocking delay (actually all your delays should be non-blocking); see the BlinkWithoutDelay example that comes with the IDE).
I've only implemented the main timing and kept your delays in place.
// variable to keep track of current 'time'
unsigned long currentMillis;
void loop()
{
// starttime of beep / buzz; also used as a flag to indicate that beep / buzz is in progress for 10 seconds
static unsigned long starttime = 0;
// get current 'time'
currentMillis = millis();
int value = digitalRead(PIRPin);
// if triggered or beep / buzz in progress
if (value == HIGH || starttime != 0) {
// set start time
starttime = currentMillis;
tone(9, 1500);
delay(300);
noTone(9);
delay(300);
digitalWrite(LEDPin, HIGH);
delay(100);
digitalWrite(LEDPin, LOW);
delay(100);
// check if beep / buzz duration has lapsed
if(currentMillis - starttime >= 10000)
{
// indicate 10 seconds passed
starttime = 0;
}
}
else
{
digitalWrite(LEDPin, LOW);
tone(10000, 00);
}
}