// BY IRFXN.ISMAIL-ADAM IZZAT-ZAFRAN
#define MOTION_SENSOR_PIN 2 // The Arduino Nano pin connected to the motion sensor
int motion_state = LOW; // current state of the pin
int prev_motion_state = LOW; // previous state of the pin
void setup() {
Serial.begin(9600); // Initialize the Serial communication
pinMode(MOTION_SENSOR_PIN, INPUT); // Set Arduino pin to input mode
}
void loop() {
prev_motion_state = motion_state; // store old state
motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state
if (prev_motion_state == LOW && motion_state == HIGH) {
// Motion detected
Serial.println("Motion detected!");
// TODO: turn on alarm, light, or activate a device here
}
else if (prev_motion_state == HIGH && motion_state == LOW) {
// Motion stopped
Serial.println("Motion stopped!");
}
delay(100); // small delay to avoid bouncing issues
}