How would you get rid of delay in this example

So I have been trying very unsuccessfully to remove the delay components ( delay(blinkTime); ) of this script minus the 10 second one which is necessary for the button push.

int buttonState = 0;
int blinkTime = 200;
int ledPin = 13;

void setup()
{
  pinMode(2, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop()
{
  buttonState = digitalRead(2);
  if (buttonState == HIGH) {
  for (int i = 0; i < 5; i++)
    {
      digitalWrite(ledPin, HIGH);
      delay(blinkTime);
      digitalWrite(ledPin, LOW);
      delay(blinkTime);
    }
  } else {
    digitalWrite(ledPin, LOW);
  }
  delay(10);
}

The desired effect is that when I press the button, the LED flashes 5 times then goes to low.

Look inside the examples-library... There's an example called "Blink without Delay". Might be just what you are looking for...

Just the same way as you flash the led without using delay, but add a state variable to count the flashes...

int count = 0

if (button pressed) {
  count = 5
  last_time = now
  led = on
}

// flash the led, stop when count == 0
if (timer popped and count > 0) {
  if (led == on) {
    led = off
    count = count - 1
  } else {
    led = on
  }
  last_time = now
}

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

this may be a bit simpler

int buttonState = 0;
const unsigned long BlinkTime = 200;

int ledPin = 13;
byte butPin = A1;

void setup()
{
    pinMode(butPin, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);
}

void loop()
{
    buttonState = digitalRead(butPin);

    if (buttonState == LOW) {
        digitalWrite(ledPin, LOW);
        return;
    }
    
    static unsigned long msecLst = 0;
           unsigned long msec    = millis();

    if (msec - msecLst > BlinkTime)  {
        msecLst = msec;
        digitalWrite(ledPin, ! digitalRead (ledPin));
    }
}

gcjr:
this may be a bit simpler

It is - but it also doesn't fulfil the OP's "only blink 5 times when I press the button" requirement?

I agree it is simpler from the code point of view using the ! (NOT) logic to invert the state of the pin. But it's also, in some ways, "more complex" for a beginner to understand.

And, if you want to do only 5 flashes, you then have to have a counter from 10 (or perhaps 9) to count half cycles for both ON and OFF periods, instead of whole cycles. Or introduce an IF anyway so as to only decrement count from 5 on the OFF edge....

Hence why I coded the "if its on... turn it off... else... turn it on...". Easy for beginner to understand, especially if they are not already familiar with boolean logic, the ! (NOT) operator, and that fact you can read from an output pin.

Not difficult concepts to grasp I grant you, but every little simplification helps for someone who is just starting out (in my book anyway, YMMV).

pcbbc:
It is - but it also doesn't fulfil the OP's "only blink 5 times when I press the button" requirement?

you're right. didn't read his last sentence

int butLst;
int buttonState = 0;
int blinkTime = 200;

int ledPin = 13;
int butPin = A1;

#define OFF HIGH
#define ON  LOW

void setup()
{
    pinMode(A1, INPUT_PULLUP);
    butLst = digitalRead (butPin); 

    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, OFF);
}

void loop()
{
    buttonState = digitalRead(A1);
    if (butLst != buttonState) {
        butLst = buttonState;

        if (LOW == buttonState)  {
            for (int i = 0; i < 10; i++) {
                digitalWrite(ledPin, ! digitalRead (ledPin));
                delay(blinkTime);
            }
        }
    }
}

Thanks for the example! Worked like a charm except now I have to get the delay out of there. I flipped these to gain the effect I needed.

#define OFF LOW
#define ON  HIGH

I've run into this before. The button must be held to continue to work. I'm hoping that I can simply engage the button and it will carry out 5 of the same on off sequences.

gcjr:
this may be a bit simpler

int buttonState = 0;

const unsigned long BlinkTime = 200;

int ledPin = 13;
byte butPin = A1;

void setup()
{
    pinMode(butPin, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);
}

void loop()
{
    buttonState = digitalRead(butPin);

if (buttonState == LOW) {
        digitalWrite(ledPin, LOW);
        return;
    }
   
    static unsigned long msecLst = 0;
          unsigned long msec    = millis();

if (msec - msecLst > BlinkTime)  {
        msecLst = msec;
        digitalWrite(ledPin, ! digitalRead (ledPin));
    }
}

code needs to detect a change in button state and then test for the button depressed state

   if (butLst != buttonState) {
        butLst = buttonState;

        if (LOW == buttonState)  {

chipnod2020:
I've run into this before. The button must be held to continue to work. I'm hoping that I can simply engage the button and it will carry out 5 of the same on off sequences.

See post #2 for the pseudo code for this. Just put together the bits of code in this thread to implement it.