I have an array, which I split into 12 tokens within my void loop()
strRecordIn = 56.0,0.5,-0,100,830,960,0,400,4100,10240,4095,20
char strRecordIn[RECORD_IN_ARRAY_SIZE]; // one record in from BMS; every second a new one
char strRecordOut[RECORD_OUT_ARRAY_SIZE]; // one record out from BMS
If a run a for() through strRecordIn , I see all tokens
I am add some of the values to strRecordOut, like so:
// create array of BMS values
int arrayElements = 11; // number of BMS values (12)
char *arrValues[arrayElements]; // array holding values
arrValues[0] = strtok(strRecordIn, ",");
for (int j = 1; j <=arrayElements; j++) {
arrValues[j] = strtok(NULL, ","); // Split string into tokens
}
strcat(strRecordOut, clock.dateFormat("d/m/Y H:i:s", dt)); // add delimiter
for (int i = 0; i <= 3; i++) { // VLT, AMP, AHR, SOC
strcat(strRecordOut, ","); // add delimiter
strcat(strRecordOut, arrValues[i]); // add value
}
if (intLoopCounter == 50) { // every 50 records read temperature sensor
sensors.requestTemperatures(); // get temperatues
currentTemp = sensors.getTempCByIndex(0); // read 1st temperature sensor
intLoopCounter = 0; // reset
}
// here: temp float value, -12.34 (6), 2, buf = 6+\0
//char charBuf[] = "";
//dtostrf(currentTemp, 6, 2, charBuf); // convert float to char
strcat(strRecordOut, ","); // add delimiter
//strcat(strRecordOut, charBuf); // ad temperature
strcat(strRecordOut, "22.22"); // ad temperature
// get field 7 -- ERR error value // ERR
strcat(strRecordOut, ","); // add delimiter
strcat(strRecordOut, arrValues[6]); // add error value
// get field 12 -- TIM value // TIM= BMS record count (0-45)
strcat(strRecordOut, ","); // add delimiter
strcat(strRecordOut, arrValues[11]); // add received record count
for (int k = 0; k <=arrayElements; k++) {
Serial.print(k);
Serial.print(" ");
Serial.println(arrValues[k]);
}
Serial.println(strRecordOut);
strRecordIn[0] = '\0'; // empty array
strRecordOut[0] = '\0'; // empty array
recordInByteCount = 0; // reset byte counter
When I check the array values, they are all OK (values here do not match the array above; as I am showing output of real values)
0 53.0
1 -13.7
2 -86
3 79
4 835
5 960
6 0
7 400
8 3958
9 1975
10 4095
11 4
08/07/2016 19:56:47,53.0,-13.7,-86,79,22.22,0,4 // strRecordOut
Now the interesting part. when I change this section
//char charBuf[] = "";
//dtostrf(currentTemp, 6, 2, charBuf); // convert float to char
strcat(strRecordOut, ","); // add delimiter
//strcat(strRecordOut, charBuf); // ad temperature
strcat(strRecordOut, "22.22"); // ad temperature
to:
char charBuf[] = "";
dtostrf(currentTemp, 6, 2, charBuf); // convert float to char
strcat(strRecordOut, ","); // add delimiter
strcat(strRecordOut, charBuf); // ad temperature
which changes the temperature float value to char, I l no longer have access to array value 11, and the output is like so:
0 52.9
1 -35.3
2 -88
3 78
4 833
5 960
6 0
7 400
8 3742
9 12616
10 4095
11 ™ê
08/07/2016 20:02:48,52.9,-35.3,-88,78, 24.13,0,™ê
Why on earth does it kill that last value? Any help/hint appreciated.
Also, the temperature value 24.13 has a space in front of it. Is there an easy way to get rid of it?