Hey all. First time posting to this forum. I have an issue with creating values for an array in a nested for-loop. The output of this code results in garbage and non-sense values being printed to the Serial port. It seems so straight-forward that I can't figure out what is going wrong and where. I tried printing out the individual index values (idx) in each loop iteration, and they are also nonsense which makes sense as to why the values of the array are nonsense. I've tried troubleshooting and can't seem to come up with a good reason for this on my own. I took out all extraneous code to make sure nothing else was happening, so here is what remains and still has the problem occur:
int test[160];
void setup() {
Serial.begin(9600);
readMatrix(test);
delay(1000);
for(int i = 0; i <160; i++)
{
Serial.println(test[i]);
}
}
void loop() {
}
void readMatrix(int mat[])
{
int idx; // create index
for (int i = 0; i < 10; i++){
for (int j = 0; j < 16; j++){ // 10 * 16 loops goes through all 160 indeces
idx = ((i+1) * (j+1)) - 1; // update index
//idx = i;
mat[idx] = idx; // set array value of index equal to the index value
Serial.println(idx); // print it out
}
}
}
Editing the readMatrix() function to the following results in it printing out the values 1 - 160 exactly as intended in the above code:
void readMatrix(int mat[])
{
int idx; // create index
for (int i = 0; i < 160; i++){
idx = i;
mat[idx] = idx; // set array value of index equal to the index value
Serial.println(idx); // print it out
}
}
Questions I assume might be asked about the code and I will try to answer beforehand:
Why use a function to alter the array?
I need to use this function multiple times in the full code, and it would be far too messy otherwise.
What is the overall purpose of this?
Reading values from a force sensor matrix that is 10 x 16. The way the multiplexers are set up, the nested for loop is the easiest and best option for filling the values of the array with the force readings.
Board?
Arduino Uno. I don't think it's the board because it's working with tons of other scripts I've written for it.
Any help would be greatly appreciated. Also, with this being my first post, if this is the wrong place for this I will move it.