# How do I transform data for a column to data for a row, for a led matrix

I have a 8x8 led matrix. I'm using LedControl.h library to control the matrix, which has 3 ways to light up the leds: setLed, setRow and setColumn. I've found that setColumn it's really slow, it's using setLed to print columns. With all my code, I can only print 3 times a second with setColumn. The function setRow is way faster, I'm guessing that the design of the matrix is allowing to print the rows directly. I can print more than 10 times a second with setRow, so I have to use it, since speed is crucial to my project. In my code, the data i have to print is stored in an array of 8 bytes. This is how it looks like when I use setColumn

``````for(int i = 7; i >= 0; i--)
lc.setColumn(0, i, vector[i]);
``````

i is the row to be printed, and vector[i] is a byte value, indicating which leds to be on or off. Column 0 is the left-most one, while column 7 is the right-most one, so i go from 7 to 0 to print from right to left, because that's what I have to do. My goal is to transform somehow to data in vector[i], to data that I can use it using setRow. So that's what I've done so far.

``````byte mask = 128, x;
for (int j = 0; j < 8; j++){
x = 0;
for(int p = 0; p < 8; p++){
x = x | (valueVector[p] & mask);
if (p != 7)
x = x >> 1;
}
if (j != 7)
lc.setRow(0, j, x);
}
``````

Now i print the rows from row 0 to row 7, from top to bottom. Since I'm printing from top to bottom, this means I have to take the last bit of every column (last bit of every element in vector), and put it in a byte variable. I was thinking to make a byte mask = 128 (b1000000, do a AND logic between to mask and the vector value in order to take the bit I'm looking for, and then making a OR to put the bit in the x byte. After every mask operation, I shift the x variable to the right, in order to make space for the next bit. After i'm done with a row, I shift the mask to the right, in order to take the second bit from the value in the vector. The result I get, is quite odd, I can't explain it, it's not good at all. What I'm i doing wrong with the mask and shifts? I hope I explained well what I'm trying to do. If necessary, I can provide more information. Thanks.

• please post the entire code Dec 15 '20 at 22:32
• why can't you physically rotate the square matrix? Dec 15 '20 at 23:53

You use a mask `10000000`. With each iteration of the outer loop the `1` in the mask is shifted to the right. `01000000` -> `00100000` -> `00010000`. Let's say we are in the 2nd iteration the mask is `01000000`. `x = x | (valueVector[p] & mask);` takes the 2nd bit (counted from left) and sets it at the 2nd position in the x value. But this position contains the bit from the last iteration (as you shifted it to this position).

Possible solution: (I have no compiler at hand. So hopefully it is correct. ;-)

``````byte x;

for ( int j=0; j<8; j++ )
{
x = 0;

for ( int p=0; p<8; p++ )
x = x | ((valueVector[p] & (1 << p)) >> p);

lc.setRow(0, j, x);
}
``````