For loop into a function

In this section of code I believe you should be checking buttonState2 instead of buttonState :

  buttonState2 = digitalRead(buttonPin2);
  if (buttonState == LOW) {
    for (int i = 3; i <= 5; i++) {
      digitalWrite(ledPin, HIGH); // turn the LED on
      delay(50); // wait
      digitalWrite(ledPin, LOW); // turn the LED off
      delay(500); // wait
    }
  }