Turning the PIR Sensor On/Off

The purpose of this code is to turn on and off a PIR Sensor, which activates the piezo buzzer if the sensor detects movement. The activation of the buzzer portion of the code works, but I am stuck on the on and off switch part. I attempted to use an if-else statements, but the code didn't work. Would anyone mind taking a look at my code?
Ty in advance

int speakerOut = 9;//Piezo buzzer's positive terminal is connected to digital pin 9               
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
const int BUTTON = 7;
const int MOTION = 2; 
int val = 0;
int statePin = LOW;
void siren();

 volatile byte intruder;
 void setup()
 {
  
   Serial.begin(115200);
   attachInterrupt(0, intruder_detect, RISING);//Initialize the intterrupt pin for the motion sensor (Arduino digital pin 2)
   intruder = 0;
 }
 void loop(){
  val = digitalRead(BUTTON);

  if (val == HIGH) {
    digitalWrite(MOTION, HIGH);
  } else {
    digitalWrite(MOTION, LOW);
  }
  
 }
 void intruder_detect()//This function is called whenever an intruder is detected by the arduino
 {
   intruder++;
   Serial.println("Intruder detected");
   for(int i=0; i<3; i++)//Play the alarm three times
   siren();
 }
 void siren()//This function will make the alarm sound using the piezo buzzer
{
for (count = 0; count < MAX_COUNT; count++) {
      for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {       
          analogWrite(speakerOut,1023);
          delayMicroseconds(tones[count2]);
          analogWrite(speakerOut, 0);
          delayMicroseconds(tones[count2]);
        } 
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          analogWrite(speakerOut, 0);
          delayMicroseconds(100);
        }
      }
    }
  }
}

You don't turn the sensor on and off. It is on. You might elect to read, or not, the sensor.

I have no idea what you have connected to pins 2 and 7 but maybe try configuring pin 2 as an output before you write to it.

Steve

PaulS:
You don't turn the sensor on and off. It is on. You might elect to read, or not, the sensor.

Doesn't my code already accomplish this?
The lines here:
void loop(){
val = digitalRead(BUTTON);

if (val == HIGH) {
digitalWrite(MOTION, HIGH);
} else {
digitalWrite(MOTION, LOW);
}

Doesn't my code already accomplish this?

No. It does not make sense to use digitalWrite() on an input pin. All that does is turn the internal pullup resistor on or off. And, doing that makes no sense.

PaulS:
No. It does not make sense to use digitalWrite() on an input pin. All that does is turn the internal pullup resistor on or off. And, doing that makes no sense.

If digitalWrite doesn't accomplish this task, then what would? Do you mind offering some suggestions as to how I should I approach this?

slipstick:
try configuring pin 2 as an output before you write to it.

If you didn't understand that you could have asked. Try looking up pinMode() in the reference (top of the page, under Resources).

Steve

If digitalWrite doesn't accomplish this task, then what would?

Do you have a clock? Do you turn it off when you don't need to know what time it is? Of course you don't. You just don't look at it. If you don't want to know if there is motion, or not, DO NOT LOOK AT THE SENSOR.

  val = digitalRead(BUTTON);

  if (val == HIGH) 
  {
    pirState = digitalRead(MOTION);  //pirState needs to be defined
  }

or maybe

  val = digitalRead(BUTTON);

  if (val == HIGH) 
  {
    if(digitalRead(MOTION)) //pirState needs to be defined
    {
        intruder_detect();
    }
  }

evanmars:

  val = digitalRead(BUTTON);

if (val == HIGH)
 {
   pirState = digitalRead(MOTION);  //pirState needs to be defined
 }




or maybe



val = digitalRead(BUTTON);

if (val == HIGH)
 {
   if(digitalRead(MOTION)) //pirState needs to be defined
   {
       intruder_detect();
   }
 }

Thanks, I'll try it out.

PaulS:
Do you have a clock? Do you turn it off when you don't need to know what time it is? Of course you don't. You just don't look at it. If you don't want to know if there is motion, or not, DO NOT LOOK AT THE SENSOR.

I understand your analogy. I'll keep that in mind