int inputPin = 7;
int pirState = LOW;
int val = 0;
#define ECHO_PIN 2
#define TRIG_PIN 3
const int relayPin = 13;
void setup() {
pinMode(inputPin, INPUT);
pinMode( relayPin, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void loop() {
val = digitalRead(inputPin);
if (val == HIGH) {
digitalWrite(relayPin, HIGH);
delay( 600);
digitalWrite( relayPin, LOW);
if (pirState == LOW) {
Serial.println("Gempa Terdeteksi");
pirState = HIGH;
}
} else {
digitalWrite(relayPin, LOW);
if (pirState == HIGH) {
Serial.println("Gempa Berakhir!");
pirState = LOW;
}
}
float distance = readDistanceCM();
bool isNearby = distance < 100;{
digitalWrite(relayPin, isNearby);
digitalWrite(relayPin, HIGH);
delay( 1000);
digitalWrite( relayPin, LOW);
delay( 1000);
}
Serial.print("Maling Jarak : ");
Serial.println(readDistanceCM());
delay(100);
}