Global Variables

Is there a way to define a global variable within a function or nearby and just outside the function? By global variable I mean a variable that is accessible throughout the entire sketch.

I have written several functions that I can potentially use in new sketches. I would like to include these functions with all their global variables at the bottom of my sketches, rather than putting their associated variables say at the beginning of the new sketch and the majority of the function at the end of the sketch.

I tried putting these function global variables just above and outside the function but I get "not in scope" errors. It appears that global variables are best declared at the top of the sketch.

P.S. I think that I can make these functions into libraries and add them to my sketches via #include statements, but I'll reserve that idea for when I have more time to learn how to do it (I know that is supposed to be easy).

You can define your variables in an include or at the start of the sketch

extern char laterDefined[];

and define the variables in global skope somewhere.

char laterDefined = "I'm here";

frank2644:
.S. I think that I can make these functions into libraries and add them to my sketches via #include statements, but I'll reserve that idea for when I have more time to learn how to do it (I know that is supposed to be easy).

you can try also putting these variables/functions into an .ino file in the same directory, which will share global scope without needing the extern reference.

Libraries are easy. You can write all your code in the .h file. You only need a .cpp file if the code is long and needs to be better organized.

One or two functions are a great way to get started with a library.

Then, when you're comfortable with that, creating some classes will really help to organize your variables so that the main sketch can access them without accidentally damaging them.

MorganS:
You can write all your code in the .h file. You only need a .cpp file if the code is long and needs to be better organized.

I disagree.

You should use a cpp file as soon as objects are defined and not only declared.

If all data and functions are part of a class you may get away with only a hpp file,
but not if you have static data members.

Thank you for all the suggestions. Let’s see if I understand.

  1. I can make a .cpp file and put it into my library.
  2. That .cpp file only needs to contain my function code plus, of course, any variables.
  3. The .cpp file does not need a loop section or setup section.
  4. My new sketch only needs a #include <myfunction.cpp> entry
  5. The new sketch can now use any variables in the myfunction.cpp file, as well as use the function itself.

Do I have it right?

I would do exactly that, except call it a .h file.

and define the variables in global skope somewhere.

Be good to define it properly, though. 8)

char laterDefined[] = "I'm here";

Yep, that would be better. :smiley:

When I try and compile a sketch that includes the ".h" file above I get "no such file or directory".

When I open the IDE Sketch tab and select "Include Library", my ".h" file is there.

When use file explorer i see a folder in the Library named the same as my ".h" file, with that folder having an ".ino" file. IOW, it looks like most other library entries.

Any ideas on how I can proceed?

P.S. I'm not sure it matters, but my ".h" file does not compile, giving errors because there is no loop or setup sections. So I just copied the uncompiled file to my Library.

you want to put your helper file into a header that is in the same directory
Screen Shot 2017-05-18 at 13.37.02.png

and reference it with quotes in the #include:
Screen Shot 2017-05-18 at 13.36.39.png

No, that won't make the library available to multiple sketches, only the one that it's in.

It's a good way to experiment with the library and get it working on one sketch but it must eventually move to the libraries folder with other user-installed libraries.

Thanks everyone again for the help.

Unfortunately, when I put the ".h" file into the same directory as the sketch that will use it, i still get the IDE error "no such file or directory". I also used quotes for the #include statement.

Here is a basic recap of what I did:

  1. I made a ".h" file, that contained some functions and some global variables, and called it mystuff.h.

  2. I simply copied that file into the Arduino library using file explorer..

  3. Somehow that file became a folder named mystuff.h. Not sure how a file became a folder, but I assume it's a normal thing.

  4. Magically that folder contained a file called mystuff.ino. Not sure how that happened, again I assume that's a normal thing.

Anyway, when I try and #include "mystuff.h" I get the "no such file...." error.

Maybe this is a clue: Instead of me putting the include statement into my sketch, when I attempt to do that via the Arduino Sketch tab (by selecting "Include Library") I see mystuff.h. (sounds good so far). However, when I click on "mystuff.h" nothing happens, although it seems to add a blank line to my sketch. If I click on another item in that same list I don't get a blank line, instead I get a #include statement for that item added to my sketch. Obviously, something is wrong with my file resulting in a blank line.

Any help is appreciated..

  1. Good.

  2. What? A library such as MyStuff.h should be in Documents\Arduino\libraries\Mystuff\

  3. It means you tried to open it in the Arduino IDE. It expects .ino files inside folders of the same name. To edit the file you need a different editor. I like Edit Plus but others like Notepad Plus.

  4. You definitely did something wrong with the Arduino editor.

The menu command "include library" is just a cheap way to type out what you should have typed out anyway. It doesn't do anything magical except putting that #include statement at the top of your .ino file.

Thanks for hanging in with me.

I started over...

I created a sketch in my IDE as in the prior BulldogLowell post called myhelpers (thanks, Bulldog). I then used the IDE “save as” facility to save “myhelpers” in the \Documents\Arduino directory.

Looking in that directory using Windows File Explorer I see a folder called “myhelpers” that contains a file called “myhelpers.ino”.

I then closed and reopened the IDE and it only contained the usual skeleton file, basically empty with just setup() and loop() sections.

I typed #include “myhelpers” in line 1. When I click the IDE verify/compile menu I get the error “no such file or directory”.

I also tried to include "myhelpers" into several of my other sketches and get the same error.

Any suggestions?

P.S. I am using IDE 1.6.5

Any suggestions?

Start a new Arduino sketch and save it. DO NOT call it myhelpers unless you are determined to confuse yourself. In the IDE add a new tab and name it myhelpers.h. Put whatever variable declarations you like in the new tab. In the first tab add #include "myhelpers.h" at the top of the file. The quotation marks are important. Save the file.

Close the file and reopen it in the IDE. There should be 2 tabs. The myhelpers.h file will be in the same folder as the .ino file

You can save the myhelpers.h file elsewhere so that it is available to all programs but get this version working first.

A quick and dirty try of your tab idea seems to work great. I took an existing sketch and moved some variables to a newtab, did the include statement and the sketch compiled perfectly.

I also want to move some functions, but the wife has other ideas for my time...might be a day or two before I get back to this.

But, by jove, I think you got me going.

Thank you.

Note that you don’t have to add the tab using the IDE. If you use the OS to copy the.h file into the same folder as the .ino file then the IDE will automatically open it when you open the .ino file.

Now try this. Create a folder in the sketch directory libraries folder named myhelpers and copy the myhelpers.h file into it. Delete the myhelpers.h file from the folder with the .ino in it and change the #include to
#include <myhelpers.h>and save the .ino
Stop and restart the IDE, load your program and run it.

Now, whenever you want to use the variables declared in myhelpers.h you can just include it in any program.

Thanks, again everyone for the help and tips.

I am adding a function to the myhelpers.h tab. That function calls another function in the primary tab. I am getting "function not declared in this scope".

Any ideas?

frank2644:
Thanks, again everyone for the help and tips.

That function calls another function in the primary tab.

the reason you went this way was to create a group of helper functions that you used in your main sketch.,

You now have a reverse dependency, what is the benefit of that?

if you ‘export’ a group of functions into a library, you ought not have any other dependancies unless they are #included in that library… agreed?