1d Pong Game malfunction

I am working on a 1d pong game and need a bit of help. Pardon the state of my code, I am new to this field.

I do not know why, but my code will not exit the volley loop. At the start of the game both end leds are illuminated and then a volley is activated based on which button is hit. The game goes back and forth until 4 points are scored. Each score will activate the Serve loop which illuminates that sides led until a button press. Here is where the problem pops up. After the score condition is meet, the loop is exited but the code hangs and illuminates a single led. No button presses will react and nothing should be illuminating the led. I mocked it up in tinkercad and ran a line by line debug and no lines are firing during this bit. After Volley runs through, shouldn’t it loop back through the main loop? Or, am I mistaking about that entirely? PS I know my score display is not operating but thats a different battle and (I dont think) its not causing the issue.

//1D Pong 
//based on code by Andrew Schmidt (aschmidt20) modified by Shayne Pry 1/10/2018. Added handlers for charlieplexed leds and a few game stability changes.
//June 21, 2013

int button1Pin = 8;
int button2Pin = 6;
int button1State, button2State, prior1State, prior2State;
String gameInstance = "wait";
int player1Score, player2Score = 0;
int delayTime;
int A = 12;
int B = 11;
int C = 10;
int D = 9;
int dir = 0;
int vActive = 0;
int charliePath[12][2] = {
  {A, B},
  {B, A},
  {D, B},
  {B, D},
  {D, A},
  {A, D},
  {D, C},
  {C, D},
  {A, C},
  {C, A},
  {B, C},
  {C, B}
};
int charlieScore1[4][2] = {
  {B, A},
  {D, B},
  {B, D},
  {D, A}
};
int charlieScore2[4][2] = {
  {C, D},
  {A, C},
  {C, A},
  {B, C}
};
void setup()
{
  pinMode(A, INPUT);
  pinMode(B, INPUT);
  pinMode(C, INPUT);
  pinMode(D, INPUT);
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  delayTime = 100;
  Serial.begin(9600);
}

void loop()
{                                 
  while (gameInstance == "wait"){
  	button1State = digitalRead(button1Pin);
  	button2State = digitalRead(button2Pin);
  	if (button1State == HIGH)
  	{
    	gameInstance = "volley 1";
    	Serial.println("volley 1" );
    	dir = 2;
        vActive = 1;
        Serial.println("vActive1" );
      	volley();
  	}
  	else if (button2State == HIGH)
  	{
    	gameInstance = "volley 2";
    	Serial.println("volley 2" );
    	dir = 1;
      	vActive = 1;
      	Serial.println("vActive2" );
      	volley();
  	}
  	else
  	{ 
    	Serial.println("wait");
    	charlieFlash(charliePath[0][0],charliePath[0][1], 10);
    	charlieFlash(charliePath[11][0],charliePath[11][1], 10);  
  	}
  }	   
}

void volley()
{
  while (vActive == 1)
  {
    if (dir == 1)
  	{
  		for(int index = 0; index <= 9; index++)
  		{
    		charlieFlash(charliePath[index][0],charliePath[index][1], delayTime);
  		}

  		prior1State = digitalRead(button1Pin);
  
  		charlieOn(charliePath[10][0], charliePath[10][1], delayTime);
  		charlieOn(charliePath[11][0], charliePath[11][1], delayTime);
  
  		button1State = digitalRead(button1Pin);
  		charlieOff(charliePath[11][0],charliePath[11][0]);
  
  		if (button1State == HIGH && prior1State == LOW)
  		{
   		    delayTime = constrain(delayTime - 5, 20, 100);
    		dir = 2; 
          	Serial.println("player 1 return");
  		}
  		else
  		{
    		Serial.println("player 2 point");
          	player2Score++;
    		delayTime = 100;
    		showScore();
            if (vActive == 1)
            {
          		serve2();  
            }
        }
  	}
  	else if (dir == 2)
  	{
    	for(int index = 11; index >=2; index--)
  		{
    		charlieFlash(charliePath[index][0], charliePath[index][1], delayTime);
  		}

  		prior2State = digitalRead(button2Pin);
  
  		charlieOn(charliePath[1][0], charliePath[1][1],delayTime);
  		charlieOn(charliePath[0][0], charliePath[0][1],delayTime);
  
  		button2State = digitalRead(button2Pin);
      
      	charlieOff(charliePath[0][0], charliePath[0][1]);
  
  		if (button2State == HIGH && prior2State == LOW)
  		{
   		    delayTime = constrain(delayTime - 5, 20, 100);
    		dir = 1; 
          	Serial.println("player 2 return");
  		}
  		else
  		{
    		Serial.println("player 1 point");
          	player1Score++;
    		delayTime = 100;
    		showScore();
          	if (vActive = 1)
            {
          	  serve1();
            }  			
        }
    }
  }
};
void serve1()
{
  while (gameInstance = "serve1")
  {
    button1State = digitalRead(button1Pin);
    if (button1State == HIGH)
    {
      dir = 2;
      gameInstance = "volley 1";
      volley();
    }
    else
    { 
      charlieFlash(charliePath[11][0],charliePath[11][1], 10);  
    }
  }
}

void serve2()
{
  while (gameInstance = "serve2")
  {
    button2State = digitalRead(button2Pin);
    if (button2State == HIGH)
    {
      dir = 1;
      gameInstance = "volley 2";
      volley();
    }
    else
    { 
      charlieFlash(charliePath[0][0],charliePath[0][1], 10);  
    }
  }
}

void showScore()
{
  // Show player 1 score
  for (int j =0; j < 30; j++){
    for (int i = 0; i < 4; i++)
    {
      if (i <= player2Score)
      {
        charlieFlash(charlieScore1[i][0],charlieScore1[i][0], 10);
      }
      if (i <= player1Score)
      {
        charlieFlash(charlieScore2[i][0],charlieScore2[i][0], 10);
      }
  }
  }
  delay(1000);
  if (player1Score == 4)
  {
    marquee(1);
  }
  else if (player2Score == 4)
  {
    marquee(2);
  }
}

void marquee(int win)
{
  if (win == 1){
  	for(int i = 0; i <= 5; i++)
  	{
    	for(int mar = 5; mar < 9; mar++)
    	{
      	charlieFlash(charliePath[mar][0], charliePath[mar][1], 70);
    	}
  	}
  } 
  else if (win == 2)
  {
  	for(int i = 0; i <= 5; i++)
  	{
    	for(int mar = 8; mar > 5 ; mar--)
    	{
      		charlieFlash(charliePath[mar][0], charliePath[mar][1], 70);
    	}
  	}
  }  
  
  delay(2500);
  gameRestart();
}
void gameRestart()
{
  button1State = LOW;
  button2State = LOW;
  player1Score = 0;
  player2Score = 0;
  delayTime = 100;
  dir = 0;
  vActive = 0;
  gameInstance = "wait";
  Serial.println("wait");
}
void charlieOn(int hi, int lo, int dly){
  pinMode(hi, OUTPUT);
  pinMode(lo, OUTPUT); 
  digitalWrite(hi, HIGH);
  digitalWrite(lo, LOW);
  delay(dly);
}
void charlieOff(int hi, int lo){
  pinMode(hi, INPUT);
  pinMode(lo, INPUT);
}
void charlieFlash(int hi, int lo, int dly) {
  pinMode(hi, OUTPUT);
  pinMode(lo, OUTPUT); 
  digitalWrite(hi, HIGH);
  digitalWrite(lo, LOW);
  delay(dly);
  pinMode(hi, INPUT);
  pinMode(lo, INPUT);
}

my code will not exit the volley loop

Where in the while() loop is vActive changed?

while (vActive == 1)
if (vActive = 1)

You are setting vActive to 1 at the end of the loop.

jremington:
Where in the while() loop is vActive changed?

vActive is set to 0 in a different loop, once the serve results in a point, the show score cycle through and if the points equal 4 then gamerestart loop fires. vActive is set in that loop.

Also jiaojiao that segment of code only runs if player 1 fires, this issue is happening on both player one and two. Also I changed it to if (vActive == 1) to no change. I guess == is comparative and = is set?

Im still stuck.

Here is the tikercad arduino simulation. Tinkercad | From mind to design in minutes

vActive is set in that loop.

In theory. Have you checked to see if that ever happens?

Another thing to check is memory. Use of String objects often leads to Arduinos crashing, because they tend fragment memory. For this reason, most forum participants recommend that you not use them.

I'll check into the string fragment issue, but I will say that I have removed a substantial amount of string variables from the original code.

Also, I have verified that the vActive variable does indeed switch to 0 at gamerestart and properly exits the while loop in volley. The sketch just never seems to return to the initial loop.

OK I modified the code and replaced the string variables. I had some screwy logic with the string as flags, so I dont know if it was the fragmentation or not. After the issues were resolved, everything seems to be working properly.

Now I just have to work on my score display logic. Thanks for all the guidance.