Why the code here below doesn't work? It appears that "doit" does not get assigned the value of 0 in the beginning so the code doesn't run on the first run.
void loop() {
static int doit= 0;
while (doit==0) {
// run some code...
doit = 1;
}
...
}
BUT if I declare "doit" as a global variable, then it works fine :
int doit= 0;
void loop() {
while (doit==0) {
// run some code...
doit = 1;
}
...
}
I really don't get it. And BTW if I do the following it works :
void loop() {
int doit= 0;
while (doit) {
// run some code... THE CODE WILL RUN
doit = 0;
}
}
It's not the first time I program, it's been years (C under Unix) but the above makes no sense to me. Could it be a bug with the compiler?! I'm under Windows 7 64 bits with arduino version 0017.
Now I'm confused. The way I read the last example you gave, it SHOULD print hello1, pause 2 seconds, print hello2, pause 2 seconds, and repeat forever.
The while loop only executes once, printing "hello1", then the code after the while loop executes and prints "hello2". Then the whole thing repeats because it's in 'loop()' which is called over and over again, forever, from the hidden main routine that built into Arduino programs.
Your original problem was that the while loop was never executing. This seems like a different situation. Was the last example you gave supposed to do something different?
Please understand that I'm not criticizing in any way, I'm just trying to understand.
Mike, the problem (from the last example) if different -- that last one is after I upgraded to 0018. On this last one, I forgot to declare the variable has "static". The original problem is gone in 0018. Was it my IDE installation that was "defective" (I doubt it) but I went back to my 0017 and got the same bug again. I didn't re-download 0017 to verify if it was my installation or not.