Author Options:

I need help coding an arduino sketch to activate audio from an mp3 board when my PIR senses motion Answered

My goal is to have a sound effect play when a person first walks into the field of view of my PIR. So far I can get it to work, but the sound "stutters" for about 4 seconds before it will play through all the way.
The parts that I am using are:
http://www.mdfly.com/products/microsd-tf-card-mp3-wav-player-module-ttl.html (this board doesn't have a datasheet, so i have been using the 'old version' datasheet: http://mdfly.net/Download/Module/AU5016.pdf )
and an arduino uno

besides vcc and ground, my arduino's pins connect pin7 to my mp3 board's number 1 pin (which is active low) and also from my arduino's pin3 to the out/signal from my PIR sensor.
I have tried a couple different example sketches and modified them to something that i thought would work, but they both have similar problems with "stuttering". Below are the sketches after modification. (note: I haven't finished changing some of the commenting yet.)
I would be very thankful for any and all help. I can admit, I'm not very good at this kind of thing. This project is for halloween in 2016. I'm so bad (and I realize it) that I'm starting a year in advance.

// Uses a PIR sensor to detect movement, buzzes a buzzer
// more info here: http://blog.makezine.com/projects/pir-sensor-arduino-alarm/
// email me, John Park, at jp@jpixl.net
// based upon:
// PIR sensor tester by Limor Fried of Adafruit
// tone code by michael@thegrebs.com

int MP3Song1Pin = 7;                // choose the pin for the mp3 board
int inputPin = 3;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

void setup() {
  pinMode(MP3Song1Pin, OUTPUT);      // declare mp3 board as output
  pinMode(inputPin, INPUT);     // declare sensor as input

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(MP3Song1Pin, LOW);  // turn mp3 board on
    digitalWrite(MP3Song1Pin, HIGH);  // turn mp3 board

    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
  } else {
      digitalWrite(MP3Song1Pin, HIGH); // turn mp3 board OFF
      if (pirState == HIGH){
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;

* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
* @date:   3. September 2006
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* (http://www.parallax.com/detail.asp?product_id=555-28027)
* The sensor's output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time,
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-phases shorter than a given time,
* assuming continuous motion is present during these phases.


//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;       

//the time when the sensor outputs a low impulse
long unsigned int lowIn;        

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000; 

boolean lockLow = true;
boolean takeLowTime; 

int pirPin = 3;    //the digital pin connected to the PIR sensor's output
int ledPin = 7;

void setup(){
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");

void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, LOW);   //the led visualizes the sensors output pin state
       digitalWrite(ledPin, HIGH);
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;           
         Serial.print("motion detected at ");
         Serial.println(" sec");
         takeLowTime = true;

     if(digitalRead(pirPin) == LOW){      
       digitalWrite(ledPin, HIGH);  //the led visualizes the sensors output pin state

        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");