Simple variable in loop causes trouble...

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.

Thanx for any help / clue.

Pierre

Whenever I suspect the compiler I do

avr-objdump foo.elf  -s -S >bar
gedit bar

This usually helps to clarify if the compiler is to blame or not.

Cheers, Udo

Just tried:

void setup ()
{
  Serial.begin (9600);  
}

void loop() {
    static int doit= 0;
     
    while (doit==0) {
        Serial.println ("Hello"); 
        doit = 1;
    }
}

Prints "Hello" just once.
Duemilenova, Windoze 7, Arduino 0017

[edit]Removed the initialisation, too. Still works, as expected[/edit]

Here's a small code that shows my problem, it prints hello1 hello2 hello1 hello2, etc.

I upgraded to 0018 , same problem :frowning: Why is this happening to me !!! >:( >:( >:( >:(

#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);

void setup() {
lcd.init();
}

void loop() {
int once = 0;

while (once==0) {
lcd.cursorTo(1, 0);
lcd.printIn("hello1");
delay(2000);
once = 1;
}
lcd.cursorTo(1, 0);
lcd.printIn("hello2");
delay(2000);
}

You may want to test again with one small change...

#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);

void setup() {
     lcd.init();
}

void loop() {
   [glow]static [/glow]int once = 0;
     
   while (once==0) {
           lcd.cursorTo(1, 0);
           lcd.printIn("hello1");
           delay(2000);
           once = 1;
     }
     lcd.cursorTo(1, 0);
     lcd.printIn("hello2");
     delay(2000);
}

Yes STATIC I know but in my "longuer" code that was still giving me trouble under 0018.

Ok I just tried with 0018, fresh install. It works fine, but it wasn't with 0017 :frowning:

I lost like 3-4 hours debuggin my own code and ending rolling back to "loop()" . Perhaps it was my 0017 installation that was wrong.

For me to register here and post a message its cuz my mind was really really going knuts :-?

Thanx everyone, at least I know I'm not crazy!!!

PB

belanger,

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.

Regards,

-Mike

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.

Pierre

belanger,

Ah, I understand now. Thanks.

-Mike