I need a time counter for a on/off action

My problem is the following, I need to install an on / off button that works in such a way by pressing 5 times quickly and consecutively a push button, this button activates a circuit in which there can be a led, a motor or what it is necessary to turn on. Therefore, these 5 "fast and consecutive" beats must depend on time; that is, for example, for the circuit to be activated (or desactivated), 5 consecutive pulses are needed, which must be executed in a maximum time of 7 seconds. I have been using millis (), but every time I feel I have THE code, another problem arises. Among these is the fact of how to do to initialize the time count (7 seconds); since, this must be initiated with a pulsation which depends entirely on the user.

P.S. I am using an Arduino LEONARDO.

The following code is the one I am using, although it is still far from what I want to do.

P.S. 2 I am peruvian, that's why my variables are in spanish.
Any hero?

#define PULSADAS        5
#define INTERVALO       4000

const byte buttonPin    = 2;    
const byte ledPin       = 11;
const byte ledpin2      = 12;      

// Variables will change:
int buttonPushCounter   = 0;
int buttonPushCounter2  = 0;
bool buttonState        = false;        
bool lastButtonState    = false; 
bool flag               = false; 
bool ledpinstate        = false;
unsigned long start;   

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  pinMode(ledpin2, INPUT);
}


void loop() {

  buttonState = digitalRead(buttonPin);
  
  if (buttonState && !lastButtonState) {
      if (buttonPushCounter++ == 1) {
          start = millis(); // inicio conteo de 7 seg
           Serial.println("pushcounter");
             Serial.println(buttonPushCounter);
      if (buttonPushCounter == PULSADAS) { // hay 5 pulsaciones => verifico tiempo
          unsigned long dif = millis()-start;
          if (dif <= INTERVALO) { // todo se expresa en milisegunedos 7000 = 7 segundos 
              flag = true;
              Serial.println("Se han pulsado 5 veces dentro de 7 seg");
          }
          else {
              flag = false;
              Serial.println("No se han pulsado 5 veces dentro de 7 seg");
          } 
          buttonPushCounter = 0;
          
      }
      Serial.print("Numero de pulsadas : ");
      Serial.println(buttonPushCounter);
        Serial.print("flag y ledPinState: ");
  Serial.println(flag);
  Serial.println(ledpinstate);
 ledpinstate = digitalRead(ledpin2);
  }
lastButtonState = buttonState;
  if (flag == true && ledpinstate == true) {
       digitalWrite(ledPin, LOW);
  }
  if (flag == true && ledpinstate == false) {
      digitalWrite(ledPin, HIGH);
}

}

Show us what you have done so far.

Use CTRL T to format your code.
Attach your sketch between code tags
[code]Paste your sketch here[/code]

This should (not tested) get the vape thingy turned on. How does it turn off?

const byte pinSw = 2;
const byte pinLed = LED_BUILTIN;

byte
    currSw,
    lastSw;

bool
    bONOFFState;

void setup( void )
{
    pinMode( pinLed, OUTPUT );
    digitalWrite( pinLed, LOW );
    pinMode( pinSw, INPUT_PULLUP );
    lastSw = digitalRead( pinSw );

    bONOFFState = false;
    
}//setup

#define TIMEOUT     2000ul
#define NUM_CLICKS  5

void loop( void )
{
    static unsigned long
        timeSw;
    static byte
        pressCount = 0;

    //if we've got at least one press, check for a timeout
    //if timeout occurs, reset the press counter back to zero    
    if( pressCount > 0 )
    {
        if( millis() - timeSw >= TIMEOUT )
            pressCount = 0;
            
    }//if

    //if the system is already on (bONOFFState = true) don't do button reads anymore
    //can be changed to a simple state machine; when transitioned to ON, state changes
    //to a state that looks for the key pattern required to turn it off again...
    //for now, just turn it on
    //
    //read the switch; if it's not the same as last time and it's low...
    currSw = digitalRead( pinSw );
    if( currSw != lastSw & !bONOFFState )
    {
        lastSw = currSw;
        if( currSw == LOW )
        {
            //...if this is the first press (out of reset or after a timeout),
            //grad the millis() count
            //we only do this for the first press because it's from that point
            //that we time the TIMEOUT
            if( pressCount == 0 )            
                timeSw = millis();

            //bump the presscount
            pressCount++;
            //have we reached NUM_CLICKS? ...
            if( pressCount == NUM_CLICKS )
            {
                //yes, do something. Here I just light the Arduino's built-in LED
                digitalWrite( pinLed, HIGH );
                bONOFFState = true;
                
            }//if
            
        }//if
        
    }//if
    
}//loop
void setup( void )
{
    pinMode( pinLed, OUTPUT );
    digitalWrite( pinLed, LOW );
    pinMode( pinSw, INPUT_PULLUP );
    lastSw = digitalRead( pinSw );

    bONOFFState = false;
   
}//loop

That comment looks REALLY stupid.

PaulS:

.

.

}//loop



That comment looks REALLY stupid.

That comment? The "//loop" typo at the end of setup?

I see you're generally a miserable **** towards just about everybody on the forum Paul so I'll just attribute this complete overreaction to that. Have a nice day.

Blackfin:
I see you're generally a miserable **** towards just about everybody on the forum Paul so I'll just attribute this complete overreaction to that. Have a nice day.

When someone points out a silly mistake in one of my Posts I say "thank you" and make the necessary correction.

...R

Blackfin:
This should (not tested) get the vape thingy turned on. How does it turn off?

Well, the vape must turn off with other 5 fast clicks. I just compiled your code and sadly, nothing happens. Honestly, I am a beginner so there are many commands and functions on that code that I can't understand clearly yet. What I need are two things: a counter of time and a counter of pushes which can work parallel. Like I said, my vape must turn on counting 5 clicks on an interval of (just for example) 5 seconds. If we set up the vape like this, my main problem is how can we set the initializer because the program should be initialize by one click which is totally random, because it depends of the user. On the other hand, I have been using millis() but I couldn't figure out where I should putting the command.

By the way, I am using Arduino LEONARDO.

HELP, PLS!

This typo might cause a problem:

if( currSw != lastSw & !bONOFFState )

Change to:

if( currSw != lastSw && !bONOFFState )

Code will need to be modded to turn things off. Should be easy to do.

Robin2:
When someone...

is needlessly confrontational (as that member is in just about every post of his I've seen) I return in kind.

pierocosme:
Well, the vape must turn off with other 5 fast clicks.

You say that as if it is common knowledge. It is not. Try to be more descriptive when stating your intentions. And post some code, you have shown little evidence of any effort.

The first subject in this section of the arduino forum is a sticky entitled Using millis() for timing. A beginners guide.

link Using millis() for timing. A beginners guide - Introductory Tutorials - Arduino Forum

The guide leads up to an example almost identical to your needs, but it's main benefit for you is the clear and concise explanation of what the code is doing and how to use it, in addition it will serve as a great reference for future projects.

Blackfin:
is needlessly confrontational (as that member is in just about every post of his I've seen) I return in kind.

I have remarked to @Pauls about his poor "bedside manner" on many occasions - he seems to have good days and bad days :). Nevertheless if you are prepared to put that aside his advice is usually sound.

...R