LED blink once with chain switch

So i have a chain switch VVV

and i want to use it to blink a led once, and when pulled again blink once again, and so forth. I have this code:

const int buttonPin = 7;     // the number of the pushbutton pin
const int ledPin =  4;      // the number of the LED pin
 
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
 
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}
 
void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
 
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);

  }
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
  }
}

but the button now continuously blinks. Does someone know how to only let it blink once?

Tnx!

Pseudocode

if storedState not equal to measuredState
{
debounce delay
storedState := measuredState
do the things you want to do when there is a state change
}

It's not as complex as what you've coded... especially since the response to on and off is supposed to be the same.

aarg:
if storedState not equal to measuredState
{
storedState := measuredState
do the things you want to do when there is a state change
}

i just put this is my code?

Xypod13:
i just put this is my code?

As stated in @aarg's post it's pseudocode.

Read about State Change Detection

dougp:
As stated in @aarg's post it's pseudocode.

Read about State Change Detection

Ok, I think I get it, but not sure how to put it in my code :{

Srr I'm just learning

Xypod13:
i just put this is my code?

You mean this?

do the things you want to do when there is a state change

The art of programming hasn't advanced yet to the point where a computer can read your mind.

aarg:
The art of programming hasn't advanced yet to the point where a computer can read your mind.

XD yeah srr, was talking about the code that you posted.

But @dougp said State Change Direction, but i don't know how to put it in my code correctly.