Interrupting program loop

Hello,

I have an if loop and I was wondering how to stop it mid-cycle. It will constantly do the program and never go for the input == high. I was wondering if there is a way to interrupt Program(); when input == HIGH

If input == HIGH {
//do weird stuff//
}

Program();

void Program();
If xx = HIGH {
//do stuff//
Program();
}

else
//do different stuff//
Program();

EDIT: The program is not the recursion loop, I can do the code without it, the main problem is that while it is executing a program, it does not check anymore for the input

void Program();
If xx = HIGH {
//do stuff//
Program();
}

Recursion is generally a bad idea, unless you know what you are doing.

It does not look like you want to interrupt anything. You simply want to return from a function if some condition is true, and continue in the function otherwise.

PaulS:

void Program();

If xx = HIGH {
//do stuff//
Program();
}



Recursion is generally a bad idea, unless you know what you are doing.

It does not look like you want to interrupt anything. You simply want to return from a function if some condition is true, and continue in the function otherwise.

That is correct, but it needs to check all the time in the program.

#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);
    }

Video
This is what the code does One addition that I have to make is that It needs to stop the driving immidiatly if the whiskers at the front are being pressed and then execute a program that repositions the car and then starts void loop() again. Any tips?
(2 modified servo's Left/Right 1 unmodified servo on TOP and whiskers are just mechanical switches, the ultra sonar thing on top is hc sr04 )

An IF is not a loop - it is a test.

Have a look at how the demo Several Things at a Time works

...R

Well it's a loop in the sense that it will go back and forth,

Start program
Finish program --> Start program
Finish program --> Start program

        loop();} } }

Didn't I say recursion was a bad idea?

GerbenxB:
Well it's a loop in the sense that it will go back and forth,

If you don't like the answers then don't ask the questions. IF is not a loop. WHILE and FOR are loops.

...R

Look at "break" and "continue"