//ultrasonic
// #define PIN_TRIG 3
// #define PIN_ECHO 2
int ledpin = 8;
int inputpin = 7;
int pirState = LOW;
int val = 0;
void setup() {
// ultrasonic
// Serial.begin(115200);
// pinMode(PIN_TRIG, OUTPUT);
// pinMode(PIN_ECHO, INPUT);
pinMode(ledpin, OUTPUT);
pinMode(inputpin, INPUT);
Serial.begin(9600);
}
// 2
// float readDistanceCM() {
// digitalWrite(PIN_TRIG, LOW);
// delayMicroseconds(2);
// digitalWrite(PIN_TRIG, HIGH);
// delayMicroseconds(10);
// digitalWrite(PIN_TRIG, LOW);
// int duration = pulseIn(PIN_ECHO, HIGH);
// return duration * 0.034 / 2;
// }
void loop() {
// 1
// digitalWrite(PIN_TRIG, HIGH);
// delayMicroseconds(10);
// digitalWrite(PIN_TRIG, LOW);
// int duration = pulseIn(PIN_ECHO, HIGH);
// Serial.print("Distance in CM: ");
// Serial.println(duration / 58);
// Serial.print("Distance in Inches: ");
// Serial.println(duration / 148);
// 2
// digitalWrite(PIN_TRIG, LOW);
// delayMicroseconds(2);
// digitalWrite(PIN_TRIG, HIGH);
// delayMicroseconds(10);
// digitalWrite(PIN_TRIG, LOW);
// int duration = pulseIn(PIN_ECHO, HIGH);
// return duration * 0.034 / 2;
// Serial.print("Jarak: ");
// Serial.println(readDistanceCM());
// delay(1000);
// pir
val = digitalRead(inputpin);
if (val == HIGH){
digitalWrite(ledpin, HIGH);
if(pirState == LOW){
Serial.println("Motion Detected");
pirState = HIGH;
}
} else {
digitalWrite(ledpin, LOW);
Serial.println("Motion Ended");
pirState = LOW;
}
}