Go Down

Topic: Qhere and how do I declare a global variable? (Read 12318 times) previous topic - next topic


I'm an old VB4 programmer, don't know beans about C. I got a Duemilanove to control the roof on my observatory.  I'm trying to declare a variable called CurrentState (before "void setup") and getting an error message:

error: expected initializer before numeric constant

code is:

#define OpenedLed 0 //Roof fully closed led green
#define ClosedLed 1 //Roof fully closed led green
#define OpeningLed 2 //Roof is opening led blinking red
#define ClosingLed 3 //Roof is closing led blinking red
#define FullyOpen 4 // Fully open switch fully open = switch closed
#define FullyClosed 5 //Fully closed switch fully closed = switch closed
#define CloseCommand 6 //Close roof command switch
#define OpenCommand 7 //Open roof command switch
#define OpenButton 8 //Open pushbutton
#define CloseButton 9 //Close pushbutton
int CurrentState 0 /*1 = open,  2 = close,  3 = moving open,  4 moving close,  5 = error,  6 = startup */

If I comment out the "int CurrentState 0" it gives no error. I want CurrentState to be a global variable. Where should this variable be defined and how?

8000ft above the average


missing = and ;

Code: [Select]
int CurrentState = 0;

should do it

Mike Mc

Jun 18, 2009, 11:37 pm Last Edit: Jun 18, 2009, 11:38 pm by zooto68 Reason: 1
For the variable to be global it just needs to be before your setup function.

You an astronomer?


Code: [Select]
int CurrentState 0 /*1 = open,  2 = close,  3 = moving open,  4 moving close,  5 = error,  6 = startup */

For this, you can do the following:
Code: [Select]

enum roofState {roofUndefined, roofOpen, roofClosed, roofOpening, roofClosing, roofError, roofStartup};
enum roofState CurrentState = roofUndefined;

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Jun 21, 2009, 06:49 pm Last Edit: Jun 21, 2009, 06:50 pm by DaveK Reason: 1
I like the option of using an enum too, however it seems that there are some quirks in Arduino when using enums.

if you add an enum:
Code: [Select]

enum roofState {roofUndefined, roofOpen, roofClosed, roofOpening, roofClosing, roofError, roofStartup};

and then try to use it as a parameter type:
Code: [Select]

void showRoofState( roofState currentState )

you will get an error when you try to compile:

"error: variable or field 'showRoofState' declared void"

Of course this makes no sense at all unless you already know that enums don't work as you'd expect them to, or if you are very familiar with how the parser works.

To fix this you must declare the enum in a seperate .h file as described here:


Fortunately this is trivially easy to do, I just wish it were more apparent that it was necessary, I wasted most of an hour last night trying to figure it out.

Also, incidentally, in C++ it isn't necessary to include 'enum' when you use the type, you can specify it like any other type:

Code: [Select]

roofState CurrentState = roofUndefined;


I'm afraid, at my level, this is beyond me! All I see here is more to type, ie.

enum roofState {roofUndefined, roofOpen, roofClosed, roofOpening, roofClosing, roofError, roofStartup};

void showRoofState( roofState currentState )

instead of

int CurrentState = 0;

 switch (CurrentState)
  case 1

What are the advantages?

PS I only have one hand so reduced typing is a BIG advantage!!
8000ft above the average

Robert Lee

In a small simple program, there's not a great advantage (outside of adhering to accepted best practices).

The real advantage comes in when you start getting lots of different numeric identifiers, especially when they are spread through many different source files.

By using a named enumerator you don't have to go back and look up that 1 = open. This is particularly helpful in IDE's that have Intellisense or other code preview routines that actually shows you the choices that you have.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131