Incrementation bug? WTF? [BROKEN COMPILER - SOLVED]

Could somebody explain this to me. Maybe I'm just plain stupid and overlooked something, but this thing is driving me mad.

This doesn't work (nothing happens):

int hue;

void setup()  
{ 
  hue = 0;
  pinMode(9, OUTPUT);
} 

void loop()  
{ 
  analogWrite(9, hue);    
 
  hue = hue + 1;
  
  if(hue > 255) hue = 0;
  
  delay(30);                            
}

SURPRISE!!! This works:

int hue;

void setup()  
{ 
  hue = 0;
  pinMode(9, OUTPUT);
} 

void loop()  
{ 
  analogWrite(9, hue);    
 
  hue = hue + 10;
  
  if(hue > 255) hue = 0;
  
  delay(30);                            
}

And this also works:

int hue;
int dupa = 1;

void setup()  
{ 
  hue = 0;
  pinMode(9, OUTPUT);
} 

void loop()  
{ 
  analogWrite(9, hue);    
 
  hue = hue + dupa;
  
  if(hue > 255) hue = 0;
  
  delay(30);                            
}

This doesn't work (nothing happens)

How long did you wait?

Trust me, long enough. Even if I waited too briefly it still doesn't explain the ones (last one) that work.

Which OS, IDE and Arduino are you using?

Linux 32bit, Arduino IDE, original Arduino UNO.

Interesting. Compiler problem? Though personally, I think not. Why not use the increment operator?

Does this work...

int hue;
const int dupa = 1;

void setup()  
{ 
  hue = 0;
  pinMode(9, OUTPUT);
} 

void loop()  
{ 
  analogWrite(9, hue);    
 
  hue = hue + dupa;
  
  if(hue > 255) hue = 0;
  
  delay(30);                            
}

Yeah, this works like charm as I wrote...

No it doesn't, I'm sorry I didn't see the 'const'.

Maybe I should make hue volatile...

How to produce an .S assmebly file from arduino IDE?

Which linux distribution?

I think i just discovered a compiler bug in Arch linux, Ubuntu seems to be OK though... I'm wondering if there's a bug affecting some distributions and not others.

The bug would lead to a very similar problem to that which you describe. Please try uploading the Blink example, and tell me if it blinks correctly, or too fast, or not at all.

stimmer:
Which linux distribution?

I think i just discovered a compiler bug in Arch linux, Ubuntu seems to be OK though... I'm wondering if there's a bug affecting some distributions and not others.

The bug would lead to a very similar problem to that which you describe. Please try uploading the Blink example, and tell me if it blinks correctly, or too fast, or not at all.

It's ArchLinux. Blink doesn't work - the light is constantly on, not blinking. AVR-GCC is v 4.5.2-1

Read this thread:
http://arduino.cc/forum/index.php/topic,49900.0.html
Although that thread is about the Mega I think it is a similar bug. It affects programs with no initialised global variables, that's why your first example fails but the third one works. The thread also explains how to do a disassembly.

stimmer:
Read this thread:
http://arduino.cc/forum/index.php/topic,49900.0.html
Although that thread is about the Mega I think it is a similar bug. It affects programs with no initialised global variables, that's why your first example fails but the third one works. The thread also explains how to do a disassembly.

Even if I initialize hue (int hue=0;) it still doesn't work. I tried every combination.

Don't initialise to zero, that doesn't avoid the bug. Try initialising to 1.

@Stimmer:

You are right. Initializing to 1 does solve the problem (if you can call it solution;)).

Also inserting a dummy global variable solves this problem. Did you find out if this is Distro-specific or compiler-version-specific?

I don't know how specific it is, so far I've only seen people running Arch linux mention it. Someone on the other thread downgraded the compiler to 4.3.5 (?) on Arch Linux but still had the problem. It isn't affecting Ubuntu.

You might try avoiding the problem by applying good programming practice, and declare hue in the function where it's used (instead of using a global variable):

void loop()
{
   static int hue = 0;

   ...

-j

I just would like to know if it's likely that installing avr-gcc from sources will solve the problem?

@kg4wsv

Thanks, I usually apply good programming practices. I just got my arduino today and was experimenting. It's not like it's some kind of a production code. Besides in a one-file, 50 liner it really doesn't matter if you use global variables.

And working around compiler bugs like this is way more evil than bad programming practices.

I don't know if compiling from source would help, if you've got the time and the patience you could try it. I couldn't get the compiler to compile on Ubuntu and I just gave up.

It could be gcc version or configure switches. I adapted PKGBUILDs from tinyos and compiled latest vanilla binutils (2.21) and gcc (4.5.2) and still the same problem.

If you know anything about compiling gcc-avr tell me if you see something suspicious here:

GCC:

  ../configure --disable-libssp \
               --disable-nls \
               --enable-languages=c,c++ \
               --infodir=/usr/share/info \
               --libdir=/usr/lib \
               --libexecdir=/usr/lib \
               --mandir=/usr/share/man \
               --prefix=/usr \
               --target=avr \
               --with-gnu-as \
               --with-gnu-ld \
               --with-as=/usr/bin/avr-as \
               --with-ld=/usr/bin/avr-ld

binutils:

    ../configure --build=$(../config.guess) \
                 --disable-nls \
                 --enable-install-libbfd \
                 --includedir=/usr/$(../config.guess)/include \
                 --infodir=/usr/share/info \
                 --libdir=/usr/lib \
                 --mandir=/usr/share/man \
                 --prefix=/usr \
                 --target=avr \
                 --disable-werror

Just compiled gcc 4.2.3 and it doesn't support atmega328p. One more try... This time 4.4.5.

--- EDIT --- No luck with 4.4.5 either. I suppose there are some patches or configure switches that fix this issue...

And I almost forgot: Thank you all for help, it's very appreciated.

I'd suggest downgrading to version 4.3.5, and applying the set of patches that Debian currently uses. The patches are here (debian/bsdpatches directory):
http://ftp.de.debian.org/debian/pool/main/g/gcc-avr/gcc-avr_4.3.5-1.tar.gz
(this is just the patches, you'll need to download the GCC 4.3.5 source as well from gcc.gnu.org)

Binutils may also need Debian patches.
http://ftp.de.debian.org/debian/pool/main/b/binutils-avr/binutils-avr_2.20.1-1.tar.gz