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);
}
@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.
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?