MP3 Shield + PIR sensor not working

Hi guys, I want my arduino with MP3 shield to play a song (one song, track001.mp3) when movement is noticed by the PIR sensor I'm using. This seemed to work fine (song played when movement was noticed), but after a couple of times the song started to play by itself (without detecting movement by the PIR sensor) after 20-30 sec. Do you guys know what the problem could be?
Please help me!
Thanks in advance

The sketch:

#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h> 
#include <SFEMP3Shield.h>

SdFat sd;
SFEMP3Shield MP3player;

int pirPin = 5;    //the digital pin connected to the PIR sensor's output

void setup() {

  Serial.begin(9600);
  pinMode(pirPin, INPUT);
    digitalWrite(pirPin, LOW);
  //start the shield
  sd.begin(SD_SEL, SPI_HALF_SPEED);
  MP3player.begin();
  MP3player.setVolume(10, 10);
}

//do something else now
void loop() {

  if (digitalRead(pirPin) == HIGH){
      MP3player.playTrack(1);
      digitalWrite(pirPin, LOW);
}
  
  Serial.println("I'm bored!");
  delay(2000);

}
  if (digitalRead(pirPin) == HIGH){
      MP3player.playTrack(1);
      digitalWrite(pirPin, LOW);
}

Why are you f**king with an input pin? You can't make the PIR sensor NOT report a value!

Why are you f**king with an input pin? You can't make the PIR sensor NOT report a value!

Oke, that make sense :roll_eyes: I'm kind of a noob in programming and got most of this sketch from other projects/examples. So concider

digitalWrite(pirPin, LOW);

deleted. What should I do next to make this sketch work?

Okay removing the digital write...

#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h> 
#include <SFEMP3Shield.h>

SdFat sd;
SFEMP3Shield MP3player;

int pirPin = 5;    //the digital pin connected to the PIR sensor's output

void setup() {
  Serial.begin(9600);
  pinMode(pirPin, INPUT);

  //start the shield
  sd.begin(SD_SEL, SPI_HALF_SPEED);
  MP3player.begin();
  MP3player.setVolume(10, 10);
}

//do something else now
void loop() {
  if (digitalRead(pirPin) == HIGH)
 {
      // Add the following line to test if the Pir is detecting motion
      Serial.println("Motion detected!"
      MP3player.playTrack(1);
 }
  
  Serial.println("I'm bored!");
  delay(2000);
}

Have you connected a volt meter to the pir pin and confirmed that the signal is about 5V/3.3V when the pir sensor detects motion? Also the delay(2000) means that even if the PIR is detecting motion and generating a logic HIGH, but it occurs and goes away while the 2 second delay is happening, your code will never detect it.

If you only want something to occur after a period of time has elapsed, but still want the rest of the program working, like checking if a sensor is tripped, you can't use delay you have to use something like this.

long lastBored=0;

void setup()
{
}

loop()
{
   if ((millis() - lastBored) > 2000L)
   {
      lastBored = millis();
      Serial.println("I'm bored");
   }
}