Looping for a set amount of times.

PeterH:

Giland:
the for loop works if you set it up as a function instead of inside the void loop.

When the if loop is done, it would automatically return to void loop and look for another button press.

I can't interpret either of those sentences in any way that makes sense.

how about

the for command works if used in a separate function instead of being inside the void loop().
when the if loop has i in the function reach 5 (no longer less than 5), it will return back to the void loop() and wait for another button press.

for example

void loop ()
{// general code to look for button press, current state, etc etc

if (button == 3) buttonstate3;
}

int butttonstate3()
{
 {for(int i = 0; i < 5; i++)        
 {
  Serial.println(i);  
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  delay(400);

  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  delay(400);
  }
button = 4;
}}