Multi-function pushbutton

So I’ve gotten a new moped and thought of adding a multi-function pushbutton, that works in the way of pushing the button X amount of times within 5 seconds i.e 1 push within 5 seconds turns on some neon lights with a relay, 2 pushes turns on a speaker in the seat etc. And of course it should be able to toggle them off again with same amount of pushes for the corresponding action.

To do this my friend suggested using millis to count to 5 after the first push, detecting the number of inputs from the button within those seconds, resetting millis after 5 seconds until pushing it again and executing the action.

So I’m quite lost on how to reset millis when the 5’th second is hit and counting the inputs within these seconds.

The counting of pushes I think should be a simple integer that executes “if”.

I hope you guys could help me some. All suggestions are appreciated. :smiley:

try something like :

// use pin 5 for the button
#define pin1  5

void setup() {
  // put your setup code here, to run once:

  // set pin1 as input
  pinmode(pin1,INPUT_PULLUP);
  
}

void loop() {

  static unsigned long first_push=0;
  static push_count = 0;
  static previous_state = HIGH;
  
 if (digitalRead(pin1) == LOW)
 {
 
   // in case it is still pressed make sure it was release first
   if (previous_state == HIGH)
   {	
   	// maybe include a debounce code here

    	// count push
    	push_count++;
    
    	// if first time detect, save the time
    	if (first_push == 0) first_push= millis();
       
       // The press was detected and counted 
       previous_state = LOW;
    }
 }
 else
 	// remember it was released
 	previous_state = HIGH;

 // if a push was detected before
 if (first_push > 0)
 {
    // 5 seconds is 5000ms
    if ( first_push + 5000 > millis())
    {
      // here your action depending on push_count
      if (push_count == 1) dosomething();
      else if (push_count == 2) dosomething_else();
      else if (push_count == 3) dosomething_else_else();
      
      // reset
      push_count = 0;
      first_push = 0;
    }
}

The line

if ( first_push + 5000 > millis()) should read

if ( first_push + 5000 < millis())