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) ;
}
}