Go Down

Topic: Wav player with motion sensor keeps looping sound with LED commands (Read 188 times) previous topic - next topic

Shotran

I have an issue combining a wav player and LEDs. The wav player plays a sound when motion sensor is on - play the sound all the way through once turned on without any stop in the middle of play, after it played a sound, it stops until it senses other motion.
However, when I add LED commands in the code to turn the LED on while the sound is being played, it keeps looping the sound no matter it senses a motion or not.

I wonder if someone could solve this issue and it will be very helpful if there is any advise for this.

Here is the code:

Code: [Select]
#include <SD.h>                      // need to include the SD library
#define SD_ChipSelectPin 10  //using digital pin 4 on arduino nano 328 and53 on MEGA
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>
#define led1 2
#define led2 3
#define led3 4

TMRpcm tmrpcm;   // create an object for use in this sketch
 int SW1;
 
void setup(){
 pinMode(A0,INPUT);  //Define A0 as digital input.
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 
  tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
 
//  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
//  return;   // don't do anything more if not
//  }

  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail"); 
    return;   // don't do anything more if not

  }
  else{   
    Serial.println("SD ok");   
  }

 tmrpcm.volume(7); //0 to 7 volume level
 tmrpcm.play("0.wav"); //the sound file "0" will play each time the arduino powers up, or is reset
 
}
 
void loop(){
  SW1=digitalRead(A0);
 
  if (SW1 == HIGH && !tmrpcm.isPlaying()) { //if SW1 pressed and is not playing then play
    tmrpcm.play("1.wav");
    digitalWrite(led1,HIGH);
    digitalWrite(led2,HIGH);
    digitalWrite(led3,HIGH);
    }
  if (!tmrpcm.isPlaying()) {  //if it isn't playing turn the LEDs off
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }     
}

Go Up