Structs have some benefits in that each variable in the struct can be a different type, e.g. Voltage could be float, but you may want to store RPM as an int
You can have an array of structs if you want to store multiple lots of the same group of data.
Roger's use of structs is a good one. I tend to think of arrays of structs as arrays for adults. You can also use symbolic constants and also consts:
#define VOLTAGE 0
#define AMPERAGE 1
const int CAPACITANCE 2
const int RPM 3
// some statements...
int myArray[4];
int val;
val = myArray[VOLTAGE];
// more code...
val = myArray[CAPACITANCE];
The symbolic constants using the #defines are simply textual replacements, they do not create variables in the true sense of the word. The consts do create variables so if we ever get a symbolic debugger, they will have an advantage over symbolic constants.
A struct is definitely the way to go. An array makes all the values inside it set to a single type, int, byte, boolean . . . whereas in a struct, each variable can have its own type. You can even put a struct in an array, if you ever feel the need to.
econjack:
The symbolic constants using the #defines are simply textual replacements, they do not create variables in the true sense of the word. The consts do create variables so if we ever get a symbolic debugger, they will have an advantage over symbolic constants.
That is what david was pointing out with an enum.
enum{
VOLTAGE,
AMPERAGE,
CAPACITANCE,
RPM,
};
int myArray[4];
int val;
void setup() {
val = myArray[ VOLTAGE ];
val = myArray[ CAPACITANCE ];
}
With 2 arrays of equal size, I've used one array for data, another for the data labels. The data labels are strings that get stored in SDRAM and do not use up valuable ram (I think).
Each string will need to be placed into PROGMEM, then an array of those pointers can be formed.
Otherwise the whole lot is in precious RAM.
Oops ... I meant to reply with "const char* data_label...", but access would be by index.
Great suggestions here ... I'll be testing the use of enumeration in my future coding.
dlloyd:
With 2 arrays of equal size, I've used one array for data, another for the data labels. The data labels are strings that get stored in SDRAM and do not use up valuable ram (I think).
Each string will need to be placed into PROGMEM, then an array of those pointers can be formed.
Otherwise the whole lot is in precious RAM.
How do I access the contents of cell 4, at any time, for example?
Also, is there an advantage to using an array if you only need small values of data such as 5 variables?
I know the advantage is huge when having a 10x10 array, but say for a 1 column and 6 rows?
How do I access the contents of cell 4, at any time, for example?
myInts will have 6 elements, indexed from 0 to 5. So the 4th element is index number 3, and would be accessed like this:
Serial.println(myInts[3]);
// or ...
byte arrayIndex = 3;
Serial.println(myInts[arrayIndex]);
The advantage of using arrays is that the index can be a variable and therefore it is easy to access all elements sequentially in a for loop, or access a specific element based on the result of a calculation. Otherwise, you would need lots of repeated code or complex if or switch statements.
Also, is there an advantage to using an array if you only need small values of data such as 5 variables?
I know the advantage is huge when having a 10x10 array, but say for a 1 column and 6 rows?
Choice of data structure involves more than just the number of variables. One consideration is the algorithms that will be used. You should not put unrelated variables in an array just because there are many of them. With experience you will get a better feel for what data structure to use in a given situation.