Arduino motion sensor code not working :(

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


}




}

Maybe this helps

At no time do you use this line... follow @Kai-R and learn how the program works. Have fun with all your projects.

forgot the ;

change this to Serial.println(frequency); so you get a new, clean line once you enter void loop()

Oops, watch that usage of = vs ==

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!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.