Bahan yang diperlukan:
- Buzzer
- PIR Motion HC-SR501
- LED Generic
- Switch Button
- Resistor 330 ohm
- Resistor 1 K
- Breadboard
- Kabel jumper
- Arduino UNO
Listing Program:
void setup()
{
//The Following are our output
pinMode(ledPin,OUTPUT);
pinMode(buzzerPin,OUTPUT);
//Button is our Input
pinMode(buttonPin, INPUT);
// Wait before starting the alarm
delay(5000);
}
void loop()
{
// To chech whether the motion is detected or not
if (digitalRead(motionPin)) {
buzzer_mode = true;
}
// If alarm mode is on,blink our LED
if (buzzer_mode){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// Switch the LED
digitalWrite(ledPin, ledState);
}
tone(buzzerPin,1000);
}
// If alarm is off
if (buzzer_mode == false) {
// No tone & LED off
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
}
// If our button is pressed Switch off ringing and Setup
int button_state = digitalRead(buttonPin);
if (button_state) {buzzer_mode = false;}
}
comment 0 komentar
more_vert