( error )
arraytest.ino:11:34: error: cannot convert '' to 'float*' in assignment
array = {X_Axis, Y_Axis, Z_Axis};
^
arraytest.ino:12:10: error: return-statement with a value, in function returning 'void' [-fpermissive]
return xyzarray;
^~~~~~~~
arraytest.ino: In function 'void loop()':
arraytest.ino:18:31: error: variable or field 'adxl345measure' declared void
void adxl345measure(xyzarray);
^
arraytest.ino:18:23: error: 'xyzarray' was not declared in this scope
void adxl345measure(xyzarray);
^~~~~~~~
exit status 1
Compilation error: cannot convert '' to 'float*' in assignment
Hi
Array is not a type of data, it is a set of several values. You can't assign arrays from the list other than at initialization time. And you can't return array from the function as return value.
xyzarray only exists inside the function adxl345measure, so you can't pass it in.
What are you trying to do? Return an array from a function? You can only do this "by reference" - you pass it in as a parameter, and the function will update it directly.
pass a pointer to the array or a reference to a structure for the data to your function and let the function fills in that memory
in the example from @red_car you pass the array information (pointer to the start of the array , data type and array size) or you could use a struct and a reference
// the type where we will be collecting information
struct t_data {
float samples[3];
uint32_t samplingTime;
};
t_data measure;
void adxl345measure(t_data & theData) { // the & denotes that you pass the parameter by reference
theData.samples[0] = 1.1;
theData.samples[1] = 2.2;
theData.samples[2] = 3.3;
theData.samplingTime = millis();
}
void setup() {
Serial.begin(115200);
adxl345measure(measure);
Serial.println(measure.samplingTime);
Serial.println(measure.samples[0]);
Serial.println(measure.samples[1]);
Serial.println(measure.samples[2]);
}
void loop() {}
the structure gives you freedom to organise the data as you feel is more relevant, you could do for example
// the type where we will be collecting information
struct t_data {
float x;
float y;
float z;
uint32_t samplingTime;
};
t_data measure;
void adxl345measure(t_data & theData) { // the & denotes that you pass the parameter by reference
theData.x = 1.1;
theData.y = 2.2;
theData.z = 3.3;
theData.samplingTime = millis();
}
void setup() {
Serial.begin(115200);
adxl345measure(measure);
Serial.println(measure.samplingTime);
Serial.println(measure.x);
Serial.println(measure.y);
Serial.println(measure.z);
}
void loop() {}
and once you are there you are not far from OOP
// the type where we will be collecting information
class Adxl345measure {
public:
float x;
float y;
float z;
uint32_t samplingTime;
void acquire() {
x = 1.1;
y = 2.2;
z = 3.3;
samplingTime = millis();
}
};
Adxl345measure data;
void setup() {
Serial.begin(115200);
data.acquire();
// not always a good idea to access member variables directly, but just for the sake of example
Serial.println(data.samplingTime);
Serial.println(data.x);
Serial.println(data.y);
Serial.println(data.z);
}
void loop() {}
This sounds like a perfect example of employing a little OO to the mix. I’m sure there are lots of other ways to do this, but I like structs….
I’m not sure what level of c++ developer you are, so this may be redundant to you but here’s a little refresher
If you know it’s always going to be an array, then you can declare an internal variable as an array and hold it there. Otherwise you can declare your struct an array declaration time, and hold many versions of your struct in memory.
Keep in mind, the previous poster mentioned, that you can only pass these by reference. So if something updates them outside your main program flow, the data can change unexpectedly.
When the data items of the measure variable can be directly manipulated by the adxl345measure() function, then what is the necessity/benefit of creating another data space named thedata?
void adxl345measure()//(t_data & theData)
{ // the & denotes that you pass the parameter by reference
measure.samples[0] = 1.1;
measure.samples[1] = 2.2;
measure.samples[2] = 3.3;
measure.samplingTime = millis();
}
you can use the function to deal with different containers and not depend on the global variable. (say you want to make five measures sort them in some sort of order)