Unable to upload code

Hello everyone, I have experienced a problem when trying to upload code to my Arduino Mega 2560 board. I am using a windows 10 laptop and Arduino IDE version 1.8.9.
Here is the code that I am trying to upload:

//Code to run the motors

//Create variables for the pins that will be used for the motors. The values assigned to those variables are the pin numbers on the Arduino.
int motorA_Speed = 10;
int motorA = 12;
int motorB_Speed = 11;
int motorB = 13;

int accel_Delay = 50; //in microseconds, the time between each increase in the speed of the motor for smooth acceleration

void setup() {
  // put your setup code here, to run once:
  //pinMode() sets the mode of the pin (either INPUT or OUTPUT, chose OUTPUT because it will send electricity towards the motors)
  pinMode(motorA, OUTPUT);
  pinMode(motorA_Speed, OUTPUT);
  pinMode(motorB, OUTPUT);
  pinMode(motorB_Speed, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  //run motor A forward at 50% speed
  Motor('A', 'f', 50);
  delay(3000);
}

void Motor(char mot, char dir, int speed){
  int newSpeed;
  if (speed == 0){
    newSpeed = 0;
  }
  else{
    newSpeed = map(speed, 1, 100, 0, 255);
  }
  
  switch(mot){
    case 'A':
    if (dir == 'f'){
      digitalWrite(motorA,HIGH);
    }
    else if (dir == 'r'){
      digitalWrite(motorA,LOW);
    }
    analogWrite(motorA_Speed, newSpeed);
    break;

    case 'B':
    if (dir == 'f'){
      digitalWrite(motorB,HIGH);
    }
    else if (dir == 'r'){
      digitalWrite(motorB,LOW);
    }
    analogWrite(motorA_Speed, newSpeed);
    break;

    case 'C':
    if (dir == 'f'){
      digitalWrite(motorA,HIGH);
      digitalWrite(motorB,HIGH);
    }
    else if (dir == 'r'){
      digitalWrite(motorA,LOW);
      digitalWrite(motorB,LOW);
    }
    analogWrite(motorA_Speed, newSpeed);
    break;
  }
  Serial.print ("Motors activated: ");
  if (mot=='C'){
    Serial.print("Both");
  }
  else{
    Serial.print(mot);
  }

  Serial.print("\n Direction: ");
  Serial.print(dir);
  Serial.print("\n Speed (from 0-100): ");
  Serial.print(speed);
  Serial.print("\n Mapped speed (from 0-255): ");
  Serial.print(newSpeed);
}

And then when I try to upload it to my Mega 2560 board, an error message comes up saying:
avrdude: Version 6.3-20190619
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

Using Port : COM4
Using Programmer : wiring
Overriding Baud Rate : 115200
avrdude: ser_open(): can't set com-state for "\.\COM4"
avrdude: ser_drain(): read error: The handle is invalid.

avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

avrdude done. Thank you.

the selected serial port
does not exist or your board is not connected

I have already bought a new cable because I thought that the cable could be affecting it. It didn't work.
I have bought an adapter so I could upload my code from my phone instead of my laptop, but it didn't work.
How can this be fixed?

I would go into windows device manager and see if com5 is there.

In the Type here to search type device manager you will see the serial ports about 2/3 down the column.

I'VE FINALLY SOLVED THIS PROBLEM

It turns out that my board is not official/original/true Arduino, as it is made by another company (my board is a clone of Arduino boards).
I had to:

  • install Ch340 drivers
    because that is the specific driver that is needed to transfer code to those Arduino clone boards.
    Here is the link to the website which allowed me to solve this problem:
    How to Install CH340 Drivers - SparkFun Learn

Hope this helps anyone in the future who is faced with this problem.

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