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
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]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.