Arduino ide sketch not uploading to Mega 2560

**Since 2 weeks i keep getting the issue that the arduino ide does not want to upload the sketch to the arduino mega 2560. **

The sketch was working absolutely fine but after an update it is not doing the job anymore.

**I have reinstalled the ide, reinstalled the libraries used the older version of the libraries. **
**Tried resseting the mega witth rxo txo and that works sometimes but still doesnt work frequently. ** Overall it is not working as it used to.

But when i open my laptop every morning and connect the arduino, the sketch is working fine without having to upload. But if i try again to upload the sketch than again it doesnt want to upload and it gives the message: Not connected select a board and a port to connect automatically while under the serial monitor it clearly sais that the arduino is connected, than after 2 minutes when it is trying to upload i get an error:

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
Failed uploading: uploading error: exit status 1

**I dont know what the issue is and i have tried a lot of methods to resolve the issue, so if someone can help me out i would really appreciate it. **

Please check the code if you think there is something wrong or bugged in the code:

#include <Wire.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include "Adafruit_MCP9600.h"
#include <Servo.h> 

#define PCA9548A_ADDRESS 0x70
#define MCP9600_ADDRESS 0x60

#define FAN_PIN 50
#define SOLENOID_PIN 51
#define SERVO_PIN1 9
#define SERVO_PIN2 10
#define TEMPERATURE_THRESHOLD 30.0
#define DURATION 10000
#define MAX_ULONG 4294967295

Adafruit_MCP9600 mcp1;
Adafruit_MCP9600 mcp2;
Adafruit_MCP9600 mcp3;
Adafruit_MCP9600 mcp4;
Adafruit_MCP9600 mcp5;
Adafruit_MCP9600 mcp6;
Adafruit_MCP9600 mcp7;

Servo servo1;
Servo servo2;
unsigned long activationTime = MAX_ULONG;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  
  setupMCP(&mcp1, 0);
  setupMCP(&mcp2, 1);
  setupMCP(&mcp3, 2);
  setupMCP(&mcp4, 3);
  setupMCP(&mcp5, 4);
  setupMCP(&mcp6, 5);
  setupMCP(&mcp7, 6);
  
  pinMode(FAN_PIN, OUTPUT);
  pinMode(SOLENOID_PIN, OUTPUT);
  servo1.attach(SERVO_PIN1);
  servo2.attach(SERVO_PIN2);
}

void loop() {
  String dataString = "";

  dataString += processSensor(&mcp1, millis());
  dataString += ", ";
  dataString += processSensor(&mcp2, 0);
  dataString += ", ";
  dataString += processSensor(&mcp3, 0);
  dataString += ", ";
  dataString += processSensor(&mcp4, 0);
  dataString += ", ";
  dataString += processSensor(&mcp5, 0);
  dataString += ", ";
  dataString += processSensor(&mcp6, 0);
  dataString += ", ";
  dataString += processSensor(&mcp7, 0);
  
  Serial.println(dataString);
  
  delay(500);
}

void setupMCP(Adafruit_MCP9600* mcp, uint8_t channel) {
  selectChannel(channel);
  if (!mcp->begin(MCP9600_ADDRESS)) {
    Serial.print("Failed to find MCP9600 chip ");
    Serial.println(channel);
    while (1) {
      delay(10);
    }
  }
  mcp->setThermocoupleType(MCP9600_TYPE_K);
}

float processSensor(Adafruit_MCP9600* mcp, unsigned long currentTime) {
  selectChannel(getChannel(mcp));
  float temp = mcp->readThermocouple();
  
  if(mcp == &mcp1) {
    if(temp >= TEMPERATURE_THRESHOLD) {
      digitalWrite(FAN_PIN, LOW);
      digitalWrite(SOLENOID_PIN, LOW);
      servo1.write(90);
      servo2.write(90);
      activationTime = currentTime;
    } else if(activationTime != MAX_ULONG && currentTime - activationTime < DURATION) {
      digitalWrite(FAN_PIN, LOW);
      digitalWrite(SOLENOID_PIN, LOW);
      servo1.write(90);
      servo2.write(90);
    } else {
      digitalWrite(FAN_PIN, HIGH);
      digitalWrite(SOLENOID_PIN, HIGH);
      servo1.write(0);
      servo2.write(0);
      activationTime = MAX_ULONG;
    }
  }
  
  return temp;
}

void selectChannel(uint8_t channel) {
  Wire.beginTransmission(PCA9548A_ADDRESS);
  Wire.write(1 << channel);
  Wire.endTransmission();
}

uint8_t getChannel(Adafruit_MCP9600 * mcp) {
  if(mcp == &mcp1) return 0;
  else if(mcp == &mcp2) return 1;
  else if(mcp == &mcp3) return 2;
  else if(mcp == &mcp4) return 3;
  else if(mcp == &mcp5) return 4;
  else if(mcp == &mcp6) return 5;
  else if(mcp == &mcp7) return 6;
  else return 0;  // default to channel 0 if unknown sensor
}

See errors:


Does your sketch provide serial output (that you e.g. can see in Serial Monitor).

What kind of update? Update of your sketch, update of the IDE, update of the libraries or update of Windows?

What does Windows device manager think of your board?
Which version of Windows?
Does COM5 disappear when you disconnect the board?
What is the TTL-to-USB converter chip on the board (16U2, CH340, FTDI...) ?
If it has a FTDI chip, there are known issues with the combination IDE 2.x / upload / FTDI (although I thought that it resulted in access denied); close the serial monitor might help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.