#include <Servo.h>
Servo myServo; // Membuat objek servo untuk mengontrol motor servo
const int pirPin = 2; // PIR sensor connected to digital pin 2
const int buzzerPin = 9; // Pin for the buzzer
const int ledPin = 8; // Pin for the LED
const int servoPin = 10; // Pin for the Servo motor
void setup() {
myServo.attach(servoPin); // Attaches the servo on pin 10 to the servo object
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
// Motion detected
tone(buzzerPin, 1000); // Sound the buzzer at 1000 Hz
digitalWrite(ledPin, HIGH); // Turn on the LED
myServo.write(90); // Move servo to 90 degrees
delay(1000); // Wait for 1 second
noTone(buzzerPin); // Turn off the buzzer
digitalWrite(ledPin, LOW); // Turn off the LED
myServo.write(0); // Return servo to 0 degrees
delay(1000); // Wait for 1 second
} else {
// No motion detected
noTone(buzzerPin); // Ensure the buzzer is off
digitalWrite(ledPin, LOW); // Ensure the LED is off
}
}