Skip to content

Instantly share code, notes, and snippets.

@antonprafanto
Created November 10, 2024 12:41
Show Gist options
  • Select an option

  • Save antonprafanto/05d6afe6dbfe085988cd3a8f47874408 to your computer and use it in GitHub Desktop.

Select an option

Save antonprafanto/05d6afe6dbfe085988cd3a8f47874408 to your computer and use it in GitHub Desktop.
#define timeSeconds 10
// Tentukan GPIO untuk LED dan Sensor Gerak PIR
const int led = 26;
const int motionSensor = 27;
// Variabel tambahan
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;
boolean motion = false;
// Fungsi untuk mendeteksi gerakan
void IRAM_ATTR detectMovement() {
digitalWrite(led, HIGH);
startTimer = true;
lastTrigger = millis();
}
void setup() {
// Mulai komunikasi serial
Serial.begin(115200);
// Atur sensor gerak PIR sebagai INPUT PULLUP
pinMode(motionSensor, INPUT_PULLUP);
// Atur interupsi untuk sensor gerak PIR
attachInterrupt(digitalPinToInterrupt(motionSensor), detectMovement, RISING);
// Atur LED sebagai output
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
// Dapatkan waktu saat ini
now = millis();
// Periksa jika LED hidup dan gerakan terdeteksi
if (digitalRead(led) == HIGH && motion == false) {
Serial.println("MOTION DETECTED!!!");
motion = true;
}
// Matikan LED setelah waktu tertentu
if (startTimer && (now - lastTrigger > (timeSeconds * 1000))) {
Serial.println("Motion stopped...");
digitalWrite(led, LOW);
startTimer = false;
motion = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment