error opening serial port 'com4'. (port busy)

I was using a mega for a project and it was working fine. Then I randomly got the error.error opening serial port 'com4'. (port busy)The board works fine. The code I added to the program was tested on the same board and worked fine. The code is for a TSC3200 colour sensor and a light sensor (light variable resistor in a voltage divider). I checked in control panel and there is only one com device (com4).

Many people have has this issue when using Arduino and Possessing at the same time but I have never used Possessing.

Shutting down the computer did nothing.

Unplugging and pluging in the arduino did nothing.

Uploading the program to an uno (works on uno) and going back to the mega did nothing.

Getting the latest IDE did nothing.

Commenting out the new code did nothing. The block of commented out code is the new code.

Fix encoding and reload did nothing.

My code:

int outputPin = 39;

int aClock = 22;
int rClock = 23;

boolean aClockState = HIGH;
boolean rClockState = LOW;

int type = 0;
int header = 0;
int sign = 0;
int value = 0;
int repeat = 0;

int triggerPin = 52;
int echoPin = 53;

int duration = 0;
int distance = 0;

int numSensors = 5;

int S3 = 49;
int S2 = 48;
int out = 47;

int red = 0;
int green = 0;
int blue = 0;

void setup() {
  // THIS IS THE BOORING SETUP PART FOR THE PROGRAM       SETUP HERE        SETUP HERE      SETUP HERE
  Serial.begin(9600);

  changePinMode(OUTPUT);
  
  for(int i = 24; i < 40; i++) {
    digitalWrite(i, LOW);
  }

//  for(int i = 24; i < 40; i++) {
//    pinMode(i, OUTPUT);
//  }
  pinMode(44, OUTPUT);
  pinMode(42, OUTPUT);
  pinMode(43, OUTPUT);

  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(aClock, OUTPUT);
  pinMode(rClock, INPUT);

  digitalWrite(aClock, HIGH);
  transmit(0, 63);
  while(digitalRead(rClock) == rClockState);
  rClockState = !rClockState;
  digitalWrite(aClock, LOW);
  transmit(numSensors, 0);
}

// THIS IS WHERE THE FUN STUFF HAPPENS

void loop() {
  // THIS IS THE LOOPY THING / PART OF TEH PROGRAM            LOOP HERE         LOOP HERE       LOOP HERE
  digitalWrite(aClock, aClockState);

  while(digitalRead(rClock) == rClockState);
  rClockState = !rClockState;

  //ultrasonic
  // do the nessary stuff
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(5);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  
  // duration between pulses
  duration = pulseIn(echoPin, HIGH, 30000);

  // calculate distance based on duration
  distance = (duration / 2) / 29.1;

  Serial.print(distance);
  Serial.print("cm    ");
     
  transmit(distance, 1);

  type = 0;
  header = 0;
  sign = 0;
  value = 0;
  repeat = 0;

  aClockState = !aClockState;
  digitalWrite(aClock, aClockState);

//  transmit(analogRead(8), 2);

//  Serial.print(analogRead(8));
//  Serial.print("  ");

//  aClockState = !aClockState;
//  digitalWrite(aClock, aClockState);

//  digitalWrite(S2, LOW);
//  digitalWrite(S3, LOW);

//  value = pulseIn(out, LOW);

//  red = map(value, 173, 2000, 1023, 0);
  
  //green
//  digitalWrite(S2, HIGH);
//  digitalWrite(S3, HIGH);

//  value = pulseIn(out, LOW);

//  green = map(value, 173, 2000, 1023, 0);
  
  //blue
//  digitalWrite(S2, LOW);
//  digitalWrite(S3, HIGH);

//  value = pulseIn(out, LOW);

//  blue = map(value, 173, 2000, 1023, 0);
  
//  transmit(red, 3);

//  Serial.print("r:"); Serial.print(red); Serial.print("  ");
  
//  aClockState = !aClockState;
//  digitalWrite(aClock, aClockState);
  
//  transmit(green, 4);

//  Serial.print("g:"); Serial.print(green); Serial.print("  ");

//  aClockState = !aClockState;
//  digitalWrite(aClock, aClockState);
  
//  transmit(blue, 5);

//  Serial.print("b:"); Serial.print(blue); Serial.print("  ");

//  aClockState = !aClockState;
//  digitalWrite(aClock, aClockState);
}

// THIS IS THE TRANSMITTING CODE                           TRANSMIT HERE      TRANSMIT HERE     TRANSMIT HERE
void transmit(int val, int sensP) {
  for(int i = 9, x = 39; (i > -1) && (x > 23); i--, x--) {
    digitalWrite(x, bitRead(val, i));
  }
  for(int i = 5, x = 29; (i > -1) && (x > 23); i--, x--) {
    digitalWrite(x, bitRead(sensP, i));
  }
}

// THIS IS THE RECIVE CODE                                 RECIVE HERE        RECIVE HERE       RECIVE HERE
void recive() {
  // type
  if(digitalRead(39) == HIGH) type = type + 2;
  if(digitalRead(38) == HIGH) type = type + 1;

  //header
  if(digitalRead(37) == HIGH) header = header + 4;
  if(digitalRead(36) == HIGH) header = header + 2;
  if(digitalRead(35) == HIGH) header = header + 1;

  //sign
  if(digitalRead(34) == HIGH) sign = sign + 1;

  //value
  if(digitalRead(33) == HIGH) value = value + 256;
  if(digitalRead(32) == HIGH) value = value + 128;
  if(digitalRead(31) == HIGH) value = value + 64;
  if(digitalRead(30) == HIGH) value = value + 32;
  if(digitalRead(29) == HIGH) value = value + 16;
  if(digitalRead(28) == HIGH) value = value + 8;
  if(digitalRead(27) == HIGH) value = value + 4;
  if(digitalRead(26) == HIGH) value = value + 2;
  if(digitalRead(25) == HIGH) value = value + 1;

  //repeat
  if(digitalRead(24) == HIGH) repeat = repeat + 1;
}

void doRecive() {
  recive();

  if(type == 0) {
    if(sign == 1) {
      digitalWrite(42, HIGH);
      digitalWrite(43, LOW);
    }
    else {
      digitalWrite(42, LOW);
      digitalWrite(43, HIGH);
    }
    analogWrite(44, value);
  }
}

int changePinMode(boolean mode) {
  for(int i = 24; i < 40; i++) {
    pinMode(i, mode);
  }
}

This is an older version of my code that works.

//int value = 722;
//int port = 43;
int outputPin = 39;
int aClock = 22;
int rClock = 23;

int type = 0;
int header = 0;
int sign = 0;
int value = 0;
int repeat = 0;

int triggerPin = 52;
int echoPin = 53;

int duration = 0;
int distance = 0;

void setup() {
  // THIS IS THE BOORING SETUP PART FOR THE PROGRAM       SETUP HERE        SETUP HERE      SETUP HERE
  Serial.begin(9600);

//  changePinMode(OUTPUT);
  
  for(int i = 24; i < 40; i++) {
    pinMode(i, OUTPUT);
  }
  
  for(int i = 24; i < 40; i++) {
    digitalWrite(i, LOW);
  }

  for(int i = 24; i < 40; i++) {
    pinMode(i, OUTPUT);
  }
  pinMode(44, OUTPUT);
  pinMode(42, OUTPUT);
  pinMode(43, OUTPUT);

  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

// THIS IS WHERE THE FUN STUFF HAPPENS

void loop() {
  // THIS IS THE LOOPY THING / PART OF TEH PROGRAM            LOOP HERE         LOOP HERE       LOOP HERE
  
//  digitalWrite(aClock, LOW);

  // do the nessary stuff
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(5);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  
  // duration between pulses
  duration = pulseIn(echoPin, HIGH);
  
  // calculate distance based on duration
  distance = (duration / 2) / 29.1;

  Serial.print(distance);
  Serial.println("cm");
      
  transmit(distance, 1);
//  doRecive();
//  Serial.println();

  type = 0;
  header = 0;
  sign = 0;
  value = 0;
  repeat = 0;
  
  delay(100);
}

// THIS IS THE TRANSMITTING CODE                           TRANSMIT HERE      TRANSMIT HERE     TRANSMIT HERE
void transmit(int val, int sensP) {
  for(int i = 9, x = 39; (i > -1) && (x > 23); i--, x--) {
    digitalWrite(x, bitRead(val, i));
  }
  for(int i = 5, x = 29; (i > -1) && (x > 23); i--, x--) {
    digitalWrite(x, bitRead(sensP, i));
  }
}

// THIS IS THE RECIVE CODE                                 RECIVE HERE        RECIVE HERE       RECIVE HERE
void recive() {
  // type
  if(digitalRead(39) == HIGH) type = type + 2;
  if(digitalRead(38) == HIGH) type = type + 1;

  //header
  if(digitalRead(37) == HIGH) header = header + 4;
  if(digitalRead(36) == HIGH) header = header + 2;
  if(digitalRead(35) == HIGH) header = header + 1;

  //sign
  if(digitalRead(34) == HIGH) sign = sign + 1;

  //value
  if(digitalRead(33) == HIGH) value = value + 256;
  if(digitalRead(32) == HIGH) value = value + 128;
  if(digitalRead(31) == HIGH) value = value + 64;
  if(digitalRead(30) == HIGH) value = value + 32;
  if(digitalRead(29) == HIGH) value = value + 16;
  if(digitalRead(28) == HIGH) value = value + 8;
  if(digitalRead(27) == HIGH) value = value + 4;
  if(digitalRead(26) == HIGH) value = value + 2;
  if(digitalRead(25) == HIGH) value = value + 1;

  //repeat
  if(digitalRead(24) == HIGH) repeat = repeat + 1;
}

void doRecive() {
  recive();

  if(type == 0) {
    if(sign == 1) {
      digitalWrite(42, HIGH);
      digitalWrite(43, LOW);
    }
    else {
      digitalWrite(42, LOW);
      digitalWrite(43, HIGH);
    }
    analogWrite(44, value);
  }
}

// ULTRASONIC SENSOR CODE HERE                                  ULTRASONIC HERE     ULTRASONIC HERE     ULTRASONIC HERE
int ultrasonic() {
  // do the nessary stuff
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(5);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  
  // duration between pulses
  duration = pulseIn(echoPin, HIGH);
  
  // calculate distance based on duration
  distance = (duration / 2) / 29.1;

  return distance;
}

//int changePinMode(boolean mode) {
//  for(int i = 24; i < 40; i++) {
//    pinMode(i, mode);
//  }
//}