How to start a program AFTER attachInterrupt

Hello,

you can't use a delay in attachinterrupt, but is there a way to start a program after the interrupt? Like interrupt a program --> Start a new program

Set a flag inside the interrupt. When the sketch returns from the interrupt and the flag is set, call your program (function). The function can reset the flag so it can be set again the next time the interrupt fires. If that is not what you want, you need to provide more detail about what you want to accomplish.

groundfungus:
Set a flag inside the interrupt. When the sketch returns from the interrupt and the flag is set, call your program (function). The function can reset the flag so it can be set again the next time the interrupt fires. If that is not what you want, you need to provide more detail about what you want to accomplish.

The problem is that you need to call all the time to check that flag value, or am I wrong there?
What I want is the following:

Program1 is running
attachInterrupt(Input, TempProgram1, RISING);
Stop Program 1 and go to Program2, after Program 2 has been done, start void loop() again

You are right, but the loop() function runs over and over continuously (ideally). You don't call loop(). A properly written sketch will execute loop hundreds or thousands of times per second so the flag should be read as often as loop() executes.

Please describe what you are trying to do. It will be easier to help with a specific problem than write a general tutorial on interrupt handling.

groundfungus:
You are right, but the loop() function runs over and over continuously (ideally). You don't call loop(). A properly written sketch will execute loop hundreds or thousands of times per second so the flag should be read as often as loop() executes.

Please describe what you are trying to do. It will be easier to help with a specific problem than write a general tutorial on interrupt handling.

I am making a servo car that can drive through mazes. I have a program that continuously scans the area and then makes descisions based on the walls around it (Like in front a wall and on the left a wall, then it will turn Right.) So It is a gigantic loop that takes 5 seconds or so. That loop/program needs to be interuppted when A whisker(input) is triggered (means it has hit an object it has not seen). That should make the car drive backwards and scan the area and then it should turn and drive away. The problem is that I cant get the the attatchinterrupt to properly interrupt the scanning/driving.

#include <Servo.h> 

int SnorRechts = 6;
int SnorLinks = 5;
int SnorRechtsStaat = LOW;
int SnorLinksStaat = LOW;
int Ontvanger = 7;
int Echo = 8;
long duration, cm;
int LED = 13;

Servo Left;
Servo Right;
Servo Top;

void setup() {
  Serial.begin(9600);
  Top.attach(9);
  Left.attach(11);
  Right.attach(12);
  pinMode(SnorRechts, INPUT);
  pinMode(SnorLinks, INPUT);
  pinMode(Echo, OUTPUT);
  pinMode(Ontvanger, INPUT);
  pinMode(LED, OUTPUT);
  Top.write(12);
       }

void loop() {
  
  duration = pulseIn(Ontvanger, HIGH);
  cm = duration/58.4; 
    
 SnorRechtsStaat = digitalRead(SnorRechts);
  SnorLinksStaat = digitalRead(SnorLinks);

  Right.write(112);
  Left.write(108); 

  delay(50);
  EchoSendRecieve();

  if (cm >= 30 && cm != 0){ //east no wall
    Top.write(102);
    delay(400);
    EchoSendRecieve();
    delay (20);
    
    if (cm <= 30 && cm != 0){ //north wall
      Top.write(192);
      delay(400);
      EchoSendRecieve();
      
     if (cm <= 30 && cm != 0){ //west wall
        Left.write(18);
        Right.write(202);
        delay(1200);
        Right.write(102);
        delay(1080);
        Right.write(202);
        Top.write(12);
        delay(403);
        loop();}
      if (cm >= 30 && cm != 0){ //west no wall
        Left.write(18);
        Right.write(102);
        delay(1080);
        Right.write(202);
        Top.write(12);
        delay(403);
        loop(); ;} }
      
    if (cm >= 30 && cm != 0){ //north no wall
      Top.write(192);
      delay(400);
      EchoSendRecieve();
      
       if (cm <= 30 && cm != 0){ //west wall
        Left.write(18);
        Right.write(202);
        delay(1200);
        Right.write(102);
        delay(1080);
        Right.write(202);
        Top.write(12);
        delay(403);
        loop();}
       if (cm >= 30 && cm != 0){ //west no wall
        Right.write(202);
        Left.write(18);
        Top.write(12);
        delay(403);
        loop();} } }
        
 else {                          //east wall
    Top.write(102);
    delay(400);
    EchoSendRecieve();
    delay (20);
    
    if (cm >= 30 && cm != 0){ //north no wall
      Top.write(192);
      delay(400);
      EchoSendRecieve();
      
      if (cm <= 30 && cm != 0){ //west wall 
        Right.write(202);
        Left.write(18);
        Top.write(12);
        delay(403);
        loop();}                   
      if (cm >= 30 && cm != 0){ //west no wall
        Right.write(202);
        Left.write(18);
        delay(1200);
        Left.write(118);
        delay(1205);
        Left.write(18);
        Top.write(12);
        delay(403);
        loop();} } 
      
    
    if (cm <= 30 && cm != 0){ //north wall
      Top.write(192);
      delay(400);
      EchoSendRecieve();
      
     if (cm <= 30 && cm != 0){ //west wall
        Left.write(18);
        Right.write(102);
        delay(2600);
        Right.write(202);
        delay(50);
        Top.write(12);
        delay(403);
        loop();}
        
     if (cm >= 30 && cm != 0){ //west no wall
        Right.write(202);
        Left.write(18);
        delay(1200);
        Left.write(118);
        delay(1205);
        Left.write(18);
        Top.write(12);
        delay(403);
        loop();} } } 

    delay(100);
     
      
     
   /* if (cm <= 20){
   Left.write(198);
   Right.write(22);
   delay(1403);
   Serial.println(cm);
   delay(100);
        }
  if (cm >= 20){

  Right.write(202);
  Left.write(18); 
  delay(50);
  Serial.println(cm);
  delay(100);
  //digitalWrite(LED, LOW); */
 
  Right.write(112);
  Left.write(108);
  
     
  if(SnorRechtsStaat == HIGH) {
    delay(100);
    Left.write(198);
    Right.write(22);
    delay(750);
    Rechtsturn(); 
    Linksturn(); 
  
  
} 
 if(SnorLinksStaat == HIGH){
   delay(100);
   Left.write(198);
   Right.write(22);
   delay(750);
   Linksturn();
   Rechtsturn();
  
 }
}

 

  void Rechtsturn(){
  Left.write(18);
  Right.write(202);
  delay(750);
  Right.write(102);
  //delay(3650);
  //delay(6850);
  delay(1080.0);
  //Left.write(108);
  //Right.write(112);
  }
  void Linksturn(){
  Left.write(18);
  Right.write(202);
  delay(750);
  Left.write(118);
  delay(1205);
  }
  void achtje(){
  Right.write(202);
  Left.write(103);
  delay(7200);
  Left.write(18);
  delay(3000);
  Right.write(117);
  delay(10000);
  Right.write(202);
  delay(3000); }

  void EchoSendRecieve(){
    
  digitalWrite(Echo, LOW);
  delayMicroseconds(10);
  
  digitalWrite(Echo, HIGH);
  delayMicroseconds(200);
  
  digitalWrite(Echo, LOW); 
  duration = pulseIn(Ontvanger, HIGH);
  cm = duration/58.4; 
  Serial.println(cm);
  delayMicroseconds(10);
  delay(10);
    }

This is the project so far: https://www.youtube.com/watch?v=k9FWRDZVdM0

GerbenxB:
So It is a gigantic loop that takes 5 seconds or so.

Rewrite the program so the loop takes no more than a few millisecs.

Get rid of EVERY delay(). Use millis() to manage timing in a non-blocking way as illustrated in Several Things at a Time

...R

And - you do not want interrupts in this sort of application!