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