ATMEGA 2560 stk500v2_ReceiveMessage(): timeout

Hello everyone. When I try to upload this program on Arduino Mega:

Options in Arduino IDE shows in the screenshot

  1. RX and TX LEDs blink for a short time
  2. Uploading bar is at the end but it still uploading
  3. After 5 minutes it shows this error:
    Arduino: 1.8.10 (Windows 10), Vývojová deska: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

Arduino: 1.8.10 (Windows 10), Vývojová deska: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

Projekt zabírá 5916 bytů (2%) úložného místa pro program. Maximum je 253952 bytů.
Globální proměnné zabírají 449 bytů (5%) dynamické paměti, 7743 bytů zůstává pro lokální proměnné. Maximum je 8192 bytů.
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
Nastala chyba při nahrávaní projektu.

Táto zpráva by měla mít víc informacií v
"Zobrazení podrobného výstupu při kompilaci"
podle zapnuté volby v Soubor -> Nastavení.

// RFID čtečka - ukázka čtení adres tagů

// připojení knihoven SPI a MFRC522
#include <SPI.h>
#include <MFRC522.h>

// definování pinů pro SDA a RST
#define SDA_PIN 7
#define RST_PIN 4
// vytvoření instance RFID čtečky z knihovny
MFRC522 rfid(SDA_PIN, RST_PIN);

void setup() {
  // komunikace přes sériovou linku rychlostí 9600 baud
  Serial.begin(9600);
  // inicializace komunikace přes SPI
  SPI.begin();
  // inicializace komunikace s RFID čtečkou
  rfid.PCD_Init();
}

void loop() {
  // kontrola RFID tagů v okolí modulu,
  // pokud není žádný tag v okolí, volá se loop funkce od začátku,
  // celá část s RFID by tedy měla být na konci programu
  if ( ! rfid.PICC_IsNewCardPresent())
    return;
  // kontrola správného přečtení RFID tagu
  if ( ! rfid.PICC_ReadCardSerial())
    return;
  // výpis informace o verzi RFID tagu
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.print("RFID tag typu: ");
  Serial.println(rfid.PICC_GetTypeName(piccType));

  // kontrola podporovaných typů RFID tagu
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println("Tento RFID tag neni podporovany (typ MIFARE Classic).");
    return;
  }
  // výpis adresy RFID tagu v hexa formátu
  Serial.print("Adresa RFID tagu: ");
  vypisHex(rfid.uid.uidByte, rfid.uid.size);
  Serial.println();

  if(rfid.uid.uidByte[0] == 0x52 & rfid.uid.uidByte[1] == 0x3D & rfid.uid.uidByte[2] == 0xE5 & rfid.uid.uidByte[3] == 0xD5) {
    Serial.println("Detekovana bila karta!");
  }
  else if(rfid.uid.uidByte[0] == 0xC1 & rfid.uid.uidByte[1] == 0x07 & rfid.uid.uidByte[2] == 0x22 & rfid.uid.uidByte[3] == 0x77) {
    Serial.println("Detekovan modry privesek!");
  }
  /* ukázka přidání dalšího tagu
  else if(rfid.uid.uidByte[0] == 0x?? & rfid.uid.uidByte[1] == 0x?? & rfid.uid.uidByte[2] == 0x?? & rfid.uid.uidByte[3] == 0x??) {
    Serial.println("Detekovan novy tag ..!");
  } 
   */
  else {
    Serial.println("Detekovan neznamy RFID tag!");
  }
  Serial.println();
  // ukončení komunikace a jejího zabezpečení
  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
}
// podprogram pro výpis adresy RFID tagu v hexa formátu
void vypisHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

So I tried to burn bootloader. Doesnt help.
Then I find Arduino DFU but I had problems with it, so I didnt upgrade it.

Make sure you have selected the port of your Arduino board from the Tools > Port menu.

Sometimes the port will be labeled with the board name in the menu. Other times it will not. If you don’t know which port is your Arduino, you can find it like this:

  • Unplug your Arduino board from the computer.
  • Tools > Port
  • Note the ports, if any, listed in the menu.
  • Close the Tools menu
  • Plug your Arduino board into the computer.
  • Tools > Port - The new port listed in the menu is your Arduino board.

pert:
Make sure you have selected the port of your Arduino board from the Tools > Port menu.

Sometimes the port will be labeled with the board name in the menu. Other times it will not. If you don’t know which port is your Arduino, you can find it like this:

  • Unplug your Arduino board from the computer.
  • Tools > Port
  • Note the ports, if any, listed in the menu.
  • Close the Tools menu
  • Plug your Arduino board into the computer.
  • Tools > Port - The new port listed in the menu is your Arduino board.

I think I have selected it. Its the second screenshot in my first post.