I define an array from another array in a loop. When I print from within this loop the created array is as expected, but when I print from outside the loop it changes.
int PDOL_response[PDOL_Length];
for (int i = PDOL_start_index; i < (PDOL_start_index + PDOL_Length); i++){
for (int j = 0; j< PDOL_Length; j++){
PDOL_response[j] = (response[i]);
Serial.println(PDOL_response[j],HEX);
break;
}
}
This outputs:
9F, 35, 01, 9F, 6E, 04, 9F, 4E, 20
for (int i = 0; i<PDOL_Length; i++){
Serial.println(PDOL_response[i],HEX);
}
not clear what the values are in both PD0L_response [] and response []
in the more conventionally formatted code below, the "break" prevents j from every getting beyond a value of 0 which is since PDOL_response [0] is set to response[i] means the values in response []
int PDOL_response[PDOL_Length];
...
for (int i = PDOL_start_index; i < (PDOL_start_index + PDOL_Length); i++){
for (int j = 0; j < PDOL_Length; j++){
PDOL_response[j] = (response[i]);
Serial.println (PDOL_response[j], HEX);
break;
}
}
...
That is obviously NOT what you thought you were doing. What is it you thought you were doing? Moving all of the bytes into ints, for some reason?
So you are basically printing 'response[i]': 9F, 35, 01, 9F, 6E, 04, 9F, 4E, 20
Since you are only ever setting 'PDOL_response[0]' it makes sense that the first element is the last value it was set to and the other elements are garbage.
I think what you might have wanted is:
byte PDOL_response[PDOL_Length];
for (int i = 0; i < PDOL_Length; i++)
{
PDOL_response[i] = response[PDOL_start_index + i];
Serial.println(PDOL_response[i], HEX);
}