Convert incoming Array to a matrix

Hii guys,

I’m getting an array of 768 temperature values from my sensor. I’m taking the average value of the temps and transform this to binary values (maybe I should put this in an array too?). Part of the code:

    for (int x = 0 ; x < 768 ; x++)
    {
      PixelValue = mlx90640To[x];
      
      if (ThresholdValue > PixelValue) {
        PixelValueBinary = 0;
      }
      else {
        PixelValueValue = 1;
      }
    }

Now want I want to do: I want to put the binary value into an 32x24 (X, Y) Matrix. Tried some stuff but without succes. Any suggestions? Saw this standard form:

int Matrix[Y][X];
for (int i =0; i < Y; i++) {
  for (int j =0; j < X; j++) {
    Matrix[i][j] = some data;
  }
}

That's an awful lot of RAM.

AWOL:
That's an awful lot of RAM.

Yes using a Teensy, can't be done on an Arduino

Tried some stuff but

didn't post it, or explain what problems were encountered.

Is the suggestion here that you're using a sixteen bit int to store a single bit of data?

AWOL:
didn’t post it, or explain what problems were encountered.

Is the suggestion here that you’re using a sixteen bit int to store a single bit of data?

Kinda but that’s not really the problem. I’m getting temp values from my sensor and calculate the average value of all 768 values. Every value above the average value gets an 1, and everything underneath the average value get’s a 0. So the values are changed from temperature values to binary.

Now I want to store the binary data into a matrix of 32x24, but I can’t figure out how.

Could it be that the compiler threw out this line?Matrix[i][j] = some data;

for (int x = 0 ; x < 768 ; x++)
    {
      PixelValue = mlx90640To[x];
                                                 /*XValue ++;

                                                 if (XValue == 31){
                                                 YValue ++;
                                                 XValue=0;
                                                 }*/

      if (ThresholdWaarde > PixelValue) {
        PixelValueBinary = 0;
      }
      else {
        PixelValueBinary = 1;
      }
      //Serial.print (PixelWaardeBinair);
      //Serial.print(", ");
    }

          int Matrix[24][32];
      for (int i = 0; i < 23; i++) {
      for (int j = 0; j < 31; j++) {
        Matrix[i][j] = PixelValueBinary;
      }
    }
      Serial.print (Matrix [24][32]);

I’m stuck at this now, I think I should store the PixelValueBinary inside an array first. Because the matrix for-loop inside the other for-loop won’t work of course.

You could do something like this:

  int x = 0;
  byte matrix_X = 0;
  byte matrix_Y = 0;

  do {
    do {
      PixelValue = mlx90640To[x];
      if (ThresholdValue > PixelValue) {
        PixelValueBinary = 0;
      } else {
        PixelValueBinary = 1;
      }
      Maxrix[matrix_X][matrix_Y] = PixelValueBinary;
      x++;
      matrix_Y++;
    } while (matrix_Y < 24);
    matrix_Y = 0;
    matrix_X++;
  } while (matrix_X < 32);

or a simpler form, although you might not be familiar with the ternary operator

  int x = 0;
  byte matrix_X = 0;
  byte matrix_Y = 0;

  do {
    do {
      Maxrix[matrix_X][matrix_Y] = (ThresholdValue > mlx90640To[x])? 0:1; //returns 0 if condition true, 1 if false
      x++;
      matrix_Y++;
    } while (matrix_Y < 24);
    matrix_Y = 0;
    matrix_X++;
  } while (matrix_X < 32);

Because the matrix for-loop inside the other for-loop won't work of course.

Have you noticed that 32 x 24 = 768?

Maybe your outer loop could be, in fact, two loops.

Thanks David, used the first option:

 int x = 0;
  byte matrix_X = 0;
  byte matrix_Y = 0;
  int Matrix[32][24];

  do {
    do {
      PixelWaarde = mlx90640To[x];
      if (ThresholdWaarde > PixelWaarde) {
        PixelWaardeBinair = 0;
      } else {
        PixelWaardeBinair = 1;
      }
      Matrix[matrix_X][matrix_Y] = PixelWaardeBinair;
      x++;
      matrix_Y++;
    } while (matrix_Y < 24);
    matrix_Y = 0;
    matrix_X++;
    } while (matrix_X < 32);

  for (int a = 0; a < 24 ; a++)
  {    
    for (int b = 0; b < 32; b++)
    {
  Serial.print(Matrix [b][a]);
  }
  Serial.println("");
  }

Giving me an output:

11:01:43.040 → 27.85,26.96,25.14,25.89,24.74,24.97,24.44,24.51,25.02,24.55,24.38…
11:07:31.417 → 00000000000000000010001001100000
11:07:31.417 → 00000000000000000010001001100000
11:07:31.417 → 00000000000000010011000100110000
11:07:31.417 → 00000000000000010011000000010000
11:07:31.417 → 00000000000000010011001100110000
11:07:31.417 → 00000000000000010001001100110000
11:07:31.417 → 00000000000000010001001100110000
11:07:31.417 → 00000000000000000001001100110000
11:07:31.417 → 00000000000000000001000100110100
11:07:31.417 → 00000000000000000001000100110000
11:07:31.417 → 00000000000000001001100000010000
11:07:31.417 → 00000000000000001001100000000000
11:07:31.417 → 00000000000000001001100010001000
11:07:31.417 → 00000000000000001001100110001000
11:07:31.417 → 00000000000000001000100110001000
11:07:31.417 → 00000000000000001000100110001000
11:07:31.417 → 00000000000000000000100010001010
11:07:31.417 → 00000000000000000000100010001010
11:07:31.417 → 00000000000001000100100010000000
11:07:31.417 → 00000000000001000100110000000000
11:07:31.417 → 00000000000001000100110011000000
11:07:31.417 → 00000000000001000100110011000000
11:07:31.417 → 00000000000000000100010011000000
11:07:31.417 → 00000000000000000100010011000000

Looks good so far

Why a do..while loop?
Isn't a for loop more natural?

anyway I can serial.print the matrix better?

Maybe with a newline every row?

AWOL:
Why a do..while loop?
Isn't a for loop more natural?
Maybe with a newline every row?

yes already added :slight_smile:

The newline yes, the for loops, no.

int Matrix[32][24];

Still a terrible waste of memory.

AWOL:
The newline yes, the for loops, no.Still a terrible waste of memory.

But I need the matrix for my project... or what do you mean? How could I do things differently?

But I need the matrix for my project

So? Does that mean that you need to waste 31 bits for every one you store?

Store multiple bits in each element of the array. On the Teensy, where an int is 32 bits, each row would fit in one element of an 24 element int array.

Where/how are you using that array?

PaulS:
So? Does that mean that you need to waste 31 bits for every one you store?

Store multiple bits in each element of the array. On the Teensy, where an int is 32 bits, each row would fit in one element of an 24 element int array.

Where/how are you using that array?

Ahh sorry, I understand. Fixed :wink: