Button pressed 3 times

Hello,
I want to write a code which will work at that simple rule:
If i press the button 3 times then something...

I don't know how I can write that program. Can someone help me and give some example of that code ? :sob:

Sorry for my bad English. I hope you will know what I mean.

First, gain an understanding of *debouncing * and state change detection . There are example sketches (and many more) in the IDE: file/examples/digital/xxx.

Second, you'll need to define some operating parameters. Press three times? Once today, once tomorrow...? Or maybe once every second or three times within one second? James Bond like - one short press, two long presses?

As above...

Read about INPUT_PULLUP pin mode.

  1. Learn how to detect a button (input) press and release (state change)

  2. Then put a counter variable outside that, and increment the value each time your button is pressed.

There will be close to 10,000 examples online which will help you with #1
Then come back, and we’ll give specifics to do #2

The whole solution will be less than ten lines of code.

Can I have a example code, please ?

No, but you can make a start, and we’ll help you move forward by learning.

(Please use code tags when posting code snippets)

One of the key statements for you is digitalRead() , but there is a bit more to it than just that as others have already pointed out.

Detecting three presses involves detecting one press when the number of previous presses is 2.

So you have to store that state (number of previous presses) in a variable.

Code like this is usually written as a state-machine, I'd recommend learning about state machines.

Go easy on the OP !
Let him detect a switch closure, then count them...

State machines may come in later but he’s a long way from there.

const byte ButtonPin = 2;  // Wire button between this pin and Ground
unsigned ButtonPressCount = 0;


void setup()
{
  pinMode(ButtonPin, INPUT_PULLUP);
}


void loop()
{
  static unsigned long buttonPressTime = 0;


  if (!digitalRead(ButtonPin) && (millis() - buttonPressTime > 10))
  {
    buttonPressTime = millis();  // Debounce timer start
    ButtonPressCount++;
  }
  
  if (ButtonPressCount == 3)
  {
    // Something
  }
}
2 Likes

I hope the OP learns something JW.
You have certainly helped, but he didn’t lift a finger to help himself...

Thanks for a help. I'm a beginner of arduino. I'm at "blinking led". I don't want to learn now because I want to build useful project for my vape and program a very tiny arduino. I'm not working with arduino everyday.

lastchancename:
I hope the OP learns something JW.
You have certainly helped, but he didn’t lift a finger to help himself...

I hope they learn something, too. As written, the program does nothing. If they want it to do the "something" in the problem description then they will have to figure that out. And if they want the program to do anything after the third press, they will have to figure that part out, too.

And those presses had better be short since I forgot to put in StateChangeDetection. :slight_smile:

2 Likes

mr_surykatek:
Can I have a example code, please ?

Which part of file/[color=blue][b]examples[/b][/color]/digital/xxx is unclear?

. I don't want to learn now because I want to build useful project

Unfortunately says it all...
The force is weak with this one.

As mentioned, there are examples in the IDE that cover basic concepts.

If you spend the time and master these examples, you will learn how to do things like this.

When you write a sketch and run into problems, come here and ask for help.

mr_surykatek:
Thanks for a help. I'm a beginner of arduino. I'm at "blinking led". I don't want to learn now because I want to build useful project for my vape and program a very tiny arduino. I'm not working with arduino everyday.

I think this is what you are looking for. This way, you don't have to learn now.

mr_surykatek:
Thanks for a help. I'm a beginner of arduino. I'm at "blinking led". I don't want to learn now because I want to build useful project for my vape and program a very tiny arduino. I'm not working with arduino everyday.

https://forum.arduino.cc/index.php?board=26.0

I think this is what you are looking for. This way, you don't have to learn now.