I have a program where it's very important to exit a while loop program containing a pile of sub for loops immediately as a digital input goes low. I guess what that means is that I need it to NOT finish the execution of the remainder of whats in the While loop and immediately exit in real time response to an input going from high to low.
Right now I have a
while(pin xxxxxx == HIGH)
{
..... bunch of loops in the program
}
and it starts the code but it finishes out the entire while loop contents before it reads the pin again. I even call to read the pin at the first line of code in the while loop. But something's not quite working.
Could you post your current code? As often by rearranging the logic you don't need such a jump. maybe you need to abstract an inner loop into a function();
If it works in your code, then use goto. May be better than using bunches of breaks. The old hens club usually acts like the sky is falling when goto is mentioned, but that is probably because they have difficulty wrapping their head around code flow logic (computershave absolutely no problem with it).
If its one pin or two just use an interrupt and handle that pin in interruptions, as said in the avrfreaks if the while loop is long you need multiple checks along the code.
The old hens club usually acts like the sky is falling when goto is mentioned, but that is probably because they have difficulty wrapping their head around code flow logic (computershave absolutely no problem with it).
That's because the old hens club have read their K&R, and if the very authors of "The C Programming Language" disagree with the use of goto, that's a very strong warning, and one that should be heeded carefully.
The problem isn't knowing when to use goto, its in knowing when not to. Its very easy to abuse the power of goto; this is why we old hens argue that it should remain somewhat under wraps. The goto statement was included in BASIC, and the spaghetti code that was written was utterly unbelievable. Perhaps we don't want to see that happen to C/C++.
Honestly, if your code includes a ton of nested loops, its as likely as not that you are applying the wrong design pattern to the problem at hand; you likely would be better off with a state machine pattern in many cases. Regardless, though, any time your code seemingly becomes overly complex (like, when you feel a need to use goto, for instance), that's a strong indication that refactoring is needed. Take a step back (and maybe a break), and recheck your design.
/IMHO - but willing to listen to arguments otherwise...
Do I need to put the GOTO in every single loop within the program? seems like I included it in the main while loop (houses all the other loops) and it works but it finishes what it's doing in the loop first (not immediate).
Do I need to put the GOTO in every single loop within the program? seems like I included it in the main while loop (houses all the other loops) and it works but it finishes what it's doing in the loop first (not immediate).
Without posting your code, how do you expect anybody to have any idea what it needs?