Asking for help,
I'm really trying to comprehend Arrays. I just started messing with them a few weeks ago.
Im wanting to create a sketch that will read a couple of arrays and create a single string to be passed to a function. I've developed the following that does just that:
#define TAGS(x) (sizeof(x) / sizeof(x[0]))
#define VALS(x) (sizeof(x) / sizeof(x[0]))
const char *tag[] = {"State= ", ", Bat(%)= ", ", Bat(mV)= ", ", RSSI= ", ", SMS#= ", ", Lock= ", ", Light= "};
char *val[] = {"1", "100", "4217", "22", "11", "unlocked", "OFF"};
char message[100];
int i;
int pbat = 97;
void setup() {
Serial.begin(115200);
}
void loop() {
getStats();
delay(5000);
}
void getStats(){
strcpy(message, "");
for(i=0; i<TAGS(tag); i++){
strcat(message, tag[i]);
strcat(message, val[i]);
}
Serial.println(message);
}
The out is as expeted below:
State= 1, Bat(%)= 100, Bat(mV)= 4217, RSSI= 22, SMS#= 11, Lock= unlocked, Light= OFF
But I need to change the contents of the 2nd Array based on changing inputs of my Arduino UNO. The problem I'm having is changing int's to char's and then writting them to the 2nd Array. The following code works for doing this but it contains Strings, and I'm trying to avoid using Strings as much as possible.
char cpbat[4];
String str;
str = String(pbat);
str.toCharArray(cpbat, 4);
val[1] = cpbat;
The output is as expected below:
State= 1, Bat(%)= 97, Bat(mV)= 4217, RSSI= 22, SMS#= 11, Lock= unlocked, Light= OFF
The following line of code will write to the array but will only write the contents to element 1 (index 0) of the array. I've tried defining which index to write it to but can't find a way so that it will complie without error:
itoa(pbat, *val, 10);
The output is as follows:
State= 97, Bat(%)= 100, Bat(mV)= 4217, RSSI= 22, SMS#= 197, Lock= , Light= OFF
The following line of code will write to the correct index that I define but again it will also always stuff the contents into element 1 (index 0) of the array, even when I tell it to write the contents to element 6 (index 5). It seems to corrupt other elements as well. Example:
val[6] = itoa(pbat, *val, 10);
The output is as follows:
State= 97, Bat(%)= 100, Bat(mV)= 4217, RSSI= 22, SMS#= 197, Lock= , Light= 97
I've tried so many variations to write what I need into the 2nd Array but to no avail and I'm starting to repeat my self with attempts. I'm really hoping this can be accomplished without using Strings.
Any ideas?