How do I stop a running function by pressing a button?

Hello guys.
I used one button to implement various functions. I made One Click, Double Click, and Long Press using the button. And now I'm going to make a function that cancels the running function by pressing the button.

void loop() {

  Button(); 

}

//========================================================================================================================

void Button(){      //Button function
    
  if(btn_flag && ((millis()-time)>1000) && !start_flag){      //once click button
    Serial.println("one click");
    Function_1(); //Run function 1  
    btn_flag = false;

  }
  
  else if(btn_flag && start_flag){ 

    //Blinking at power-on
    digitalWrite(led1, HIGH); 
    digitalWrite(led2, HIGH);
    delay(100);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    delay(100);
    digitalWrite(led1, HIGH); 
    digitalWrite(led2, HIGH);
    delay(100);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW); 

    start_flag = false;
    btn_flag = false;

  }
    
  if(digitalRead(sw) == HIGH){    //press button   
    if(btn_flag && ((millis()-time)<1000) ){        // double click button
      Serial.println("double click");
      Function_2(); //Run function 2
      btn_flag = false;
    }

    else {
      time = millis();    
      btn_flag = true;  
    }      
    delay(10);
    
    while(digitalRead(sw) == HIGH){               // Press 3 seconds to enter the sleep mode.
      if((millis()-time) > 3000){
        start_flag = true;
        sleepNow();
        return;
      }
    }
    delay(10);
  } 
}

And this is the result of pressing the button. I tested it with LED now, but I will use transistor and motor etc later.

void Function_1(){ //once click : LED 5sec ON
  digitalWrite(led1, HIGH);
  delay(500);
  
  while(digitalRead(led1) == HIGH)   //I made this code for cancellation function. But it doesn't work properly.
  {
    if(digitalRead(sw) == HIGH)
    {
      Stop();
      break;
    }
    else if(digitalRead(sw) == LOW)
    {
      delay(5000); //5sec ON
      digitalWrite(led1, LOW);
      break;
    }
  }
}

void Function_2(){ //double click : blinking 5times
  for(int i = 0; i <5; i++){
    digitalWrite(led2, HIGH); 
    delay(500);
    digitalWrite(led2, LOW); 
    delay(500);    
  }
}

//========================================================================================================================

void Stop()  //Cancel Running Function
{
  Serial.println("STOP");
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
}

I've tried use interrupt. However, if I press the button and run the function, the interrupt runs together and ends at the same time as it starts.

I might have miswritten it because I used a translator. Thank you for reading.
I'd really appreciate it if you could tell me how to fix it...!!

You might want to investigate the example in the IDE ‘State Change Detection’.

Do you know anything about the ‘State Machine’ programming technique ?

Interrupts and switches almost never go together.

You are using BWD and delay() in the same sketch, never a good idea, only use BWD.

Define these: One Click, Double Click, and Long Press


Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu. [code]Paste your sketch here[/code]

I think it'll help a lot. Thank you!

yscheol:
And now I'm going to make a function that cancels the running function by pressing the button.

One way to do that is to have a variable (let's call it functionMayRun) and when you press the button it changes the value from true to false. The function that you want to stop would be written something like this

void myFunction() {
   if (functionMayRun == false) {
      return;      // i.e. do nothing
   }
   // rest of the function code
}

and the code in loop() would be something like this

void loop() {
   checkButtons();
   myFunction();
}

Based on your description I'm not sure what would set functionMayRun to true - perhaps it would be one of the other button clicks.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R