Cancel a function before it's finished

Hello!

I want to cancel a function at any time.

As you can see under, the function "Program1" is played while "GREIN1" is at program 1 (case 0).
I want to use an external button to cancel Program1, at any time. It is a rather long-lasting program, so I can't wait for it to finish before I may change the program.
Is this even possible?
As a last resort I ask you. I am very thankful for any replies.

#include <CapacitiveSensor.h>
CapacitiveSensor   C1 = CapacitiveSensor(A8,A9);
CapacitiveSensor   C2 = CapacitiveSensor(A8,A10);
      int GREIN1 = 1;
      int MENY = 1;
      int TIMER = 100;
void setup() {
     Serial.begin(9600);
     pinMode(2, OUTPUT);
     pinMode(3, OUTPUT); 
     pinMode(4, OUTPUT);
     pinMode(5, OUTPUT);     
     pinMode(6, OUTPUT); 
     pinMode(7, OUTPUT);   
}
void loop(){
    switch(MENY){
    case 0:
    switch(GREIN1){
    case 1:
      Program1();
      kontroll(1, GREIN1, 1, +7);
    break;
    case 2:
      Program2();
      kontroll(1, GREIN1, 1, -1);
    break;
    case 3:
      Program3();
      kontroll(1, GREIN1, 1, -1);
    break;
    case 4:
      Program4();
      kontroll(1, GREIN1, 1, -1);
    break;
    case 5:
      Program5();
      kontroll(1, GREIN1, 1, -1);
    break;
    case 6:
      Program6;
      kontroll(1, GREIN1, 1, -1);
    break;
    case 7:
      Program7;
      kontroll(1, GREIN1, 1, -1);
    break;
    case 8:
      Program8();
      kontroll(1, GREIN1, -7, -1);
    break;
    break;
    }
      break;
      case 1:
      Meny1();
      kontroll2(-1, MENY, 1, 1);
      break;
      case 2:
      Meny2();
      kontroll(1, MENY, -1, -1);
      break;
      case 3:
      Meny3();
      kontroll2;
      break;
    }
} 
void kontroll(int a, int b, int c, int d){
  long start = millis();
  long total1 = C1.capacitiveSensor(1);
  long total2 = C2.capacitiveSensor(1);
    if(total1 > 100 && total2 > 100){delay(50);if(total1 > 100 && total2 > 100){MENY = MENY + a;}}
    if(total1 > 100 && total2 < 100){delay(50);if(total1 > 100 && total2 > 100){b = b + c;}}
    if(total1 < 100 && total2 > 100){delay(50);if(total1 > 100 && total2 > 100){b = b + d;}}
}

void kontroll2(int a, int b, int c, int d){
  long start = millis();
  long total1 = C1.capacitiveSensor(1);
  long total2 = C2.capacitiveSensor(1);
    if(total1 > 100 && total2 > 100){delay(50);if(total1 > 100 && total2 > 100){MENY = MENY - 1;}}
    if(total1 > 100 && total2 < 100){delay(50);if(total1 > 100 && total2 > 100){TIMER = TIMER *= 2;}}
    if(total1 < 100 && total2 > 100){delay(50);if(total1 > 100 && total2 > 100){TIMER = TIMER /= 2;}}
}
void Meny1(){
  digitalWrite(2, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  delay(500);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  delay(500);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  digitalWrite(3, LOW);
}
void Meny2(){
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  delay(500);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  digitalWrite(3, LOW);
  digitalWrite(2, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  delay(500);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(3, LOW);
  digitalWrite(2, LOW);
}
void Meny3(){
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  delay(500);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  digitalWrite(2, LOW);
  digitalWrite(2, LOW);
  delay(500);

}
void Program1(){
  digitalWrite(2, HIGH);
  digitalWrite(4, HIGH);
  delay(TIMER);
  digitalWrite(4, LOW);
  digitalWrite(2, LOW);
  delay(TIMER);
}
void Program2(){
  digitalWrite(2, HIGH);
  digitalWrite(5, HIGH);
  delay(TIMER);
  digitalWrite(5, LOW);
  digitalWrite(2, LOW);
  delay(TIMER);
}
void Program3(){
  digitalWrite(2, HIGH);
  digitalWrite(6, HIGH);
  delay(TIMER);
  digitalWrite(6, LOW);
  digitalWrite(2, LOW);
  delay(TIMER);
}
void Program4(){
  digitalWrite(2, HIGH);
  digitalWrite(7, HIGH);
  delay(TIMER);
  digitalWrite(7, LOW);
  digitalWrite(2, LOW);
  delay(TIMER);
}
void Program5(){
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(TIMER);
  digitalWrite(4, LOW);
  digitalWrite(3, LOW);
  delay(TIMER);
}
void Program6(){
  digitalWrite(3, HIGH);
  digitalWrite(5, HIGH);
  delay(TIMER);
  digitalWrite(5, LOW);
  digitalWrite(3, LOW);
  delay(TIMER);
}
void Program7(){
  digitalWrite(3, HIGH);
  digitalWrite(6, HIGH);
  delay(TIMER);
  digitalWrite(6, LOW);
  digitalWrite(3, LOW);
  delay(TIMER);
}
void Program8(){
  digitalWrite(3, HIGH);
  digitalWrite(7, HIGH);
  delay(TIMER);
  digitalWrite(7, LOW);
  digitalWrite(3, LOW);
  delay(TIMER);
}

Friendly regards
Anders Slettevold

In your Program1 code look at the switch every now and then.
When it pressed, return.

You can't "cancel" a function. You can call return from that function at any time.

I want to use an external button to cancel Program1, at any time. It is a rather long-lasting program, so I can't wait for it to finish before I may change the program.

Then, you have written it incorrectly. Periodically, the function should check to see that it need to continue running, at a minimum.

Is this even possible?

Not in the way that you are thinking.

Here is a short snip of the program:

Wrong on two counts. There are two threads at the top of the forum. Read them to determine why.

I want to use an external button to cancel Program1, at any time. It is a rather long-lasting program, so I can't wait for it to finish before I may change the program.

Then, you have written it incorrectly. Periodically, the function should check to see that it need to continue running, at a minimum.

Might I ask, how is that done?

Here is a short snip of the program:

Wrong on two counts. There are two threads at the top of the forum. Read them to determine why.

I agree that was lazy, and not very polite of me.

andersslettevold:

Periodically, the function should check to see that it need to continue running, at a minimum.

Might I ask, how is that done?

In your case, it would appear that the signal to discontinue running the function is the state of a switch. So you just need to attach a switch to a pin, and check for it periodically within the function. When the switch activates, do a return, which will exit your function.

For examples of reading a switch, see the examples directory that came with the Arduino IDE.

In your case, it would appear that the signal to discontinue running the function is the state of a switch. So you just need to attach a switch to a pin, and check for it periodically within the function. When the switch activates, do a return, which will exit your function.

For examples of reading a switch, see the examples directory that came with the Arduino IDE.

I'll try that for now. Though I have several delays within the function, this seems to be a good option. Thanks.

andersslettevold:
I'll try that for now. Though I have several delays within the function, this seems to be a good option. Thanks.

Check out the example sketch called Blink Without Delay to see how to time things without using delay(). The problem with using delay() is that your Arduino is not going to be doing anything else while waiting for the delay() to finish. You can use millis() to time things, while still letting the Arduino check for other things, like a button press, for example.

Check out the example sketch called Blink Without Delay to see how to time things without using delay(). The problem with using delay() is that your Arduino is not going to be doing anything else while waiting for the delay() to finish. You can use millis() to time things, while still letting the Arduino check for other things, like a button press, for example.

That might just be the solution! Thank you, lar3ry.