need help! Code reacting differently when power on and power on + Reset

Hi all!

I’m having a problem with my set up.
I have a Uno with a Grove mp3 Serial (pin 10,11, grnd and 3,3v), and some speakers. I want to make a “stand-alone” mp3 player, playing one song as loop, until there’s no more power, and start again when power is back on, without having anyone to do anything.

My Code is working fine, the mp3 song is playing as a loop as asked.

When I power the Uno with a usb from computer, it works fine.
When I power the Uno with 12v and press Reset, it works fine.
When I power the Uno with 12v, it starts, but don’t play the loop, just play the song once and then stops. ??
That means it still read the code, and launch, so why just not lauching the loop as well??

Anyone has a clue why it is not working?
How to make it works without having to reset?

Here’s the code in case it’s coming from there:

#include <SoftwareSerial.h>
SoftwareSerial mp3 ( 11, 10); //modify this with the connector you are using.


void setup()
{
	mp3.begin(9600);
	Serial.begin(9600); 
	delay(1000);
        if (true ==SetPlayMode(0x01))
        Serial.println("Set The Play Mode to 0x01, Single Loop Mode.");
        else
        Serial.println("Playmode Set Error");
        PauseOnOffCurrentMusic();
}

void loop()
{  
SetPlayMode(0x01);
delay(1000);
SetMusicPlay(00,01);
delay(1000);
SetVolume(0x1E);
while(1);
}

//Set the music index to play, the index is decided by the input sequence
//of the music;
//hbyte: the high byte of the index;
//lbyte: the low byte of the index;
boolean SetMusicPlay(uint8_t hbyte,uint8_t lbyte)
{
	mp3.write(0x7E);
	mp3.write(0x04);
	mp3.write(0xA0);
	mp3.write(hbyte);
	mp3.write(lbyte);
	mp3.write(0x7E);
        delay(100);
	 while(mp3.available())
	{
	if (0xA0==mp3.read())
	return true;
	else 
	return false;
        }
}

// Pause on/off  the current music
boolean PauseOnOffCurrentMusic(void)
{
 mp3.write(0x7E);
 mp3.write(0x02);
 mp3.write(0xA3);
 mp3.write(0x7E);
 delay(10);
 while(mp3.available())
 {
 if (0xA3==mp3.read())
 return true;
 else 
 return false;
 }
}



//Set the volume, the range is 0x00 to 0x1F
boolean SetVolume(uint8_t volume)
{
 mp3.write(0x7E);
 mp3.write(0x03);
 mp3.write(0xA7);
 mp3.write(volume);
 mp3.write(0x7E);
 delay(10);
 while(mp3.available())
 {
 if (0xA7==mp3.read())
 return true;
 else 
 return false;
 }
}


boolean SetPlayMode(uint8_t playmode)
{
  if (((playmode==0x00)|(playmode==0x01)|(playmode==0x02)|(playmode==0x03))==false)
  {
    Serial.println("PlayMode Parameter Error! ");
    return false;
  }
   mp3.write(0x7E);
 mp3.write(0x03);
 mp3.write(0xA9);
 mp3.write(playmode);
 mp3.write(0x7E);
 delay(10);
 while(mp3.available())
 {
 if (0xA9==mp3.read())
 return true;
 else 
 return false;
 }
}

Sorry if it’s seems a newby question… Never worked with a stand-alone-without-computer-linked Arduino project!!

Thanks a lot if you can help me on this!! :slight_smile:

After setting play mode in setup(), why do you need to set it again on every pass through loop()?

If you are going to go into an infinite loop after starting the song playing, why the f**k are you doing that in loop()? Just start the damned thing in setup(), and leave loop() empty, like you have a clue.

When I move the code from loop() to setup(), the problem is the same...
The loop of the song works if I power it on with a 12Vdc and press Reset.
Doesn't work is i just power it on....

Just a thought - the software serial pins are floating at power up and causing spurious reads - perhaps pull-up resistors so
serial lines are idle at startup?

If the 5V rail doesn't come straight up to 5V you might get some logic errors during initialisation. Perhaps the brownout detector would help with that by keeping the Arduino in reset until the voltage has reached the threshold for reliable operation.

It may be that the external hardware needs a finite time to initialise itself on powerup before it is ready to talk to the Arduino. Perhaps a delay at the start of setup() would help with that.

Heya!

Finally made it!!
I had no resistors right near me, so tried what you both were saying by just adding a delay(1000) at the beginning of the setup(), before everything else...
Yeaheee!!

Thanks a lot for your help!