Tap, Double Tap, and Hold Button

I am trying to get one button to control two motors such that 1 tap turns on motorA, 2 taps turn on motorB, and holding it down will turn off both motors. I cannot think of a way to get code a double tap. Currently, I have it set up to print different text.

Any help or advice would be great,

Thanks in advance.

int button = 10

void setup() {

Serial.begin(9600); //configures serial port
pinMode(button,INPUT); //button is now an input
}

int currentPress = 0;
int prevPress = 0;

int countOn = 0;
int countOff = 0;

int state = 0;
// state 0 = No press
// state 1 = press
// state 2 = real press

int in = 0;

void loop() {

currentPress = digitalRead (10);

if (currentPress == 1) {
if (countOn == 2000) {
Serial.println("hold");
countOn == 2001;

// this loop basically says "if it's not 2000, keep adding 1 every millisecond until it reaches 2000,
then print "hold"

}

if (countOn == 20) {
Serial.println("press");
countOn = countOn + 1;
// this loop basically says "if it's not 20, keep adding 1 every milisecond until it reaches 20, then print
"press"
// once it has been activated at 2000, we change the value to 2001 so it won't keep saying "press"

}

//below is set up for a double tap, but I cannot figure out a way to differentiate this from the single press

if (countOn == 300) {
Serial.println("twice");
countOn = countOn + 1;
}
else {
countOn = countOn + 1;
}

}
else {
countOn = 0;
}

delay(1);

}

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;

if(digitalRead(10))
{
   currentMillis = millis();
   if(currentMillis - previousMillis >= 200 && !digitalRead(10))
   {
      //oneclick

      previousMillis = currentMillis;
      bool firstClick = true;

   }else if(currentMillis - previousMillis >= 200 && digitalRead(10) && firstclick)
   {

      //DoubleClick
      
      previousMillis = currentMillis;
      firstClick = false;

   }else if(currentMillis - previousMillis >= 2000 && digitalRead(10))
   {
      //long click (hold for 2000 ms or 2s)
      previousMillis = currentMillis;
   }

   firstClick = false;
}

You could try this code.. i just typed it on my phone out of the blue don't know if it will work but I think it might! :slight_smile:

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can I ask why you want to use just one button?
What model Arduino are you using?

Thanks.. Tom.. :slight_smile:

Some things to ask yourself - and build your code around it (#1 looks like a good starting point for that):

  • what is a single click, what is a long click - as in, how long does it have to be to register is either?
  • how long time is allowed between clicks for it to be a double click rather than two single clicks?
  • how long does a click have to be to become a long click rather than a short click?
  • what if that long click is part of the second click starting within the double click interval?
  • at what point in time/click sequence to have the motors react?

The last is probably the hardest as you want your motors to react instantly on a click, but also not switch on then switch off the one-click motor if a second click comes in, or switch it on then off as the single click turns out to become a long click. Or switch on the double-click motor when the second click turns out to be a long click.

I've done the short/long click scenario, including rate repeats, but not single/double/long. That adds a whole lot of complications. In the short/long click scenario you can simply react to a short click the moment the button is released, and reacting to it as long click after a certain time of it being pressed (and if the user doesn't release the button, do the repeat thing).

In this link there is a neat piece of code for multiple click types,

I am trying to get one button to control two motors such that 1 tap turns on motorA, 2 taps turn on motorB, and holding it down will turn off both motors.

IMHO that is a great example of poor user-input design. Even if it is programmed properly the user will often get it wrong.

...R