Qhere and how do I declare a global variable?

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?

Jim

missing = and ;

int CurrentState = 0;

should do it

For the variable to be global it just needs to be before your setup function.

You an astronomer?

Hint:

int CurrentState 0 /*1 = open,  2 = close,  3 = moving open,  4 moving close,  5 = error,  6 = startup */

For this, you can do the following:

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

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:

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

and then try to use it as a parameter type:

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:

http://www.arduino.cc/playground/Code/Enum

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:

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
}
etc

What are the advantages?

Jim
PS I only have one hand so reduced typing is a BIG advantage!!

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.