Undesired loop

Here is a portion of a sketch that works as I intended it too, but then seems to get into an undesired loop.
The entire sketch is quite repetitive. It is essentially variations of this clip repeated 9 times.

I would like for the sketch to wait for one of three buttons to be pressed. When a button is pressed I would like the sketch to run a certain bit of code based on a randomly generated variable.

Here is the portion that is giving me trouble. If I could only fix this portion I could adapt it to fit the repetitious parts of the sketch.

void loop(){
randomSeed(analogRead(0));
randNumber = random(1,4); // 1 <= X <= 3
tempRand = randNumber;
Serial.println(“randNumber”);
Serial.print(randNumber);
delay(50);
digitalWrite(rledPin1, HIGH);
digitalWrite(rledPin2, HIGH);
digitalWrite(rledPin3, HIGH);
while(counter == 0) {
bouncer1.update();
buttonState1 = bouncer1.read();
if (buttonState1 == HIGH) {
pressValue = 1;
digitalWrite(gledPin1, HIGH);
delay(500);
counter++;
digitalWrite(gledPin1, LOW);
}
bouncer2.update();
buttonState2 = bouncer2.read();
if (buttonState2 == HIGH) {
pressValue = 2;
digitalWrite(gledPin2, HIGH);
delay(500);
counter++;
digitalWrite(gledPin2, LOW);
}
bouncer3.update();
buttonState3 = bouncer3.read();
if (buttonState3 == HIGH) {
pressValue = 3;
digitalWrite(gledPin3, HIGH);
delay(500);
counter++;
digitalWrite(gledPin3, LOW);
}
}
counter = 0; //resets counter to 0 for next button-read loops
Serial.println(“pressValue”);
Serial.print(pressValue);
switch(randNumber) {
case 1: //in the case that randNumber = 1
if(pressValue == 1) {
while(tempRand == 1) {
randomSeed(analogRead(0));
tempRand = random(1,4); //lockout either #2 or #3
Serial.print(“tempRand”);
Serial.print(tempRand);
}
switch(tempRand) {
case 2:
digitalWrite(rledPin2, LOW);
Serial.println(“Case 2”);
while(counter == 0) {
bouncer1.update();
buttonState1 = bouncer1.read();
if (buttonState1 == HIGH) {
pressValue = 1;
digitalWrite(gledPin1, HIGH);
delay(500);
counter++;
digitalWrite(gledPin1, LOW);
}
bouncer2.update();
buttonState2 = bouncer2.read();
if (buttonState2 == HIGH) {
pressValue = 2;
digitalWrite(gledPin2, HIGH);
delay(500);
counter++;
digitalWrite(gledPin2, LOW);
}
bouncer3.update();
buttonState3 = bouncer3.read();
if (buttonState3 == HIGH) {
pressValue = 3;
digitalWrite(gledPin3, HIGH);
delay(500);
counter++;
digitalWrite(gledPin3, LOW);
}
}
if(pressValue == randNumber) {
Serial.print(“Winner!”);
}else {
Serial.print(“Loser…”);
}
Serial.println(“pressValue”);
Serial.print(pressValue);
pressValue = 0; //RESET
tempRand = 0; //RESET
delay(50);
break;

This is output to the serial monitor:
randNumber1
pressValue1
tempRand2Case 2
Winner!
pressValue1
tempRand1tempRand2tempRand1tempRand1tempRand1tempRand1tempRand3tempRand1tempRand1…

I would for the sketch to return to the beginning of the entire loop after “Winner!” or “Loser…” is determined.

I would appreciate any direction anyone can give.
Thanks.

When you finish waiting for a button press the first time you set counter back to 0 but you don't do that after you look for the second button press.

[quote author=Cody Bill link=topic=87292.msg655011#msg655011 date=1326675908] Here is a portion of a sketch that works as I intended it too, but then seems to get into an undesired loop. The entire sketch is quite repetitive. It is essentially variations of this clip repeated 9 times. [/quote]

I haven't studied your code, but that comment makes me think there is scope to refactor the code to eliminate that redundancy. Don't repeat Yourself (DRY) is a fundamental principle of good software design, and any time you find yourself duplicating code you should ask yourself whether there's a better way to do it.