Go Down

Topic: How to use variables that are declared in a library (Read 487 times) previous topic - next topic

jsheunis

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


PaulS

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: [Select]
int a = 14;
float b = 28.0;


In the sketch:
Code: [Select]
#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()
{
}

frank26080115

#3
Aug 07, 2012, 02:31 pm Last Edit: Aug 07, 2012, 02:36 pm by frank26080115 Reason: 1
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: [Select]

class HardwareSerial
{
 public:
   int theVariableYouWantToAccess;
 private:
   int someOtherStuffYouCannotAccess;
};

extern HardwareSerial Serial;


then you can do something like
Code: [Select]
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: [Select]

int theVariableYouWantToAccess;

void foo()
{
 theVariableYouWantToAccess++;
}


then your sketch can look like
Code: [Select]

extern int theVariableYouWantToAccess;

void setup()
{
}

void loop()
{
 theVariableYouWantToAccess = 0;
}


or you can have a header file that contains

Code: [Select]

#ifndef headerFile_h
#define headerFile_h

extern int theVariableYouWantToAccess;

#endif


and your sketch looks like
Code: [Select]

#include <headerFile.h>

void setup()
{
}

void loop()
{
 theVariableYouWantToAccess = 0;
}
Freelance engineer, consultant, contractor. Graduated from UW in 2013.


jsheunis

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

PaulS

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.

MichaelMeissner


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.

Go Up