Scoping Global Variables

I hope this is an easy question. I know about variable scoping (at least I thought I did)... but I seem to be running into some errors. Basically, I have an define statement at the top of my code which checks which board type you're using (using the pins_arduino.h header file) and then declares a set of variables accordingly. So, I have something like this (note: I've simplified the example):

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)         
  int READ_APIN_LEN = 6;
  int READ_DPIN_LEN = 3;
  int WRITE_LEN = 9; 
#endif

The statement above is located at the very top of my code (under the #include statement for the pins_arduino.h file) so those variables should be considered "global". However, when I use them later in my code, I get an error that says READ_APIN_LEN was not declared in this scope (the same applies to the other variables). Am I missing something?

#include <pins_arduino.h>

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)         
  int READ_APIN_LEN = 6;
  int READ_DPIN_LEN = 3;
  int WRITE_LEN = 9; 
#endif

void setup() {}
void loop() {}
Sketch uses 466 bytes (1%) of program storage space. Maximum is 32,256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2,039 bytes for local variables. Maximum is 2,048 bytes.

Compiles just fine.

andyopayne:
Am I missing something?

Yup. Code that illustrates the problem.

Am I missing something?

Yes. You are missing an understanding of how the IDE mangles your code for compilation.

Enable verbose mode for compiling (using File + Preferences). Look in the build directory for the cpp file created from your sketch. Notice what the IDE has added.

Generally, all that is needed to get the IDE to behave properly is the add a line like:

byte me;

as the FIRST line of code in your sketch. The IDE will then do it's diddling after that statement, before your #ifdef statements, etc.

Thanks guys for the quick responses. I guess I should have provided a more complete example So, let's try this:

byte me;

#include <pins_arduino.h>

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)         
  int READ_DPIN_LEN = 3;

#endif

void setup() {
  for(int i = 0; i < READ_DPIN_LEN; i++){
    //Do something
  }
}
void loop() {}

The example code that Coding Badly posted compiled just fine because he (or she) never actually tried to make a reference to one of those variables. As soon as you do, it throws the scoping error. The example above simply tries to use the READ_DPIN_LEN variable inside a For Loop in the setup function. I tries using the byte me; suggestion that PaulS made to the beginning of the sketch, but I still get the following error message:

sketch_oct15b.ino: In function 'void setup()':
sketch_oct15b.ino:11:22: error: 'READ_DPIN_LEN' was not declared in this scope
Error compiling.

This compiles fine for me under IDE 1.0.6 for a Uno:

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)         
  int READ_DPIN_LEN = 3;

#endif

void setup() {
  for(int i = 0; i < READ_DPIN_LEN; i++){
    //Do something
  }
}
void loop() {}

What board do you have selected?

Hmm... I actually am using IDE 1.5.8 because I've been trying to modify my code to work with the DUE board. So, do you think this has to do with the latest beta build? Anyone know if this will be fixed?

I got the same result as you, but since I have a mega2560 I changed your code to read

byte me;

#include <pins_arduino.h>

#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega168__)         
  int READ_DPIN_LEN = 3;

#endif

void setup() {
  for(int i = 0; i < READ_DPIN_LEN; i++){
    //Do something
  }
}
void loop()
{
  
}

Then it compiles fine. Perhaps you haven't got the correct board selected in the ide.

andyopayne:
Hmm... I actually am using IDE 1.5.8 because I've been trying to modify my code to work with the DUE board. So, do you think this has to do with the latest beta build? Anyone know if this will be fixed?

Yes, but what board do you have selected right now? The Due is not an ATmega328.

According to once popular movie “Short circuits” - computers don't think, they just run programs.
Statistically - operator error is more likely cause of this problem.
I would suggest this complex remedy:

#if defined(AVR_ATmega328P) || defined(AVR_ATmega168)
int READ_APIN_LEN = 6;
int READ_DPIN_LEN = 3;
int WRITE_LEN = 9;
#else
you newer know what you gonna get – to quote another movie while I am at it
#endif