Error while trying to appload the sketch

Hi!
I've been using Arduino Mega 2560(genuine) which was working perfectly on everything. It suddenly throws an error "an Error occurred while apploading a sketch" with a couple of
avrdude:stk500v2_ReceiveMessage():timeout, and a stkv2_getsync():timeout communicating with programmer.

Everything is just set perfectly:board, COM port and processor.

The problem is the code can't be apploaded but the last code to appload is running.
I tried to change USB but it still throws error.
Help please.

Have you tried with no additional hardware connected to the board?

@aarg. Yes, I've tried just everything I know( checking correct port and board, changing USB, reinstalling IDE..). The only thing left is using another computer.

Are you sure you have the right board selected in the IDE? Have you tried uploading to a different Mega?

Because you have almost exhausted the options, you should provide more detail about your project so that we might recognize the cause.

...and just to be sure, you are uploading to a BARE Mega with nothing connected to any pins? What is the power supply for the Mega? Via the USB?

How do you know that the last code is running?

@aarg. I'm sure 100%. I have not tried to use other mega though. The power to the mega is just USB itself. The TX and RX lights are constantly OFF but the PWM and the ON are both lighting. This just happened suddenly and I have been using this board for months-since March to be precise, "error while uploading" was just a common error which I always was able to fix by just checking port configuration and USB connection, this time none of them works,

i want to control three relays using L293D driver. It has been working so far. These relays are meant to control a 4/3 double solenoid valve and oil pump for double acting cylinder. One solenoid and the pump should be ON for specified time while the other solenoid is staying OFF, the pump being common to each solenoid. The solenoid are NC which means opens the valve when powered and returns when power is removed.
the circuit is constructed using three relays and two diodes.

so the last code to upload is still working but it looks like I'm not getting serial communication anymore... Those avrdude:stk500v2_Receive_Message():time out are making me nervous, I don't know what is changed. The board is legitimate-not clone.

How do you know the sketch is running? You didn't mention any switches, so are the solenoid actions just timed?

@aarg. The sketch doesnt use serial communication but

Confusing. You said your sketch doesn't use serial so why would you have communication?

@aarg. I can hear relays clicking and I can measure the voltage across their contacts and that tells me the relay closed successfully.

So they run in a timed sequence?

@aarg. To be precise, that chunk of code doesn't depend on serial but is part of the program which is gonna use serial entirely. what makes me to worry about not getting serial communication is the TX or RX or Both are not blinking after some time. Some guy said there should be some blinks for TX and RX lights on the board and that signify the PC is serially active with the board. I didn't mean the board isn't working serially in itself; of course I didn't enable serial communication.

Let's go back and cover some unanswered questions - when you try to upload a new sketch, are the relays connected?

@aarg. Yes. A Sequence that extend and retract the cylinder and wait until it's told to do so again. The time to stay ON one extension is extension time for the needed length and the time to stay ON for retraction is the retraction time to initial position. I was planning to use distance measuring sensor but I figured out this would be simpler and cheaper, though doesn't account for any unexpected event.

... and just how is it "told to do it again"? Is it a test sketch that doesn't actually do that? You need to provide all the context and not leave it for us to guess. Also please answer #12

@aarg. I uploaded the code, it went into the board perfectly and relays were clicking, that was the last upload. The next time to Upload the relays were still connected and it was when the error started. I disconnected everything and tried to upload again but the error keeps showing, tried another computer, another USB, checked configuration...nothing worked.

@aarg. by 'told to do so" means there is a volatile bool flag which is the condition to execute the chunk, actually a function that extends and retracts the cylinder within set times:when the function is called with flag true, at the end of execution the condition is false so it wait until the function is called again with flag changed to true.

note: this is a modified Blinky sketch with irrelevant comments(comment doesn't mean what the chunk of code does).

[code]
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(EN1_2, OUTPUT);
  pinMode(EN3_4, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {

    if(run_){
     digitalWrite(EN1_2 ,HIGH);
      digitalWrite(EN3_4 ,HIGH); 
    digitalWrite(IN1 ,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(IN2 ,LOW);
      
     
     delay(10000);
     digitalWrite(IN1 ,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(IN2 ,LOW);
  digitalWrite(EN1_2 ,LOW);
  delay(1000);
 digitalWrite(IN3 ,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(IN4 ,LOW);
  delay(10000);
  digitalWrite(EN3_4 ,LOW);
  digitalWrite(IN1 ,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(IN2 ,LOW);
 digitalWrite(IN3 ,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(IN4 ,LOW);
   
 
  }
run_=false; 
 
}
[/code]

Can you please confirm the answer to reply #2 and reply #12? Very explicitly? By "additional hardware" we now mean the relays. Also where do the relays get power from? We want to try to upload to the bare board with nothing connected to it except the USB cable.

A schematic will be useful, links (URLs) to any components (boards, solenoids) will also be useful. Did you make any changes to the wiring and accidentally do something wrong?

As this is a genuine Mega, did you try the loopback test?

@sterretje. I don't know about it-the loop back test.

@aarg. Hey, how many times do I have to tell you something to understand? I told you I tried without anything connected,USB only.

The power comes from the same DC power supply powering the driver.