Im quite new to the Arduino forum, so sorry if I'm making mistakes or asking stupid questions. I have a problem that I need some help with. I have a large Arduino program where many variables are declared before the setup() and loop() functions are executed. What I want to know is if it is possible to put all those variable declarations in separate library files, and then just to include those library.h files at the top of my main program. I want to be able to use the variables as they if they were declared in the main program file; in other words just the variable name, without having to declare an instance of the library. I went through the Arduino Library tutorial, but I couldn't determine how to do what I want to from that. Any help or examples would by much appreciated.
What I want to know is if it is possible to put all those variable declarations in separate library files, and then just to include those library.h files at the top of my main program.
First, a bit of terminology. A library is a header file and a source file that usually define and implement a class.
You don't want a class, nor do you need a source file. You DO need a header file. You define your variables in the header file, then #include the file in the sketch.
without having to declare an instance of the library.
You don't declare an instance of a library. You declare an instance of the class defined in a library. Not the same thing at all.
I went through the Arduino Library tutorial
Might as well have been studying Mandarin Chinese for all the relevancy.
Any help or examples would by much appreciated.
Create a tab, calling the file stuff.h. In the file, put:
Yes. Except that the header file only contains the function prototype. A related source file contains the implementation. Include the header file, and the code in the source file will be compiled and linked in, too.
or do i have to create a class for functions in a library?
Not unless the concept of a class makes sense. Look at string.h, for instance. It defines a collection of functions related to string processing, but no class. All the string functions are available to you.
PaulS:
Not unless the concept of a class makes sense. Look at string.h, for instance. It defines a collection of functions related to string processing, but no class. All the string functions are available to you.
Well in the case of string.h, that comes from the C language which does not have classes. C++ is mostly upwards compatible with C, so you have the C way of doing things (single functions that take arguments) and the C++ way (classes and eventually templates if it makes sense). You can choose which path to take.