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();
}
}
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.
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.
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.
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.
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.