Code is not working unless the reset button is pressed

Hello everyone,

I have an issue where the code I have written only works if the reset button is pressed. I am using a genuine Arduino Pro Mini 5V, 16 MHz (ATmega 328P) for a model boat project. When the board is connected to my computer it works fine (I can upload programs and they do what I want). However when the board is disconnected from the computer, my code cease to work unless, once it's powered on, I press the reset button; then it is working as intended.

I'm not sure why it's behaving that way and I am hoping someone can help me. From my google research it may be linked to the bootloader but that's something I am wholly ignorant about.

Here is my code :

#include <SoftwareSerial.h>
#include <SPI.h>
#include <PWMServo.h>
#include <DFRobotDFPlayerMini.h>
#include <RF24.h>

//Pin definition
#define CSN_PIN       A0
#define CE_PIN        A1
#define TX            4
#define RX            2
#define ENABL_PIN     6
#define PHASE_PIN     5
#define SERVO_PIN     10

#define MAX_DEFLECT   45
#define NEUTRAL       90
#define NB_TRCK       3
#define DEADZONE      20

#define TIMEOUT       250

struct payload
{
  int joy_x;
  int joy_y;
  byte buttons;
};
payload data_to_receive;
payload previous_data;

SoftwareSerial secondarySerial(RX, TX); // RX, TX
DFRobotDFPlayerMini dfmp3;

PWMServo rudder;

uint8_t address[][6] = { "1Node", "2Node" };
RF24 radio(CE_PIN,CSN_PIN);

//Variables to store input from gamepad and gamepad status
bool is_playing = false;
bool radio_status = false;
bool com_started = false;
bool led_on = false;
int motor_input = 0;
int rudder_input = 90;
uint8_t current_trck = 1;
unsigned long previous_time = 0;
unsigned long last_valid_msg = 0;
unsigned long blink_timer = 0;

void setup()
{
  int attempt = 0;
  bool success = false;
  
//  Serial.begin(9600);
//  Serial.println(F("Let the show begin !"));
//  while (!Serial)
//  {}

  do
  {
    radio_status = radio.begin();
//    if(!radio_status)
//    {
//      Serial.println(F("Radio hardware is not responding !"));
//    }
//    else
//    {
//      Serial.println(F("Radio started !"));
//    }
  }while(!radio_status);
  radio.setChannel(105);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(address[1]);
  radio.openReadingPipe(1, address[0]);
  radio.startListening();
  
  //From datasheet : 500 µs = 0°, 2500 µs = 180°, 1500 µs = 90° (neutral)
  rudder.attach(SERVO_PIN, 500, 2500);

  //Set Motor
  pinMode(PHASE_PIN, OUTPUT);
  pinMode(ENABL_PIN, OUTPUT);

  //pinMode(LED_BUILTIN, OUTPUT);
  
  //init data
  data_to_receive.joy_x = 512;
  data_to_receive.joy_y = 512;
  data_to_receive.buttons = 0;
  memcpy(&previous_data, &data_to_receive, sizeof(payload));

  secondarySerial.begin(9600);
  do
  {
    success = dfmp3.begin(secondarySerial);
    attempt +=1;
//    Serial.print(F("Attempt : "));
//    Serial.println(attempt);
//    Serial.print(F("Is it starting : "));
//    Serial.println(success);
  }while(attempt<10 && !success);
  dfmp3.setTimeOut(500);
  dfmp3.volume(15);  // Between 0 and 30
  // Play "Larguez les amaaaaaaares !"
  dfmp3.advertise(1);  // sd:/advert/0001.mp3
  delay(1000);
  dfmp3.playMp3Folder(current_trck);
  is_playing = true;
  previous_time = millis();
}

void loop()
{ 
  uint8_t pipe;
  unsigned long current_time = 0;
  
//  current_time = millis();
//  if((current_time - blink_timer)>= 1000)
//  {
//    if(led_on)
//    {
//      digitalWrite(LED_BUILTIN, LOW);
//      led_on = false;
//      Serial.println("LED OFF");
//    }
//    else
//    {
//      digitalWrite(LED_BUILTIN, HIGH);
//      led_on = true;
//      Serial.println("LED ON");
//    }
//    blink_timer = current_time;
//  }
   
  if(radio.available(&pipe))
  {
    radio.read(&data_to_receive, sizeof(payload));
//    Serial.print(F("Joystick input Y :"));
//    Serial.println(data_to_receive.joy_y);
//    Serial.print(F("Joystick input X :"));
//    Serial.println(data_to_receive.joy_x);
    if(data_to_receive.joy_x != 0 || data_to_receive.joy_y != 0 || data_to_receive.buttons!=0)
    {
      memcpy(&previous_data, &data_to_receive, sizeof(payload));
      if(!com_started)
      {
        com_started = true;
      }
      last_valid_msg = millis();
    }
  }

  current_time = millis();
  if((abs(current_time - last_valid_msg)> TIMEOUT) && com_started && (abs(current_time - previous_time)>50))
  {
//    Serial.println(F("Radio hardware stopped responding!"));
//    Serial.print(F("Current time is : "));
//    Serial.println(current_time);
//    Serial.print(F("Last msg time was : "));
//    Serial.println(last_valid_msg);  
//    Serial.println(F("Attempting restart"));
    radio_status = radio.begin();
    if(radio_status)
    {
      radio.setChannel(105);
      radio.setDataRate(RF24_250KBPS);
      radio.setPALevel(RF24_PA_LOW);
      radio.openWritingPipe(address[1]);
      radio.openReadingPipe(1, address[0]);
      radio.startListening();
      previous_time = current_time;
//      Serial.println(F("Restart success !"));
    }
//    else
//    {
//      Serial.println(F("Restart failed !"));
//    }
  }
  
  if(abs(current_time - previous_time) > 50)
  {  
    if(previous_data.joy_y<(511 - DEADZONE))
    {
      digitalWrite(PHASE_PIN, LOW);
      motor_input = (512 - previous_data.joy_y)/2;
//      Serial.print(F("Motor input backward : "));
//      Serial.println(motor_input);
    }
    else if(previous_data.joy_y>(511 + DEADZONE))
    {
      digitalWrite(PHASE_PIN, HIGH);
      motor_input = (previous_data.joy_y - 511)/2;
//      Serial.print(F("Motor input forward : "));
    }
    else
    {
//      Serial.print(F("No Motor input "));
      motor_input = 0;
    }
    motor_input = constrain(motor_input, 0, 255);
    analogWrite(ENABL_PIN, motor_input);

    rudder_input = map(previous_data.joy_x, 0, 1023, NEUTRAL-MAX_DEFLECT, NEUTRAL+MAX_DEFLECT);
    rudder.write(rudder_input);

    if(dfmp3.available())
    {
      if((dfmp3.readType() == DFPlayerPlayFinished) && is_playing)
      {
        if(current_trck == NB_TRCK)
        {
          current_trck = 1;
        }
        else
        {
          current_trck += 1;
        }
        dfmp3.playMp3Folder(current_trck);
      }
    }
    
    if(previous_data.buttons & 0x10)
    {
      dfmp3.reset();
    }
    if(previous_data.buttons & 0x01)
    {
      dfmp3.volumeUp();
    }          
    if(previous_data.buttons & 0x04)
    {
      dfmp3.volumeDown();
    }
    if(previous_data.buttons & 0x08)
    {
      if(current_trck == 1)
      {
        current_trck = NB_TRCK;
      }
      else
      {
        current_trck -= 1;
      }
      dfmp3.playMp3Folder(current_trck);
    }
    if(previous_data.buttons & 0x02)
    {
      if(current_trck == NB_TRCK)
      {
        current_trck = 1;
      }
      else
      {
        current_trck += 1;
      }
      dfmp3.playMp3Folder(current_trck);
    }
//    if(previous_data.buttons & 0x20)
//    {
//      if(is_playing)
//      {
//        dfmp3.pause();
//        is_playing = false;
//      }
//      else
//      {
//        dfmp3.start();
//        is_playing = true;
//      }
//    }
//    if(previous_data.buttons & 0x40)
//    {
//      dfmp3.advertise(2);
//    }
    previous_time = current_time;
  }
}

Thank you in advance for your answers.

That sounds like a hardware problem. Can you post an annotated schematic be sure to show all of the incoming power connections and associated circuitry.

Thank you for your answer.

Here are the schematics I have done when designing the PCB the Arduino Pro is mounted on. Note that the Arduino is never powered from the USB port, it is always powered by a 5V regulator on my PCB.

The PCB is powered by two 9V batteries in parallel (currently it's powered by a lab power supply). This voltage is fed to the motor regulator that provides an output protected against revers current. This output provides 9V to two 5V regulators : one for the nRF24 board and one for the Arduino, the MP3 player and the servo. The total current draw in idle is 0.07 A. When the motor is running at full speed and the servo is turning and the sound is at its max output, the total current draw rises to 0.5 A.

The 100 µF capacitors ended up being replaced by 330 µF capacitors, other capacitors are 0.1 µF.