Skip to main content
1 of 3
techset
  • 59
  • 1
  • 1
  • 6

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 and 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) < 30){
      Serial.write("right");
      if (analogRead(distanceLeftAngle) > 30){
         Serial.write("forward");
   }

   if (analogRead(distanceRightAngle) < 30){
     Serial.write("left");
     if (analogRead(distanceRightAngle) < 30){
            Serial.write("forward");
   }
 
   if (analogRead(distanceForward) < 60){
       Serial.write("stop");
       if ((analogRead(distanceLeft) < 60) && (analogRead(distanceRight) < 60)){        
           Serial.write("left");
           if (analogRead(distanceForward) > 60){
                Serial.write("stop");
           }
       }
   }
   if ((analogRead(distanceLeft) > 60) && (analogRead(distanceRight) > 60)){       
        Serial.write("left");
        if (analogRead(distanceForward) > 60){
             Serial.write("stop");
        }
     }
   if ((analogRead(distanceLeft) > 60) && (analogRead(distanceRight) < 60)){
      Serial.write("left");
          if (analogRead(distanceForward) > 60){
               Serial.write("stop");
     }
   }
   if ((analogRead(distanceLeft) > 60) && (analogRead(distanceRight) < 60)){
       Serial.write("left");
       if (analogRead(distanceForward) > 60){
          Serial.write("stop");
   }
  
   }
   if ((analogRead(distanceLeft) < 60) && (analogRead(distanceRight) > 60)){
      Serial.write("right");
      if (analogRead(distanceForward) > 60){
          Serial.write("stop");
   }
  
 }

   else{
       Serial.write("forward");
    }

   }
   void roam(){
      Serial.write("forward");
      if (analogRead(distanceForward) < 60){
         Serial.write("stop");
         Serial.write("left");
         if (analogRead(distanceForward) > 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) < 60) && (analogRead(distanceRight) < 60)){
            hall_roam();
        }
        else if ((analogRead(distanceForward) < 60) && (analogRead(distanceBack) < 60)){
            Serial.write("left");
            if ((analogRead(distanceLeft) < 60) && (analogRead(distanceRight) < 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 recognizer by my iMac, if So how do i fix that?

techset
  • 59
  • 1
  • 1
  • 6