// beep on certain period_
const int buzzer = 12;
const long onDuration = 5;// OFF time for LED
const long offDuration = 2000;// ON time for LED
int BUZState = HIGH; // initial state of LED
long rememberTime = 0; // this is used by the code
int PIRread = 0;
int PIRpin = 13;
int PIRreadBJ = 0;
int maxNum = 6;
int i = 0;
int ledPin = 12; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 5; // milliseconds of on-time
long OffTime = 2000; // milliseconds of off-time
void setup() {
Serial.begin(9600);
pinMode(buzzer, OUTPUT); // define buzzer as output
digitalWrite(buzzer, BUZState); // set initial state
}
void loop() {
PIRread = digitalRead(PIRpin);
Serial.print("PIR =");
Serial.println(digitalRead(PIRpin));
if (PIRread == HIGH)
{
PIRreadBJ = 1;
}
if (PIRreadBJ == 1) {
for (int i = 0; i <= maxNum; i++)
{
BUZloopO(); //
}
if (i == maxNum)
{
digitalWrite(ledPin, LOW);
}
}
}
void BUZloopO() {
unsigned long currentMillis = millis();
if ((BUZState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
BUZState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, BUZState); // Update the actual LED
}
else if ((BUZState == LOW) && (currentMillis - previousMillis >= OffTime))
{
BUZState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, BUZState); // Update the actual LED
}
}