How to blink led only 2 times after push?

How do i blink a led only 2 times, every time when there is a push on a pushbutton?

I can only blink them continues and only while the button has been pushed.

It wil helping me a lot to get started with the basics program knowledge.
Google won't help me.

Thanks,

Every time you press the button it should blink twice, unless you keep pressing it fast.

This will not work if your pushButton is set up wrong. You have to set it up so one pin is connected to pin 2, also one pin of the button needs to connected to a 10k resistor which needs to be connected to ground.

int buttonPin = 2;
int ledPin = 13;

int buttonState = 0;

void setup() {
pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);
}

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

if (buttonState == HIGH) {
blink();
}
}
void blink(){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
}

Thanks for the quick response,
as simple as that, it gaves me a lot more insights.

But what about the fast pressing?
is there a solution?

Do you mean you want to queue the blinks? So if I press the button twice fast, it will do the function once and then again right after.

Keearix:
Do you mean you want to queue the blinks? So if I press the button twice fast, it will do the function once and then again right after.

Yes, thats exactly what i mean!

it has to do the function the same times as times pushed.

If you want to make a super simple buffer, create a variable called buffer.

You could attach the button to an interrupt pin and when the button is pressed it increments the buffer once regardless of what is currently executing.

Then in the main loop, you have a condition that checks if the buffer variable is greater than 1.

If it is, run your blink code and decrease buffer by one.

Pretty much what Apex said, here is the code altogether :

int buttonPin = 2;
int ledPin = 13;
int queue;

int buttonState = 0;

void setup() {
pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);
}

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

if (buttonState == HIGH) {
queue = queue + 1;
}
if (queue > 0){
blink();
}
}
void blink(){
queue = queue - 1;

digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
}

Mmmm, nope.

attachInterrupt(0, queueUP, FALLING);

void queueUP(){
queue = queue + 1;
}

is needed or else everything is sequential and your queue will be decremented immediately after being increased.

Yeah, I just tested the code and realised you also can't queue while it is running the function. Delays are bad at this point, you would have to create a variable called timer and change led on/off based of the timer point.

Eh. Depends on if OP's code is used for learning practices or is going into an actual project.
I can accept delay() in someone's code, if someone is still learning about how things operate.
But yeah, agreed, everything could run smoother if they were avoided all together.

Could be baby steps at this point.

Wow cool, thanks

At which place shoot i put the extra code in?
Its all more complicated then i thought.

In the end i want to run a stepper-motor by pulses of a Hall Effect Sensor.

ApexM0Eng:
Could be baby steps at this point.

:smiley:

attachInterrupt(); Is a function that initializes and sets the parameters of an interrupt.

So it should go in your setup(){}.
Here is the reference for interrupts. (With example code)

Your queue function (of whatever you want to actually name it) is just a basic void function.
It goes outside everything. It is called upon the interrupt happening. So make sure whatever you
end up calling it, the same name is used in attachInterrupt().

Also the that first 0 that I used represents pin 0 (Arduino Due) or Interrupt #(other MCUs). Make sure you use the correct pin for your setup, and that it has interrupt capabilities. Not all pins do.

Now, there are several ways you could go about queuing up functions. But if you have delays in your code, interrupts get around a variety of issues. Other ways involve using timers instead of delays, for example. This would also require new knowledge likely. I'd get one thing working then move onto the next.