The if statement dose not work after the recursion

Hello, My problem is simply that

If I put if statement to start the program when the switch which connected to the pin 6 is pressed, so I run the program and it is worked fine at the first time, but when I use the recursion to restart the program when some condition is true , suddenly the program activates the if statement even when the result of the logic expression is ZERO !!! and it is not just activated the IF statement, in my example it pass a print command also !

my code here is not my real program that I want to make but I tried to make it simple for represent the problem I had.

my code:

#include <Streaming.h> // some library for printing out like c++

const long BAUD = 9600;
const int DELAY = 200 ;

void switchPress(int SwitchNum);

void setup() {
  pinMode(6, INPUT);  // Switch G-LED
  Serial.begin(BAUD);
    while (!Serial){
  // wait for serial port to connect.
  }
}

void loop() {
  Serial << (digitalRead(6) == HIGH) << endl; // to Check out the result of the logic expression
                                              // before go to the if statement
  if(digitalRead(6) == HIGH){ // my problem in this IF
    while(digitalRead(6) == HIGH){
    continue;
    }
    Serial << "the program pass the if statement .... " << endl; // the program pass this line
                                                                 // when start again using recursion
                                                                 // but at the first time it dose not !!!
                                                                 // the same problem with the IF 
    while(true){
      delay(DELAY);
      if(digitalRead(6) == HIGH){
        switchPress(6);
      }
    }
  }
}

void switchPress(int SwitchNum){
  while(digitalRead(SwitchNum) == HIGH){
    continue; // just to make sure the problem not from the switch and it is LOW !!!
  }
  if(digitalRead(SwitchNum) == LOW){ // to check again it is LOW
      Serial << "THE PROGRAM GOING TO START AGAIN" << endl;
      loop();
  }
}

and this is my OUTPUT:

as you can see the program is stuck because I did not press the switch yet

when I pressed it the program activated the if statement and printed that "the program pass ... " !!

then I pressed it again and the program print that it will restart and will enter the recursion

when it restarted the program printed that the logic expression is zero one time and stopped !! unlike the first time so the program activate the if statement when the logic expression is zero after the recursion.

You should not call loop() from your switchPress function because you will run out of memory that way. Instead, just return.

Why on Earth are you calling loop() from inside the loop() function ? That does not restart the sketch, only the loop() function

it is not run out of memory, because it is at the second time only the problem appeared.

and in my real program, actually I called another function not loop() but the same problem is appeared.

in my real program, actually I called another function not loop() but the same problem is appeared, my goal not to resketch just to recall function to work again...

for example in a game that you want if someone lose restart the function that start a game.

You do not see the "program pass" message because the pin is low when loop() is called. You do see the other message because of the infinite while loop inside of your main loop() function.

Again, I would not use recursion unless you have a good reason to do so.

if this is the problem then why the program print zero

and this is outside the

infinite loop that you mention.

the program will not print ZERO again if it is stuck inside the infinite loop
and after the program print zero, the program activated the if statement even when the result of the logic expression is zero , to enter the infinite loop again.

Yes it will because within the infinite loop, switchPress is called, which calls loop whenever the pin is low.

your switchPress() function is CALLING loop(). When loop() reaches its end, WHERE do you suppose it is going to return to? Hint: it will NOT return it main, as it should/needs to.

NEVER explicitly call loop()!! Nothing good will happen if you do.

the loop actually inside the if statement not outside ... as you can see in the output 0,0,0,0
because it can't pass IF to go to the loop.

and my code you can implemented by just connecting switch to pin 6, if you want to check what I have did manually.

I said in my question that my real program not like this without any purpose , but I want to learn why is this happened, is it a bug? is it a mistake that I did? to learn and then improve.

but if you interested in my goal from recursion I use it to call function called starter() that start a game again when the user lose, but I recode to make the problem clear and simple and then debugging it.

Do you know what recursion means in the context of programming ?

Recursion is fine, IF you understand the execution path once you start the recursion. Clearly, in this case, you do not. Think about my question. Once you explicitly call loop(), what is loop() going to do. Once loop() reaches the end, WHERE is it going to return to?

It is NOT where you seem to think, and that is the root of your problem.

yes, it recall the function until the point you recalled.

Let's try this a different way...

  1. main() calls loop()
  2. loop calls switchPress()
  3. switchPress() calls loop()
  4. ?
  5. ?
  6. ?

You tell me what steps 4, 5, and 6 in the above sequence are.

Have fun.

  1. main() calls loop()

// the program dose not go to the next step until the user press the switch
// in my code the program will keep printing zero until I press the switch

  1. loop calls switchPress()

  2. switchPress() calls loop()

  3. like recursion go to step number one

but the problem is here what I think is that and what I hope to now where is my mistake

the program really go to the first of loop()
and print zero
but it dose not keep printing it as the first time it suddenly activate

even if the expression is FALSE
as we can see in the output

image

my real program is like this not infinite loop actually

      while(j < vec.size()){
        if(digitalRead(6) == HIGH){
          switchPress(6, j);
        } 
        else if(digitalRead(7) == HIGH){
          switchPress(7, j);
        }
        else if(digitalRead(8) == HIGH){
          switchPress(8, j);
        } 
        else if(digitalRead(9) == HIGH){
          switchPress(9, j);
        }
      }

inside switchPress() I have

void switchPress(int SwitchNum, int &j){
  while(digitalRead(SwitchNum) == HIGH){
    continue;
  }
  if(vec[j] == SwitchNum - 4){
    delay(DELAY);
    j++;
  } 
  else {
    delay(300);
    if(digitalRead(SwitchNum) == LOW){
      starter();
    }
  }
}

but the same problem is there.

I just tried to make it simple for you.

And steps 5 & 6?? AFTER it reaches the end of loop(), where does loop() return to?

WITHOUT any recursion, do you understand what happens when execution reaches the end of loop()?