Pages: [1]   Go Down
Author Topic: commenting out #define causes error  (Read 3598 times)
0 Members and 1 Guest are viewing this topic.
Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 29
Posts: 1071
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have a project with a lot of large constant strings, so thought I'd offload them to EEPROM and only address the ones I need in limited scopes as I need them.

To do this I thought I'd have a "#define SETUPMODE" which could be commented out after the sketch had been run once and all those strings would be gone, replaced by addresses in EEPROM to save space.  Problem is when I comment out that line the compiler gives an error in the first line of setup(), 'Serial' was not defined in this scope.  I simplified the sketch to illustrate the point, and the below simplified version does the same.

With the SETUPMODE definition it runs, without, it doesn't compile.

Any ideas why this might be? 
Code:
// comment this line to put in RUN mode
#define SETUPMODE
// comment this line to remove USB debug printlns
#define DEBUG
// debugging functions controlled by the above definition
#ifdef DEBUG
  #define DEBUG_PRINT(x)     Serial.print (x)          // print text to USB
  #define DEBUG_PRINTDEC(x)     Serial.print (x, DEC)  // print value to USB
  #define DEBUG_PRINTLN(x)  Serial.println (x)         // print text with newline to USB
#else
  #define DEBUG_PRINT(x)
  #define DEBUG_PRINTDEC(x)
  #define DEBUG_PRINTLN(x)
#endif

/* this block only present if in Setup mode - once Setup has been run once,
all these values are safely stored in the Arduino's onboard EEPROM. */
#ifdef SETUPMODE
// declare things that only get used for setupmode
  int testvar = 5;
#endif

// global scop declarations common to RUN and SETUPMODE go here
  int testval = 5;
 
void setup() {
  Serial.begin(9600);  // for talking with Arduino IDE over USB
 
#ifdef SETUPMODE
    Serial.println("Setup mode enabled.");
#else
  Serial.println("Standard mode enabled.");
#endif
}

void loop() {
 
  #ifdef SETUPMODE
    Serial.println("Setup complete, comment out #define SETUPMODE in sketch and re-upload to Arduino.");
  #endif
  for(;;);   // do nothing, forever
 
}
I have a sneaking suspicion I've been staring at the reason all night so hoping fresh eyes will see!
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

<sigh>

It's something to do with the way the IDE preprocesses your code.

If you add this line to the very start, the error goes away:

Code:
char foo;
Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 29
Posts: 1071
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You sir, are a lifesaver !  Thanks so much.
<sigh>
Sorry to cause that reaction, especially at this time of year.  My google-fu skills failed me clearly if it's a common issue.
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's not your fault.
 
I was sighing because the IDE design causes some unexpected issues like this.
Logged

Offline Offline
Edison Member
*
Karma: 31
Posts: 1417
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That problem and its solution should be a sticky!

Pete
Logged

Where are the Nick Gammons of yesteryear?

0
Offline Offline
Edison Member
*
Karma: 17
Posts: 1407
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wanted to thank you for this cause I was going crazy about this error.
hope it is Ok to push it up, it might help other people.
Logged

* Si preguntas, pon el código de tu programa, hace mucho mas fácil ayudarte. Y me ahorro un mensaje pidiendo que lo hagas.
* Si consigues solucionar tu problema, dedica unos minutos a explicar en tu post como lo conseguiste para beneficio de todos.
* Cambia el 'Subject' de tu hilo y añade 'SOLUCIONADO' cuando hayas llegado a una solución al problema que planteaste.
* Utiliza un 'Subject' para tu hilo que explique de que va el hilo.
Si estas empezando:
* Comienza a usar Arduino
* Guías de iniciación a Arduino
* Ejemplos
* Referencia del Lenguaje
* Conceptos básicos
Guia de usuario de arduino
Tutoriales en Ingles
Si necesitas que alguien te escriba el código: http://www.freelancer.com/  o esta  http://www.guru.com/

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Coincidentally you just pushed it above a thread with exactly the same error: http://arduino.cc/forum/index.php/topic,124904.msg938861.html#msg938861

That problem and its solution should be a sticky!

Pete

Funny stuff.
Logged


Pages: [1]   Go Up
Jump to: