Nubie needs help with running loop then resetting

I'm a complete beginner to Arduino and C++ so please be gentle.

The code below works fine if i press but2, LED blinks three times then stops, but if I release but2 and repress LED does not blink because blink is > 3 now.
I want to be able to press but2 multiple times and have it blink the LED three times each time.
How do I create a counter that resets after each button push?

if ((but1==0) && (but2==1) && (but3==0)) //if #2
{
if (blink <3)
{
digitalWrite(13, HIGH);
delay (100);
digitalWrite(13, LOW);
delay (100);
++blink;
} // blink
} //if #2

Welcome to the forum

Please post your complete sketch

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Your posted code is the most simple code to do what you described blink 3 times on first button-press. (but no more)

Your wanted functionality to be able to make the LED blink 3 times on each button-press requires additional lines of code.

As long as you use delay() there is trouble ahead.
For such a supertiny small code delay() is sufficient.
It will work - more or less - for blink three times on each button-press.

But as soon as you go some steps beyond that
using delay() will delay you for hours and days making code that does more than blink an led 3 times.

So even if you think I want a quick solution I recommend that you learn what non-blocking coding is. Especially non-blocking timing.
As soon as you have understood how non-blocking timing works
and
have understood how state-machines work
the world of programming is open to you.
and this will speed-up writing code to be 5 times faster as without it.

So what is your decision:
learning to think linear dead-end-route (for more complex programming)
or
learning non-blocking coding which opens up all possabilities?
(circular most outer looping)

best regards Stefan

Thanks Stefan and I hear what you are saying.
I will work on understanding nonblocking code, would you recommend using the millis function for nonblocking timing loops?
James

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.