Why do I get avrdude: stk500v2_ReceiveMessage(): timeout error when uploading to Arduino Mega? I have an Arduino Mega clone and I haven't used it for a while. I remember it not working so when I plugged it in and tried uploading the code to it and sure enough it didn't work. So I plugged in an Uno and it worked. The Mega is a 2560 Atmel. Here's my code:


#define distanceForward A0
#define distanceLeft A1
#define distanceRight A2
#define distanceRightAngle A3
#define distanceLeftAngle A4
#define distanceBack A5
int talk = 2;

void setup() {
  Serial.begin(9600);
  pinMode(talk, OUTPUT);
  pinMode(distanceForward, INPUT);
  // /\ Ir distance sensor pin for input signal to arduino, Front Facing
  pinMode(distanceLeftAngle, INPUT);
  // /\ Ir distance sensor pin for input signal to arduino, Left Angle
  pinMode(distanceRightAngle, INPUT);
  // /\ Ir distance sensor pin for input signal to arduino, Right Angle
  pinMode(distanceLeft, INPUT);
  // /\ Ir distance sensor pin for input signal to arduino, Left
  pinMode(distanceRight, INPUT);
  // /\ Ir distance sensor pin for input signal to arduino, Right
  pinMode(distanceBack, INPUT);
  // /\ Ir distance sensor pin for input signal to arduino, back
}

void hall_roam() {
  Serial.write("forward");
  if (analogRead(distanceLeftAngle) &lt; 30) {
    Serial.write("right");
    if (analogRead(distanceLeftAngle) &gt; 30) {
      Serial.write("forward");
    }
    if (analogRead(distanceRightAngle) &lt; 30) {
      Serial.write("left");
      if (analogRead(distanceRightAngle) &lt; 30) {
        Serial.write("forward");
      }
      if (analogRead(distanceForward) &lt; 60) {
        Serial.write("stop");
        if ((analogRead(distanceLeft) &lt; 60) &amp;&amp; (analogRead(distanceRight) &lt; 60)) {
          Serial.write("left");
          if (analogRead(distanceForward) &gt; 60) {
            Serial.write("stop");
          }
        }
      }
      if ((analogRead(distanceLeft) &gt; 60) &amp;&amp; (analogRead(distanceRight) &gt; 60)) {
        Serial.write("left");
        if (analogRead(distanceForward) &gt; 60) {
          Serial.write("stop");
        }
      }
      if ((analogRead(distanceLeft) &gt; 60) &amp;&amp; (analogRead(distanceRight) &lt; 60)) {
        Serial.write("left");
        if (analogRead(distanceForward) &gt; 60) {
          Serial.write("stop");
        }
      }
      if ((analogRead(distanceLeft) &gt; 60) &amp;&amp; (analogRead(distanceRight) &lt; 60)) {
        Serial.write("left");
        if (analogRead(distanceForward) &gt; 60) {
          Serial.write("stop");
        }
      }
      if ((analogRead(distanceLeft) &lt; 60) &amp;&amp; (analogRead(distanceRight) &gt; 60)) {
        Serial.write("right");
        if (analogRead(distanceForward) &gt; 60) {
          Serial.write("stop");
        }
      } else {
        Serial.write("forward");
      }
    }
  }
}

void roam() {
  Serial.write("forward");
  if (analogRead(distanceForward) &lt; 60) {
    Serial.write("stop");
    Serial.write("left");
    if (analogRead(distanceForward) &gt; 60) {
      Serial.write("stop");
      Serial.write("forward");
    }
  }
}

void talkbb8() {
  pinMode(talk, HIGH);
  pinMode(talk, LOW);
}

void chill() {
  Serial.write("stop");
  talkbb8();
}

void loop() {
  if ((analogRead(distanceLeft) &lt; 60) &amp;&amp; (analogRead(distanceRight) &lt; 60)) {
    hall_roam();
  } else if ((analogRead(distanceForward) &lt; 60) &amp;&amp; (analogRead(distanceBack) &lt; 60)) {
    Serial.write("left");
    if ((analogRead(distanceLeft) &lt; 60) &amp;&amp; (analogRead(distanceRight) &lt; 60)) {
      Serial.write("stop");
      hall_roam();
    }
  } else {
    roam();
  }
}


If there's a bracket out of place or something it's probably nothing. I'm talking about the characters. Is there anything in the characters that would screw up the upload like the three "!!!" problem I heard about or something like that. It's possible that the USB chip in it isn't recognized by my iMac. If so how do I fix that?
The error message basically means that the programmer is unable to contact the boot-loader on the device. The code you're trying to upload has no bearing on the problem.

There could be one of the four possible reason behind it. They are:


Wrong Port Selected:

If TX/RX led is not blinking, then you may have selected wrong port. If one of both of them are blinking then, you aren't using the wrong port. It might be worth checking again though, sometimes USB COM devices install on strange port numbers.
Board is not in Bootloader Mode:

The Arduino Mega is not executing the bootloader for some reason. If the programmer is not resetting the micro before attempting to connect, this might be the reason. Try to reset the board (press and release the button) while the programmer is attempting to connect. Sometimes software that runs in a tight loop will prevent the bootloader from connecting.
Barring that, the fuses might have gotten messed up or the code erased. You would need to reflash the bootloader and proper fuses, again, see the appropriate info page for your device.
Arduino Mega 2560 only: ATmega8U/16U software

Might not be working and would need reprogramming. See the Programming section on the info page, you will need the firmware and Atmel-compatible DFU (device firmware update) software on your computer toreflash the target.
Hardware damage:

There could be hardware damage to the board, AVR(s), or FTDI chip. Time to buy another board.


You can check this answer.