For an school assignment, we need to code a motion sensor. When it detects movement it will turn on a buzzer, when it doesn't it doesn't play a noise
PLEASE HELP AND THANK YOU !!!!!!
const int PIN_TO_SENSOR = 3;
int pinStateCurrent = LOW;
int pinStatePrevious = LOW;
const int buzzerPin = 9;
int rate = 500
int frequency = 1000000/ (rate * 2);
void setup() {
Serial.begin(9600);
pinMode(PIN_TO_SENSOR, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.print (frequency);
}
void loop() {
pinStatePrevious = pinStateCurrent;
pinStateCurrent = digitalRead(PIN_TO_SENSOR);
if (pinStatePrevious= LOW && pinStateCurrent == HIGH) {
Serial.println("Motion detected");
digitalWrite (buzzerPin,HIGH);
}
}
This is why it is also helpful to write if (LOW== pinStatePrevious) instead of if (pinStatePrevious== LOW). Then the compiler will complain if you use "=" instead of "==". The error will then not go unnoticed.
Thank you all so much for your feedback. We have edited the program via your help and it works great now! Thanks everyone, hope you all have a great day!