Troubles with communicating through ports

So, I have been trying to get this code for an Ardruino controlled Simon says game to work for some
time now and I am really close but I don’t know how to get the serial ports from the processing and the Arduino codes to communicate properly. I am using the arduino code for all of the actual Simon says content and the processing code to read the a,w,d,s keys which then get sent back to the serial port for interpretation by the arduino code. The problem is when I try to run the arduino it wont let me share the serial port with the processing code.

To clarify, these are the steps I take: 1. upload the Arduino code → 2. open Serial monitor in Arduino
→ 3. run processing code → 4. processing code gives me the error " COM3 port busy" (using port 3 btw).

I have tried different combinations of these steps like running processing first and then arduino but it gives me the same error on the arduino code. Is there a way to get the processing and arduino code to communicate on the same serial port or am I going about this wrong? I am very new to this and I would appreciate any help you could give me or even a different way to do it.
Here is my processing code:

import processing.serial.*;
Serial port;

void setup() {
  println(Serial.list()); 
  port = new Serial(this, Serial.list()[0], 9600);

}
void draw(){
if (key == 'a'){
  port.write('a');
}
if (key == 'w'){
  port.write('w');
}
if (key == 'd'){
  port.write('d');
}
if (key == 's'){
  port.write('s');
}
}

Here is my arduino code:

int duration2[] = {100, 200, 100, 200, 100, 400, 100, 100, 100, 100, 200, 100, 500};
int duration[] = {100, 100, 100, 300, 100, 300};
boolean ledpin[] = {9, 10, 11, 12};  // LED pins
int turn = 0;  // turn counter
int randomArray[100]; //Intentionally long to store up to 100 inputs (doubtful anyone will get this far)
int inputArray[100];  
int incomingByte;
int keystate = 0;

void setup() 
{
  Serial.begin(9600);

  for(int x=0; x<4; x++)  // LED pins are outputs
  {
    pinMode(ledpin[x], OUTPUT);
  }

  randomSeed(analogRead(0)); //Added to generate "more randomness" with the randomArray for the output function
  
}
void loop() 
{  
 if (Serial.available() > 0) {
  incomingByte = Serial.read();
 } 
  for (int y=0; y<=99; y++)
  {
    //function for generating the array to be matched by the player
    digitalWrite(ledpin[0], HIGH);
    digitalWrite(ledpin[1], HIGH);
    digitalWrite(ledpin[2], HIGH);
    digitalWrite(ledpin[3], HIGH);
  
     delay(25);
    }
    
    digitalWrite(ledpin[0], LOW);
    digitalWrite(ledpin[1], LOW);
    digitalWrite(ledpin[2], LOW);
    digitalWrite(ledpin[3], LOW);
    delay(1000);
  
    for (int y=turn; y <= turn; y++)
    { //Limited by the turn variable
      Serial.println(""); //Some serial output to follow along
      Serial.print("Turn: ");
      Serial.print(y);
      Serial.println("");
      randomArray[y] = random(1, 5); //Assigning a random number (1-4) to the randomArray[y], y being the turn count
      for (int x=0; x <= turn; x++)
      {
        Serial.print(randomArray[x]);
      
        for(int y=0; y<4; y++)
        {
      
          if (randomArray[x] == 1 && ledpin[y] == 9) 
          {  //if statements to display the stored values in the array
            digitalWrite(ledpin[y], HIGH);
            delay(400);
            digitalWrite(ledpin[y], LOW);
            delay(100);
          }

          if (randomArray[x] == 2 && ledpin[y] == 10) 
          {
            digitalWrite(ledpin[y], HIGH);
            delay(400);
            digitalWrite(ledpin[y], LOW);
            delay(100);
          }
  
          if (randomArray[x] == 3 && ledpin[y] == 11) 
          {
            digitalWrite(ledpin[y], HIGH);
            delay(400);
            digitalWrite(ledpin[y], LOW);
            delay(100);
          }

          if (randomArray[x] == 4 && ledpin[y] == 12) 
          {
            digitalWrite(ledpin[y], HIGH);
            delay(400);
            digitalWrite(ledpin[y], LOW);
            delay(100);
          }
        }
      }
    }
    input();
  }
 
void input() { //Function for allowing user input and checking input against the generated array

  for (int x=0; x <= turn;)
  { //Statement controlled by turn count

    for(int y=0; y<4; y++)
    {
      
      if (Serial.available() > 0) {
       // keystate = 1 - keystate;
      }
    
      if (keystate == LOW && incomingByte == 'a')
      { //Checking for button push
        digitalWrite(ledpin[0], HIGH);
        delay(200);
        digitalWrite(ledpin[0], LOW);
        inputArray[x] = 1;
        delay(250);
        Serial.print(" ");
        Serial.print(1);
        if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against
          fail();                              //the value in the same spot on the generated array
        }                                      //The fail function is called if it does not match
        x++;
      }
       if (keystate == LOW && incomingByte == 'w')
      {
        digitalWrite(ledpin[1], HIGH);
        delay(200);
        digitalWrite(ledpin[1], LOW);
        inputArray[x] = 2;
        delay(250);
        Serial.print(" ");
        Serial.print(2);
        if (inputArray[x] != randomArray[x]) {
          fail();
        }
        x++;
      }

      if (keystate == LOW && incomingByte == 'd')
      {
        digitalWrite(ledpin[2], HIGH);
        delay(200);
        digitalWrite(ledpin[2], LOW);
        inputArray[x] = 3;
        delay(250);
        Serial.print(" ");
        Serial.print(3);
        if (inputArray[x] != randomArray[x]) {
          fail();
        }
        x++;
      }

      if (keystate == LOW && incomingByte == 's')
      {
        digitalWrite(ledpin[3], HIGH);
        delay(200);
        digitalWrite(ledpin[3], LOW);
        inputArray[x] = 4;
        delay(250);
        Serial.print(" ");
        Serial.print(4);
        if (inputArray[x] != randomArray[x]) 
        {
          fail();
        }
        x++;
      }
    }
  }
  delay(500);
  turn++; //Increments the turn count, also the last action before starting the output function over again
}

void fail() { //Function used if the player fails to match the sequence
 
  for (int y=0; y<=2; y++)
  { //Flashes lights for failure
    
    digitalWrite(ledpin[0], HIGH);
    digitalWrite(ledpin[1], HIGH);
    digitalWrite(ledpin[2], HIGH);
    digitalWrite(ledpin[3], HIGH);
    delay(200);
    digitalWrite(ledpin[0], LOW);
    digitalWrite(ledpin[1], LOW);
    digitalWrite(ledpin[2], LOW);
    digitalWrite(ledpin[3], LOW);
    delay(200);
  }
  delay(500);
  turn = -1; //Resets turn value so the game starts over without need for a reset button
}

like I said any help would be greatly appreciated, thank you.

You can’t have two PC applications (Processing and the Arduino Serial Monitor) using the same serial port. You must choose one or the other.

I don’t know Processing but I presume you could add code to your Processing program so it would display whatever comes from the Arduino in a similar way to the Serial Monitor. I do this with Python.

…R

I don't know Processing but I presume you could add code to your Processing program so it would display whatever comes from the Arduino

I do. You can.