Pages: [1]   Go Down
Author Topic: Qhere and how do I declare a global variable?  (Read 3769 times)
0 Members and 1 Guest are viewing this topic.
Buena Vista, CO
Offline Offline
Full Member
***
Karma: 0
Posts: 181
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

8000ft above the average

Austria
Offline Offline
Sr. Member
****
Karma: 3
Posts: 260
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

missing = and ;


Code:
int CurrentState = 0;

should do it
Logged


London, England
Offline Offline
Edison Member
*
Karma: 4
Posts: 1026
Go! Go! Arduinoooo !!!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

You an astronomer?
« Last Edit: June 18, 2009, 04:38:29 pm by zooto68 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hint:
Code:
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:
enum roofState {roofUndefined, roofOpen, roofClosed, roofOpening, roofClosing, roofError, roofStartup};
enum roofState CurrentState = roofUndefined;
Logged

"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.

Omaha
Offline Offline
Full Member
***
Karma: 0
Posts: 187
AC0KG
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
enum roofState {roofUndefined, roofOpen, roofClosed, roofOpening, roofClosing, roofError, roofStartup};

and then try to use it as a parameter type:
Code:
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:

Code:
roofState CurrentState = roofUndefined;
« Last Edit: June 21, 2009, 11:50:27 am by DaveK » Logged


Buena Vista, CO
Offline Offline
Full Member
***
Karma: 0
Posts: 181
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!
Logged

8000ft above the average

Palmetto, FL USA
Offline Offline
Full Member
***
Karma: 0
Posts: 138
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Pages: [1]   Go Up
Jump to: