Pages: [1]   Go Down
Author Topic: How to use variables that are declared in a library  (Read 433 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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.

Thank you!

Stephan
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

*be
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Quote
I went through the Arduino Library tutorial
Might as well have been studying Mandarin Chinese for all the relevancy.

Quote
Any help or examples would by much appreciated.
Create a tab, calling the file stuff.h. In the file, put:
Code:
int a = 14;
float b = 28.0;

In the sketch:
Code:
#include "stuff.h"

void setup()
{
   Serial.begin(115200);
   Serial.print("a = ");
   Serial.println(a);

   Serial.print("b = ");
   Serial.println(b);

   Serial.print("b/a = ");
   Serial.println(b/a);
}

void loop()
{
}
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 594
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it's declared in a class only if the library uses a class. if it is declared in a class, it must be public for you to access it. For example

If this is in the library's header file
Code:
class HardwareSerial
{
  public:
    int theVariableYouWantToAccess;
  private:
    int someOtherStuffYouCannotAccess;
};

extern HardwareSerial Serial;

then you can do something like
Code:
Serial.theVariableYouWantToAccess = 1;
inside your own sketch

if it's not inside a class, then you should be able to access it by using "extern" inside your sketch. For example

if your library has a .c or .cpp file that looks like
Code:
int theVariableYouWantToAccess;

void foo()
{
  theVariableYouWantToAccess++;
}

then your sketch can look like
Code:
extern int theVariableYouWantToAccess;

void setup()
{
}

void loop()
{
  theVariableYouWantToAccess = 0;
}

or you can have a header file that contains

Code:
#ifndef headerFile_h
#define headerFile_h

extern int theVariableYouWantToAccess;

#endif

and your sketch looks like
Code:
#include <headerFile.h>

void setup()
{
}

void loop()
{
  theVariableYouWantToAccess = 0;
}
« Last Edit: August 07, 2012, 07:36:19 am by frank26080115 » Logged

Freelance engineer, consultant, contractor. Graduated from UW in 2013.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, PaulS. Great help!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is the same thing possible with functions? or do i have to create a class for functions in a library?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is the same thing possible with functions?
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.

Quote
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.
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1834
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: