#include <ESP32Servo.h>
#define PIR 15
#define SERVO 13
#define RELAY 2
#define SWITCH 12
#define BUZZ 14
int val = 0;
int pos = 0;
int state = LOW;
Servo servo;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(PIR, INPUT);
pinMode(RELAY, OUTPUT);
pinMode(BUZZ, OUTPUT);
pinMode(SWITCH, INPUT_PULLUP);
servo.attach(SERVO);
}
void buzzbuzz(){
tone(BUZZ,150);
noTone(BUZZ);
delay(1000);
}
void switchRelay(){
int value = digitalRead((SWITCH));
if (value == 0){
digitalWrite(RELAY, HIGH);
} else {
digitalWrite(RELAY, LOW);
}
}
void myServo(){
for(pos = 0; pos <= 180; pos++){
servo.write(pos);
Serial.println(pos);
delay(15);
}
for(pos = 180; pos >= 0; pos--){
servo.write(pos);
Serial.println(pos);
delay(15);
}
}
void pirLED(){
val = digitalRead(PIR);
if(val == HIGH){
if(state == LOW){
servo.write(180);
Serial.println("Gerakan Terdeteksi!");
tone(BUZZ,400,1000);
state = HIGH;
}
} else {
if (state == HIGH){
servo.write(90);
Serial.println("Tidak Ada Gerakan");
state = LOW;
}
}
}
void loop() {
// put your main code here, to run repeatedly:
pirLED();
//myServo();
switchRelay();
// buzzbuzz();
delay(10); // this speeds up the simulation
}