Global variables

Looking various sample firmare code i see that the structure is:

// Define zone
int..
long..
char..

// This routine runs only once upon reset
void setup() {
..
}

void loop() {
..
}

// other my function

void myfunc1() {
..
}

void myfunc2() {
..
}

Is it possible to create a global var in define zone, like String myID = Spark.deviceID(); and use is in myfunc using the myID name ?
Really i’ve tried to create it with this sample code:

// Define zone
String myID = NULL;

// This routine runs only once upon reset
void setup() {
Serial.begin(9600);
String myID = Spark.deviceID();
}

void loop() {
..
}

// other my function

void myfunc1() {
Serial.println(myID);
}

BUT myfunc1 does’nt print !

Maybe this a bad answer but i’m new here, sorry… :slight_smile:

@blondie63, you are close. The global definition String myID = NULL; is good, making myID available globally (ie everywhere). However, you then “redefine” myID in setup() with String myID = Spark.deviceID(); which creates a local variable named myID, only accessible within the setup() function. So in myfunc1() you are printing the global myID which is still NULL.

What you really wanted to do in setup() is myID = String(Spark.deviceID()); which assigns a String value to your globally declared myID. :smile:

1 Like

remove the String in setup(), its type is already defined, you are redefining it as local to that function

so it should be

void setup() {
Serial.begin(9600);
myID = Spark.deviceID();
}

1 Like

You can also just leave off the initializer:

String myID;

It will default to an empty string - which is the same as you get by assigning to NULL so just think of it as fewer characters to type!

1 Like

Many thanks to all
only one more question about variables, i’m using web ide, why “int”, “char” are blue colored and “String” stay white?

While int and char are primitives String is not.
String is a class which brings a bunch of manipulation and other methods along.

And besides this the WebIDE intellisense formating is not that intelligent anyhow :wink:

Thanks, i’ve tried Spark Dev Ide but i’ve found some problems, Web Ide is working better for me…

What problems do you have with spark-dev? Please let us know so we can help! (Ideally in a new thread. ;-))

ok, i’ve to try again to send you a better feedback… next week… :wink:

Is it possible to define true project-wide global variables in Particle? In (Microsoft Visual) C++, to make a truly global variable, you would define it like this in a header file:

global unsigned int ThisVar;

I ask because I’m having difficulty making project-wide access to a single memory space. If my project was all in one code file, it would work–but I’d also be one of the more confused programmers on the planet.
Basically, if I define a “global” variable in a header file (“main.h”, for example), I get compile errors if I include the header file in more than one source file. That is understandable–but without including a reference to the variable declaration, I can’t access the memory space, either.
Any ideas?

You might want to look up the extern keyword.

@ScruffR - Exactly what I was looking for. Thanks!
I found the following link searching the Particle Community for “extern”, and will reference it here just in case someone else has the same question: http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c, referenced from Extra code files

P.S. That shows how long ago I was programming in MS VC++…

1 Like