Wierd bug - of which kind?

Hey guys!

I would say that I am an experienced programmer, not an experienced microcontroller programmer though. I got a wierd bug in a simple program that I cannot quite figure out. I hope that you guys can help me understand it. First of all the following code snippet (taken straight from Arduino's delay-function reference page - with a little adaption):

int ledPin = 13;                 // LED connected to digital pin 13
// #define ledPin 13  // If using this line instead of the one above, problems occur

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}

I got an Arduino UNO lying in front of me and an Arduino Mega 2560. When I upload that program in its original configuration I got my flashing test led on pin 13 - not problem on both boards. However, if I use the now uncommented #define-line instead of the int-variable for ledPin, I get no flashing Led what-so-ever on the Arduino UNO board. On the Mega 2560 board everything works just fine!

I know that both boards have different specifications/capabilities but the only thing I can think of is a memory problem (too little memory for only three times 13? I don't assume that this is the problem...) or a bug in the runtime libraries.

I also made a test setup to test different pins (currently only tested pin 1) and I get the same results. Wierd enough the problem does not occur if I am using ledPin as variable anywhere in pinMode or digitalWrite at least once:

// The following code works:
int ledPin = 13;
[...]
  pinMode(13, OUTPUT); // <-- here
[...]
  digitalWrite(13, HIGH); // <-- here
  delay(1000);                  
  digitalWrite(13, LOW); // <-- or here, but once is enough
  delay(1000);              
[...]

This is so wierd that I simply do not know what the reason for this problem is. Does anyone know what the problem is?

S**t, forgot to change my placeholder title to something meaningful

Well I just tried it on my Uno and it works for either line defining that pin for me.

Thanks for trying!

Perhaps as little extra information: I was testing on an UNO rev 2 - do you have a different rev number perhaps?

This is so wierd that I simply do not know what the reason for this problem is. Does anyone know what the problem is?

Grumpy_Mike:
Well I just tried it on my Uno and it works for either line defining that pin for me.

This suggests to me that perhaps the two of you are not using the same version of the IDE on the same OS.

So, which are you two using?

Just got the same idea... Fireing up my Ubuntu machine now.

I programmed my Arduino using Arch Linux with the current packages that were availabe using pacman. Now I will try it under Ubuntu 10.11. will post my results in here.

It was one of the first Unos before there was any rev number so I guess it is a rev 1. I am on a Mac.

Wierd enough the problem does not occur if I am using ledPin as variable anywhere in pinMode or digitalWrite at least once:

This sounds suspiciously like that problem with the boot loader that came up the other week where it would not initialise a variable with -1

Might be time to break out Nick Gammon's char foo; solution
declare a useless variable at the very top of the program. Sometimes the IDE mangles translating your sketch into a compilable program.

It was one of the first Unos before there was any rev number so I guess it is a rev 1. I am on a Mac.

I think the IDE had version numbers by the time the UNO was released...

Okay guys... I tested everything a bit more with help of my Ubuntu machine. My suspicion it is a compiler-problem:

Programs created and uploaded by my Ubuntu System work fine, but the ones from my Arch Linux installation do not. The compiler versions are:

Arch Linux (32bit):

avr-c++ (GCC) 4.6.2
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Ubuntu (64bit):

avr-g++ (GCC) 4.5.3
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The binary file sizes differ and well... I think I should try to upload the binary of one machine with the other just to be sure that it are the binaries. I used for my tests the same versions of the IDE on both machines - downloaded them both within the last 3 hours.