Acts differently if plugged into power than USB

kinda new to arduino but loving it!!

new project with arduino UNO and MP3 shield where it randomly plays a song from the sd card when it detects motion.

when plugged into PC, it acts as it should. it waits for motion then plays the song on SD card.
THIS WORKS

when plugged in directly to wall wort, music starts playing automatically. no motion, just starts playing a song. after first song, IT STOPS AND WAITS FOR MOTION AGAIN as it should.

also whats interesting is that when it starts playing using wall wort, I’ll unplug it and wait and plug back in (or hit reset) and it plays the same song… not random…

I am hoping someone could help… Please??

sorry for the lack of commenting in the code…

here it is:

#include <SPI.h>

//Add the SdFat Libraries
#include <SdFat.h>
#include <SdFatUtil.h>

//and the MP3 Shield Library
#include <SFEMP3Shield.h>



long randNumber;
int pirPin = 3;
int calibrationTime = 10;

SdFat sd;
SFEMP3Shield MP3player;






void setup() {

 pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);
 
     

   //Initialize the SdCard.
  if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
  if(!sd.chdir("/")) sd.errorHalt("sd.chdir");

  //Initialize the MP3 Player Shield
  MP3player.begin();
}






void loop() {
 digitalWrite(pirPin, LOW);
  MP3player.available();

  randNumber = random(1,16);
  if(digitalRead(pirPin) == HIGH) {
    Serial.print(digitalRead(pirPin));
    MP3player.playTrack(randNumber);}


  delay(100);
}

ok.. so the same song playing on reset was just a random cooincidence. if I hit it a few times it will change songs..

seems main problem is that it wants to play a file as soon as its plugged in to wall wort.. but does not play file while plugged into USB of computer.

TIA.

Pete

also whats interesting is that when it starts playing using wall wort, I'll unplug it and wait and plug back in (or hit reset) and it plays the same song.. not random..

You need to [u]seed[/u] the random number generator once in your setup routine. (Hopefully with a different, somewhat random seed each time you start-up.)

when plugged in directly to wall wort, music starts playing automatically. no motion, just starts playing a song.

Try adding a delay in setup(), or send a "sound off" (or whatever the command is to stop the sound) command at the end of setup().

Or you might need to read your PIR into a variable instead of acting-on the direct read, and initialize that variable to zero during setup.

To aid troubleshooting, I suggest you power a Led when the motion detector pin is HIGH.
The Serial.print statement will work only when it is connected to a PC (and there is a corresponding Serial.begin(), missing in your case).