1

this is probably a beginners question (I did not go to school for this), but it is stalling me for two days now.

I am writing a sketch in Arduino to move a robot. I store my positions in an array like

int joint1[180];

I got it perfectly working and running if everything is in the robot.ino, but now I want to put this array in a home-made library stringer.h and stringer.cpp but acces it from the .ino to make it a little easier to understand for someone else.

the question redefined i readout a home made jcode from sd in the form of a string i put the values to a int whit the toint comand then i store this value in 7 arrays joint1[180] joint2[180] etc now i want to use the array in my main script robot.ino how do i acces the array whitin stringer.ccp or do i put the arrays in my .ino file and make stringer send a string to robot.ino and devide it there ..... this makes it realy hard for the other functions in stringer ????

test situation globals.h

//#include "Arduino.h"
//#include "StandardCplusplus.h"
//#include <vector>
#include "globals.h"



extern int Joint1[180];
extern int Joint2[180];
extern int Joint3[180];
extern int Joint4[180];
extern int Joint5[180];
extern int Joint6[180];
extern int Slomo[180];

globals.cpp

//#include "Arduino.h"
//#include "StandardCplusplus.h"
//#include <vector>
#include "globals.h"


int Joint1[180];
int Joint2[180];
int Joint3[180];
int Joint4[180];
int Joint5[180];
int Joint6[180];
int Slomo[180];

tester.ino

//#include "StandardCplusplus.h"
#include <globals.h>

int check = 100;
int temp = 0; 

void setup() {
for (int p = 0;p < check; p++){
Joint1[p] = p + 33;}
}

void loop() {
if (temp < check){Serial.println(Joint1[temp]);temp = temp + 1;}
}

the other way

globals.h

#include "Arduino.h"
#include "StandardCplusplus.h"
#include <vector>
#include "globals.h"



extern std::vector<int> Joint1;
extern std::vector<int> Joint2;
extern std::vector<int> Joint3;
extern std::vector<int> Joint4;
extern std::vector<int> Joint5;
extern std::vector<int> Joint6;
extern std::vector<int> Slomo;

globals.cpp

#include "Arduino.h"
#include "StandardCplusplus.h"
#include <vector>
#include "globals.h"


std::vector<int> Joint1(180);
std::vector<int> Joint2(180);
std::vector<int> Joint3(180);
std::vector<int> Joint4(180);
std::vector<int> Joint5(180);
std::vector<int> Joint6(180);
std::vector<int> Slomo(180);

i wil get a error: #include nested too deeply

Nicolini
  • 19
  • 4

1 Answers1

0

Don't use arrays. They're bad, especially since you're new to programming. Use an std::vector instead. Declare the vector in a header file, like globals.h:

#ifndef GLOBALS_H
#define GLOBALS_H
#include <vector>

extern std::vector<int> joint1;

#endif

Note the extern keyword. This means that this is only a declaration for joint1. Define the vector in a source file, like globals.cpp:

#include "globals.h"

std::vector<int> joint1(180);

When you compile, make sure to also compile globals.cpp with the rest of your sources.

In source files where you need access to joint1, make sure to:

#include "globals.h"

You can access elements in the vector the same way as you do with arrays:

joint1[index]

Although since you said you're new to programming, I'd recommend you use the at() function of vector instead:

something = joint1.at(index);
joint1.at(index) = something;
// etc.

I recommend it because it checks whether you're trying to access an element outside the vector. If for example you try:

joint.at(200)

the program with abort with an exception.

Now, with that being said, having global variables like this gets tedious and difficult to work with. It's better to not use global variables and instead define your functions to take the data you need in function arguments.

If you don't have access to the C++ standard library, and thus std::vector isn't available, then you can keep using arrays instead. extern declaration in globals.h:

extern int joint1[180];

and definition in the .cpp:

int joint1[180];

No more .at() or anything else though. Just plain old [index].

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • Tanks for so far i am going to set up a test whit this and try what you suggested maybe i wil come back to this – Nicolini Mar 26 '16 at 20:22
  • helo back again just tested the setup but my arduino ide doesn't reconize vector does not name a class i am probaly doing someting wrong i also got the mesage there is no such file vector ive been looking for the supporting files online seems that it is restricted to C++ and not to arduino (but i am probaly doing someting wrong) – Nicolini Mar 27 '16 at 12:38
  • @Nicolini Without the standard C++ library, it's going to be hard to do development on anything. Take a look at this [question](http://stackoverflow.com/questions/9986591/vectors-in-arduino). The answer links to a library that if you are able to use, it would make your life a lot easier. Note though that the more stuff you put in, the more memory it needs, and the arduino doesn't exactly have plenty of memory. – Nikos C. Mar 27 '16 at 16:17
  • i am going to check it right now .. the thing is o got everyting running whit display of je settings on a tft adjustmen funtions multiple storing posibilities everiting worked but now i want to try it the oficial way because nobody was understanding what they were reading – Nicolini Mar 27 '16 at 16:25
  • @Nicolini I also updated the answer with the array version, if you can't bother with the C++ library. – Nikos C. Mar 27 '16 at 16:25
  • i also redefined my problem and the layout of this post ps. already tanks – Nicolini Mar 27 '16 at 17:52
  • @Nicolini Your `extern` array should be at global scope, not inside a class. If you do it exactly as I suggested in the answer, meaning a `globals.h` and a `globals.cpp` file, it should work. Also, you should not use a leading `_` in your variable names. Names beginning with an underscore are reserved for the compiler (depending on whether the next letter is capitalized or not.) But instead of having to worry about that, it's best to just use a trailing underscore. In this case, instead of `_mState` you should name it `mState_` instead. – Nikos C. Mar 27 '16 at 18:02
  • Ok sorry i am going to do it like you said whit the global files in the morning i am still geting use to this way of writing the last software i kinda wrote was in basic when i was 10 22 years ago – Nicolini Mar 27 '16 at 19:07
  • back again testet what you said see code tested it both ways – Nicolini Mar 28 '16 at 11:56
  • @Nicolini You're including config.h in config.h... That results in an infinite include loop. Don't do that. A header file should not include itself. Also, put an include guard in .h files (like I have now done in the answer with `#ifndef` and `#endif`. See the [wikipedia article](https://en.wikipedia.org/wiki/Include_guard) on why you need this. – Nikos C. Mar 28 '16 at 19:44