Make an led blink a specific amount of times in void-loop?

How could I make an led blink a specific amount of times in the void-loop part of a sketch (Lets day 5 times when I first push a button)? I think I would have to use the button state detection sketch, but I don't understand it... Could someone explain it to me (or does anybody have a good link somewhere on the internet)? Thank you!

Oh and here is the Arduino example sketch...

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

  
  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
  
}

You might need to debounce the button, otherwise the code may see a pile of on/offs for every press, depending on the button or sensor.

If you're real tricky, you can have a lit led for a sensor that when you cover it with your finger acts like a button. It takes more code than to debounce a pushbutton, will only move if you make it able to move and won't work in the dark but otherwise is kind of neat.

When you say led blink, there's mainly 2 kinds here. The first is that while the led blinks, nothing else happens. Even the button will not respond. The second is BlinkWithoutDelay where you can make other things happen "at the same time" the led blinks.

The second, BWD, is one of the Examples sketches in your IDE, the first one in the 02.Digital sub-menu.

For this application, I need to have a buzzer and led blink 5 times when a toggle SWITCH (and not a button as I said earlier, sorry) is first toggled to the on position, after the 5 led/buzzer blinks, the led has to stay on continuously (that is until the switch is toggled again). So I would need the blink without delay... How should I proceed?

Start with blink without delay. Add a variable which records the number of blinks that have been completed. Initialise this to zero in code which executes at the start of your blink sequence. Each time you complete a blink cycle, increment the count. If the count has reached the required number of blinks, don't perform any more blinks.

PeterH: Start with blink without delay. Add a variable which records the number of blinks that have been completed. Initialise this to zero in code which executes at the start of your blink sequence. Each time you complete a blink cycle, increment the count. If the count has reached the required number of blinks, don't perform any more blinks.

I am kinda bad at this... Could you please make a quick example sketch (just a few lines to clear that up)?

See what you can do for yourself first.

BWD teaches how to make something happen on time using an if() inside of loop() that goes around and around.

When you can make something happen based on a time event then you can make something happen based on an input (button or sensor) or accumulation of factors that change a variable or variables or combination of any of those.

So you divide what you want the sketch to do into stages or process states or whatever you want to call them and determine the triggers for each. If something has to wait then end one stage setting up for the next by setting the wait start and interval and start the next with the time check, if (now - start >= interval). To get each stage to run, use a variable and have each stage change that when it is finished and the next should run.

If I should have it blink 5 times then I'd need to set up a blink countdown (so the blink code isn't stuck with 5) and the time variables and then set the stage to run the blink code that uses those. When it's done, change the stage and whatever else needs changing.

But first, learn BlinkWithoutDelay or learn from this more well-explained blog on Arduino multi-tasking: http://www.gammon.com.au/forum/?id=11411

OK thanks, I will try some things with the website.

The example in this Thread might be useful.

...R

I link to Nick's blog because it has plain simple explanations that cover the concept of blocking vs non-blocking. If they can work it out from BWD then Nick's blog is a more complete and sensible explanation.

If you've never seen it, go have a look at how it's done.

It's also a good meter of the student because if you can't pick that up then you either need to cover basics or your attitude/attention span or maybe you're just some other way not cut out to write code past the Weekly Reader level.

If I link someone there and it's clear they didn't bother while still pestering for code then why bother waste my time on that person? I spend as long or longer on a lot of posts than it takes to go through that. I'd rather my time went to someone that won't waste it.