const int MOTION_SENSOR_PIN = 7;
const int RELAY_PIN = A5;
int motionStateCurrent = LOW;
int motionStatePrevious = LOW;
void setup() {
Serial.begin(9600);
pinMode(MOTION_SENSOR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
motionStatePrevious = motionStateCurrent;
motionStateCurrent = digitalRead(MOTION_SENSOR_PIN);
if (motionStatePrevious == LOW && motionStateCurrent == HIGH) {
Serial.println("Motion detected!");
digitalWrite(RELAY_PIN, HIGH);
}
else
if (motionStatePrevious == HIGH && motionStateCurrent == LOW) {
Serial.println("Motion stopped!");
digitalWrite(RELAY_PIN, LOW);
}
}