RX/TX problem: ARDUINO code on ATMEGA328P

I'm using ATMEGA328P implement on a miniPCB to control the Raspberry Pi Start and control LED Status. The code (that has been written on ARDUINO IDE) works fine when ATMEGA328P is plugged on Arduino UNO board, but the same code on the PCB, on the first start, the LED has an strange behaviour, switching on the LED when the LED should be off.

If I connect the ATMEGA328P miniPCB RX/TX to Arduino board the behaviour is perfect, but if I disconnect RX lines from Arduino board, the LED does something strange the first time.

I have deleted Serial.begin and all serial commands, but it doesn't solve it, What could be the problem?

EDIT: I add the code and schematic:

EDIT 2: I have solved the problem adding a delay(400) on setup part, but I don't understand why this solves the problem.

Crystal is 16 MHz and value capacitor is 20 pF.

#define POWER_ON_TIME 1500 //preprocessor calling to save memory

const uint8_t  glob_plugged_crtl = 4; //take the control of RUN RPI pin.
const uint8_t  glob_led_red =5;       //Physical pin = 13 Connected to GPIO5 on RPI.
const uint8_t  glob_led_green=6;     //Physical pin = 18 Connected to GPIO6 on RPI.
const uint8_t  glob_led_blue = 7;     //Physical pin = 5 Connected to GPIO13 on RPI.
const uint8_t  glob_button_pin = 8;   //Physical pin = 14 Connected to ttp223 out.  
const uint8_t  glob_led_red_out =9;    //Physical pin = 12 Connected to Red Led.
const uint8_t  glob_led_green_out = 10; //Physical pin = 15 Connected to Green Led.
const uint8_t  glob_led_blue_out = 11;  //Physical pin = 6 Connected to Blue Led. 
const uint8_t  glob_override=12;  //Physical pin = 16 Connected to GPIO23 on RPI.
const uint8_t  glob_powercrtl=13;    //Physical pin = 17 Connected to GPIO3  on RPI.


uint8_t  buttonState = 1;     // current state of the button
uint8_t  glob_last_button_state = 0; // previous state of the button
uint16_t  glob_start_pressed = 0;    // the moment the button was pressed
uint16_t  glob_end_pressed = 0;      // the moment the button was released
uint16_t  glob_hold_time = 0;        // how long the button was hold
uint8_t  holdStatus=0;

uint8_t is_pressed = 0;
//uint8_t is_awaken = 0;
uint8_t is_plugged = 0; 
uint8_t is_rising_edge = false;
uint8_t is_pending = false;
uint8_t glob_override_flag = 2; //0 falling edge, 1 rising edge, 2 no edge

void setup() {
  // put your setup code here, to run once:
  pinMode(glob_plugged_crtl, OUTPUT);
  
  pinMode(glob_led_red, INPUT);
  pinMode(glob_led_green, INPUT);
  pinMode(glob_led_blue, INPUT);

  pinMode(glob_led_red_out, OUTPUT);
  pinMode(glob_led_green_out, OUTPUT);
  pinMode(glob_led_blue_out, OUTPUT);
 
  pinMode(glob_button_pin, INPUT);
  pinMode(glob_override, INPUT);

  pinMode(glob_powercrtl, OUTPUT);
  
  digitalWrite(glob_powercrtl, HIGH);
  digitalWrite(glob_plugged_crtl, LOW);
  
  //Serial.begin(9600); 

}

Thank you so much.

So it looks like a problem with the bootloader or your PCB.

The PCB is a protopcb (for soldering components on it), i don't think that was the problem.

I install the bootloader with Arduino IDE by Arduino as ISP following the tutorials.

Can it be something wrong with the code or something that forces the necessity to establish a communication by RX line?

I have tried on a Protoboard and the result is the same.

Here I can imagine the bootloader or a RX pullup/down.

If I fix a state on RX, I won't be able to communicate with the chip, right?

You will. That's the difference between a connection to GND or Vcc and a pullup/down resistor.

Can be, but we can't see it; don't forget to use code tags when posting your code. You might also want to post the schematic of the board.

I have tried to use pull up and pull down resistor: the result is the same. Only it works fine when I connect RX pin from ATMEGA328P to RX pin from Arduino Uno.

EDIT: I have solved adding delay on setup part.

Done, I add the setup code (the void loop code is not relevant I think).

I'm using a pin where pinMode is changing in different code parts.

EDIT: I have solved adding delay on setup part.