Trouble shooting push button

Hi guys

I'm trying to write a code that makes an LED light up until the button is pressed. Once the button is pressed I want the light to blink 3 times and then stay off. I've tried every combination of if, while, else, etc. but I cant get it to do what I want.

I do need the for statement in there because this is just a proof of concept for another project I'm working on that requires it.

Any help or suggestions are much appreciated.

push_button_trouble.ino (662 Bytes)

Any good reason for not posting code?
Any good reason for posting this in "Installation and troubleshooting"?

Pretty simple:

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

void setup() 
{
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // Turn LED on.  It will stay on until the button is pressed
  digitalWrite(ledPin, HIGH);
}


void loop() 
{
  // if the state has changed, increment the counter
  if (digitalRead(buttonPin) == LOW) 
  {
    for (int i = 0; i < 3; i++)
    {
      digitalWrite(ledPin, LOW);
      delay(500);
      digitalWrite(ledPin, HIGH);
      delay(500);
    }
    digitalWrite(ledPin, LOW);
    while (1);
  }
}