#define MOTION_SENSOR_PIN 15
#define BUZZER_PIN 33
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!, making sound");
digitalWrite(BUZZER_PIN, HIGH);
}
else if(motionStatePrevious == HIGH && motionStateCurrent == LOW){
Serial.println("Motion stopped!, stops making sound");
digitalWrite(BUZZER_PIN, LOW);
}
}