Pages: [1] 2 3   Go Down
Author Topic: Incrementation bug? WTF? [BROKEN COMPILER - SOLVED]  (Read 8156 times)
0 Members and 1 Guest are viewing this topic.
Cracow, PL
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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):

Code:
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:

Code:
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:

Code:
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);                            
}
« Last Edit: February 03, 2011, 07:19:30 pm by dsh1 » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12861
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This doesn't work (nothing happens)

How long did you wait?
Logged

Cracow, PL
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Which OS, IDE and Arduino are you using?
Logged


Cracow, PL
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Linux 32bit, Arduino IDE, original Arduino UNO.
Logged

Makati, Philippines
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino Rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12861
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this work...

Code:
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);                            
}
Logged

Cracow, PL
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this work...

Code:
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?
« Last Edit: February 03, 2011, 04:37:13 pm by dsh1 » Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Cracow, PL
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: February 03, 2011, 04:42:53 pm by dsh1 » Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Cracow, PL
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Cracow, PL
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@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?
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1] 2 3   Go Up
Jump to: