OK, I was looking into using an array to make it easy to modify a script later. Basically i want to declare an array of values and then update the values when needed. Now do I need to directly update the array or can I declare the items in the array as other variables and it be updated for me when the variable updates, hopefully the code example will make things a bit clearer :-/
can I just go
int a = 3;
int b = 1;
int array[] = {a, b};
void loop()
{
b = 6;
}
and the array go from [3, 1] to [3, 6]?
or do I need to go
int a = 3;
int b = 1;
int array[] = {a, b};
void loop()
{
b = 6;
array[] = {a, b};
}
I won't know when or if or what value has changed so updating the whole array at the end of the script is what I'll do (if that is needed to be done)
Neither of your examples will work as written the way you are expecting them to. To do what you are trying to accomplish:
int a = 3;
int b = 1;
int array[] = {a, b};
void loop()
{
array[1] = 6;
}
In other words, you have defined your array with two integer values of "3" and "1" in it (you could have written it also as "int array[] = {3, 1};") - these occupy slots 0 and 1 of the array (array counting begins at 0) - so, you have defined an array with two integers (the length of the array is "2"), positions 0 and 1 in the array.
In the code above, we change position 1 of the array, from the value "1" it was initialized as, to the value "6".
Also note that in your code, it would be better to define the values as integer constants rather than integer variables (unless you have a specific reason to do so), because the constants don't take up valuable RAM space (the pre-processor subs the values in where needed), whereas the variable declarations do:
Define your arrays for the size of data going into them - RAM is precious on microcontrollers, so if you only need the space for byte-sized values, declare as byte (vs 2 bytes needed for an integer value per array element, etc).
Name your variables properly - don't use "a", "b", "array" - give them names that mean something (also remember to use and stick with a type of casing approach - I typically use all upper-case for my #define constants, this kind of "CamelCasing" for my variables, and this kind of "camelCasing()" for my function defines. Trust me - it will make things MUCH easier to read and understand; even if you are the only coder on the project, you WILL come back to the code at a later date, and if you don't have much documentation (comments are your friends), you will be left scratching your head.
I have been doing software development for over 25 years in one form or another; this is experience speaking to you: please heed it, or ignore it at your peril.
Good luck, and I hope this post made things clearer.
Thanks for the help and advice,
Sounds like I will just need to be careful if and when I change which variable is store in which element of the array, the variables will change during the code so I cannot define them.
On the other 2 notes;
Yep I will be making sure that the arrays are declared as the smallest type needed, just use int in the example for no reason.
a, b and array were just example names for the example, don't worry I have seen to many "value1"s to do it. The case approach you use (specific casing style per type of object) does make a lot of sense and I will remember to use that (also got to get better at commenting everything!).
Edit:
Neither of your examples will work as written the way you are expecting them to.
does this imply that they will compile and run but not work correctly or did you mean "Neither of your examples will work as written.". If they will compile and run could you guesse what the expected result be?
They will compile and run but they won't do what your wanting them to do.
So what will they do? Nothing?
What would happen if you redeclare the same array, would the old one be scraped and a new one created or would you end up with 1000s of array until you ran out of memory?
I think I understand why "array[] = {blah, blah, blah}" would not work as you are not telling where to put "blah, blah, blah" in the array (right/wrong?)
As I don't need "array" to be global I can declare it within "void loop()" so if there is a way to deleted "array" and then redeclare it with the update variables that would work (if it's possible)
Edit:
free(array) <---- is this what I'm looking for? Example code to come
byte tempkettle;
byte levelkettle;
byte temppot;
byte levelpot;
byte disp = 1;
char* array1[] = {"temperature", "levels"};
void loop()
{
byte array2[] = {levelkettle, levelpot};
byte array3[] = {tempkettle, temppot};
serial.print(array1[disp]);
serial.print(array2[disp]);
serial.print(array3[disp]);
free(array2);
free(array3);
*update tempkettle with some sort of code*
*update levelkettle with some sort of code*
*update temppot with some sort of code*
*update levelpot with some sort of code*
*code to determine if disp == 1 or 2
}
byte tempkettle;
byte levelkettle;
byte temppot;
byte levelpot;
byte disp = 1;
char* titlearray[] = {"kettle", "pot"};
void loop()
{
byte levelarray[] = {levelkettle, levelpot};
byte temparray[] = {tempkettle, temppot};
serial.print(titlearray[disp]);
serial.print(levelarray[disp]);
serial.print(temparray[disp]);
*update tempkettle with some sort of code*
*update levelkettle with some sort of code*
*update temppot with some sort of code*
*update levelpot with some sort of code*
*code to determine if disp == 1 or 2
}
Would work?
Notice that all non informative names have been changed
Except for that fact that serial isn't a valid object, yes, this will compile. (Serial is a valid object.)
It won't work, because tempkettle, temppot, levelkettle, and levelpot aren't initialized, so the levelarray and temparray arrays are not properly initialized, either.
It won't work, because tempkettle, temppot, levelkettle, and levelpot aren't initialized, so the levelarray and temparray arrays are not properly initialized, either.
Sweet, so "= 0" for all of them will sort that? :-?
Or, put the tempkettle, temppot, levelkettle, and levelpot updating script above the "byte levelarray[] = {levelkettle, levelpot}; byte temparray[] = {tempkettle, temppot};"
would work also? :-?
Thanks Paul.