# If button is pressed 3 times within 1 second then..

Hi,

I'm searching for hours how to make this small sketch.

"If button is pressed 3 times within 1 second then.. blink a led for example.
If the button is pressed 3 < then shut the led off

I red the Using millis() for timing. A beginners guide But it's a bit to complicated for me to edit this code and make it work for me.
Can someone help me? Thanks.

Start with the basics. Do you know how to detect that a button has become pressed as opposed it being currently pressed ?

If so then you can count the number of times a button becomes pressed. If not then look at the StateChangeDetection example in the IDE

When you know that a switch HAS BECOME pressed, you can record when that happened. By keeping an array of times (for, say, the last 10 times), you can determine, anytime the switch becomes released, how many times that has happened in the last n seconds. If that number meets your criteria, take the appropriate action.

What you want to do needs some careful thought.

For example how do you know which button-press is the first one?

When the first button-press happens save the value of millis() and set the press count to 1
On each subsequent button-press add 1 to the count.

Keep checking the time with something like

``````if (millis() - firstButtonPressTime >= 1000) {
if (buttonCount == 3) {
// only 3 button-presses have happened within the second so do whatever
}
else {
// don't know what you want to happen here
}
}
``````

...R

I would suggest more or less what Paul was suggesting, i'd make a latch to determine if the button is in a released state, and when it changes to pressed, store the 'moment' in a ehm what you call these things, a form of ring array. only 3 'moments' need to be stored, either you copy the values to the next entry of the array, or have a variable point to the next entry in the array, and compare the last 'moment' (that from 3 presses ago) to the current moment. Robins solution does not account for the event tthat out of 4 presses the last 3 are within 1 second.

Wow, that's a lot of comments! Thanks.
The important thing is that one single button must be pressed 3 times within 1 second, to activate the LED.
If you press less then 3 times within the 1 second time period, the led must shut off.

UKHeliBob:
Start with the basics. Do you know how to detect that a button has become pressed as opposed it being currently pressed ?

If so then you can count the number of times a button becomes pressed. If not then look at the StateChangeDetection example in the IDE

I did look at the StateChangeDetection, and i think thats a part of the code that i hope to write, but i'm stuck there..

Robin2:
What you want to do needs some careful thought.

For example how do you know which button-press is the first one?

When the first button-press happens save the value of millis() and set the press count to 1
On each subsequent button-press add 1 to the count.

Keep checking the time with something like

``````if (millis() - firstButtonPressTime >= 1000) {
``````

if (buttonCount == 3) {
// only 3 button-presses have happened within the second so do whatever
}
else {
// don't know what you want to happen here
}
}

``````

...R
``````

Thanks, maybe this will help me.

i think thats a part of the code that i hope to write, but i'm stuck there..

Don't aim to write all of the code at once. Start by counting how many times the button is pressed. Print a message to the Serial monitor when the count reaches 3 and reset the count to zero ready for the next count of 3. You can add the timing element when you have got the counting working

record the times (ie result of millis()) of the last two presses, when the button becomes pressed you can look at
the three times and decide what to do. Then update the recorded times and do any required
action.

This is the most flexible approach for dealing with upto 3 presses, as all the decision logic sees the
complete state that it needs. You can add debouncing logic into this as well - if the current
press is too close in time to most recent previous press, simply ignore it as its a bounce.