Go Down

Topic: Matrix problem (Read 2083 times) previous topic - next topic

bogdan666

The code was with MSBFIRST, so arrow was flipped, i corrected that to LSBFIRST, and the scrolling is going from right to left, you can leave it like this for text scrolling later on, also, since the arrow was going backwards, it looked funny and i turned it around, one more modify that i made was that i made it a bit shorter, since the body of the arrow was 16 leds long, i didn't understand what was going on because the 4th row was always on. One more thing that i modified, was the multiplex period, i made it 1, and the flickering to the last row when the entire matrix is on has disappeared. This is the code that i use now

Code: [Select]
#include <SPI.h>

#define LATCH 10
#define MULTIPLEX_PERIOD 1
#define SCROLL_PERIOD 200

SPISettings shiftRegister(10000000UL, LSBFIRST, SPI_MODE0);

unsigned int image[8] = {
  0b0001000000000000,
  0b0011000000000000,
  0b0111000000000000,
  0b1111111100000000,
  0b0111000000000000,
  0b0011000000000000,
  0b0001000000000000,
  0b0000000000000000
};

void setup() {
  SPI.begin(); //Start up the SPI hardware
  SPI.beginTransaction(shiftRegister); //Prepare to send data to shift registers
  pinMode(LATCH, OUTPUT);
}

void multiplexDisplay() {
  //"static" means these variables does not loose their value when multiplexDisplay() ends
  static byte currRow; //the row, 0 to 7, of the matrix that is currently lit
  static unsigned long lastUpdate; //time when this row was lit
  unsigned long millisNow = millis(); //Get time now
  if (millisNow - lastUpdate >= MULTIPLEX_PERIOD) { //Is it time to light next row of matrix?
    lastUpdate = millisNow; //Record time this row was lit
    SPI.transfer(128 >> currRow); //Send a byte indicating which row to light
    SPI.transfer(~highByte(image[currRow])); //Send the required pattern for this row of the matrix
    SPI.transfer(~lowByte(image[currRow]));
    digitalWrite(LATCH, LOW);
    digitalWrite(LATCH, HIGH); //Latch the data into the shift register outputs
    if (currRow++ > 7) currRow = 0; //Get ready to light next row of matrix
  }
}

void scrollDisplay() {
  static unsigned long lastUpdate; //time when matrix was last scrolled
  unsigned long millisNow = millis(); //Get time now
  if (millisNow - lastUpdate >= SCROLL_PERIOD) { //Is it time to scroll the matrix?
    lastUpdate = millisNow; //Record time the matrix was scrolled
    for (byte row = 0; row < 8; row++) {
      byte savedBit = bitRead(image[row], 15);
      image[row] <<= 1;
      bitWrite(image[row], 0, savedBit);
    }
  }
}

void loop() {
  multiplexDisplay();
  scrollDisplay();
}

PaulRB

#76
Apr 14, 2019, 03:57 pm Last Edit: Apr 14, 2019, 05:12 pm by PaulRB
Ok, the next step will be a long one. We need to get the font data from your original sketch, organise it better, and get messages scrolling across the display.

To make things easier and keep the code short, I think we should try to follow the ASCII character set:
Code: [Select]
( )   32  02/00   40  20                 SPACE
(!)   33  02/01   41  21                 EXCLAMATION MARK
(")   34  02/02   42  22                 QUOTATION MARK
(#)   35  02/03   43  23                 NUMBER SIGN
($)   36  02/04   44  24                 DOLLAR SIGN
(%)   37  02/05   45  25                 PERCENT SIGN
(&)   38  02/06   46  26                 AMPERSAND
(')   39  02/07   47  27                 APOSTROPHE
(()   40  02/08   50  28                 LEFT PARENTHESIS
())   41  02/09   51  29                 RIGHT PARENTHESIS
(*)   42  02/10   52  2A                 ASTERISK
(+)   43  02/11   53  2B                 PLUS SIGN
(,)   44  02/12   54  2C                 COMMA
(-)   45  02/13   55  2D                 HYPHEN, MINUS SIGN
(.)   46  02/14   56  2E                 PERIOD, FULL STOP
(/)   47  02/15   57  2F                 SOLIDUS, SLASH
(0)   48  03/00   60  30                 DIGIT ZERO
(1)   49  03/01   61  31                 DIGIT ONE
(2)   50  03/02   62  32                 DIGIT TWO
(3)   51  03/03   63  33                 DIGIT THREE
(4)   52  03/04   64  34                 DIGIT FOUR
(5)   53  03/05   65  35                 DIGIT FIVE
(6)   54  03/06   66  36                 DIGIT SIX
(7)   55  03/07   67  37                 DIGIT SEVEN
(8)   56  03/08   70  38                 DIGIT EIGHT
(9)   57  03/09   71  39                 DIGIT NINE
(:)   58  03/10   72  3A                 COLON
(;)   59  03/11   73  3B                 SEMICOLON
(<)   60  03/12   74  3C                 LESS-THAN SIGN, LEFT ANGLE BRACKET
(=)   61  03/13   75  3D                 EQUALS SIGN
(>)   62  03/14   76  3E                 GREATER-THAN SIGN, RIGHT ANGLE BRACKET
(?)   63  03/15   77  3F                 QUESTION MARK
(@)   64  04/00  100  40                 COMMERCIAL AT SIGN
(A)   65  04/01  101  41                 CAPITAL LETTER A
(B)   66  04/02  102  42                 CAPITAL LETTER B
(C)   67  04/03  103  43                 CAPITAL LETTER C
(D)   68  04/04  104  44                 CAPITAL LETTER D
(E)   69  04/05  105  45                 CAPITAL LETTER E
(F)   70  04/06  106  46                 CAPITAL LETTER F
(G)   71  04/07  107  47                 CAPITAL LETTER G
(H)   72  04/08  110  48                 CAPITAL LETTER H
(I)   73  04/09  111  49                 CAPITAL LETTER I
(J)   74  04/10  112  4A                 CAPITAL LETTER J
(K)   75  04/11  113  4B                 CAPITAL LETTER K
(L)   76  04/12  114  4C                 CAPITAL LETTER L
(M)   77  04/13  115  4D                 CAPITAL LETTER M
(N)   78  04/14  116  4E                 CAPITAL LETTER N
(O)   79  04/15  117  4F                 CAPITAL LETTER O
(P)   80  05/00  120  50                 CAPITAL LETTER P
(Q)   81  05/01  121  51                 CAPITAL LETTER Q
(R)   82  05/02  122  52                 CAPITAL LETTER R
(S)   83  05/03  123  53                 CAPITAL LETTER S
(T)   84  05/04  124  54                 CAPITAL LETTER T
(U)   85  05/05  125  55                 CAPITAL LETTER U
(V)   86  05/06  126  56                 CAPITAL LETTER V
(W)   87  05/07  127  57                 CAPITAL LETTER W
(X)   88  05/08  130  58                 CAPITAL LETTER X
(Y)   89  05/09  131  59                 CAPITAL LETTER Y
(Z)   90  05/10  132  5A                 CAPITAL LETTER Z
([)   91  05/11  133  5B                 LEFT SQUARE BRACKET
(\)   92  05/12  134  5C                 REVERSE SOLIDUS (BACKSLASH)
(])   93  05/13  135  5D                 RIGHT SQUARE BRACKET
(^)   94  05/14  136  5E                 CIRCUMFLEX ACCENT
(_)   95  05/15  137  5F                 LOW LINE, UNDERLINE
(`)   96  06/00  140  60                 GRAVE ACCENT
(a)   97  06/01  141  61                 SMALL LETTER a
(b)   98  06/02  142  62                 SMALL LETTER b
(c)   99  06/03  143  63                 SMALL LETTER c
(d)  100  06/04  144  64                 SMALL LETTER d
(e)  101  06/05  145  65                 SMALL LETTER e
(f)  102  06/06  146  66                 SMALL LETTER f
(g)  103  06/07  147  67                 SMALL LETTER g
(h)  104  06/08  150  68                 SMALL LETTER h
(i)  105  06/09  151  69                 SMALL LETTER i
(j)  106  06/10  152  6A                 SMALL LETTER j
(k)  107  06/11  153  6B                 SMALL LETTER k
(l)  108  06/12  154  6C                 SMALL LETTER l
(m)  109  06/13  155  6D                 SMALL LETTER m
(n)  110  06/14  156  6E                 SMALL LETTER n
(o)  111  06/15  157  6F                 SMALL LETTER o
(p)  112  07/00  160  70                 SMALL LETTER p
(q)  113  07/01  161  71                 SMALL LETTER q
(r)  114  07/02  162  72                 SMALL LETTER r
(s)  115  07/03  163  73                 SMALL LETTER s
(t)  116  07/04  164  74                 SMALL LETTER t
(u)  117  07/05  165  75                 SMALL LETTER u
(v)  118  07/06  166  76                 SMALL LETTER v
(w)  119  07/07  167  77                 SMALL LETTER w
(x)  120  07/08  170  78                 SMALL LETTER x
(y)  121  07/09  171  79                 SMALL LETTER y
(z)  122  07/10  172  7A                 SMALL LETTER z
({)  123  07/11  173  7B                 LEFT CURLY BRACKET, LEFT BRACE
(|)  124  07/12  174  7C                 VERTICAL LINE, VERTICAL BAR
(})  125  07/13  175  7D                 RIGHT CURLY BRACKET, RIGHT BRACE
(~)  126  07/14  176  7E                 TILDE

PaulRB

OK, I copied the font data from your old sketch and re-organised it into a single array, with the characters appearing in the exact same order as the ASCII set above.
Code: [Select]

byte font[][6] = {
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // SPACE ' '
  {B11111111, B11111111, B00000101, B11111111, B11111111, B11111111 },    // EXCLAMATION MARK '!'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // QUOTATION MARK '"'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // NUMBER SIGN '#'
  {B11111111, B10011101, B01101101, B00000001, B01101101, B01110011 },    // DOLLER '$'
  {B11111111, B10111011, B11110111, B11101111, B11011111, B10111011 },    // PERCENT '%'
  {B11111111, B10010011, B01101101, B01100101, B10011011, B11110101 },    // AND '&'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // APOSTROPHE '''
  {B11111111, B11111111, B11111111, B10000011, B01111101, B11111111 },    // BRACKET START '('
  {B11111111, B11111111, B01111101, B10000011, B11111111, B11111111 },    // BRACKET END ')'
  {B11111111, B10111011, B11010111, B11101111, B11010111, B10111011 },    // MULTIPLY '*'
  {B11111111, B11101111, B11101111, B10000011, B11101111, B11101111 },    // PLUS '+'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // COMMA ','
  {B11111111, B11101111, B11101111, B11101111, B11101111, B11101111 },    // MINUS '-'  
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // PERIOD '.'
  {B11111111, B11111011, B11110111, B11101111, B11011111, B10111111 },    // DIVIDE '/'
  {B11111111, B10000011, B01111101, B01111101, B01111101, B10000011 },    // NUMBER 0
  {B11111111, B11111111, B10111101, B00000001, B11111101, B11111111 },
  {B11111111, B10111001, B01110101, B01101101, B01011101, B10111101 },
  {B11111111, B10111011, B01111101, B01101101, B01101101, B10010011 },
  {B11111111, B11100111, B11010111, B10110111, B00000001, B11110111 },
  {B11111111, B00011011, B01101101, B01101101, B01101101, B01110011 },
  {B11111111, B10010011, B01101101, B01101101, B01101101, B11110011 },
  {B11111111, B01111111, B01110001, B01101111, B01011111, B00111111 },
  {B11111111, B10010011, B01101101, B01101101, B01101101, B10010011 },
  {B11111111, B10011111, B01101101, B01101101, B01101101, B10010011 },    // NUMBER 9
  {B11111111, B11111111, B11111111, B10111011, B11111111, B11111111 },    // COLON ':'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // SEMICOLON ';'
  {B11111111, B11111111, B11110111, B11101011, B11011101, B11111111 },    // LESS THAN '<'
  {B11111111, B11010111, B11010111, B11010111, B11010111, B11111111 },    // EQUAL '='
  {B11111111, B11111111, B11011101, B11101011, B11110111, B11111111 },    // GREATER THAN '>'
  {B11111111, B10111111, B01111111, B01110101, B01101111, B10011111 },    // QUESTION MARK '?'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // AT SIGN '@'
  {B11111111, B10000001, B01110111, B01110111, B01110111, B10000001 },     // LETTER A
  {B11111111, B00000001, B01101101, B01101101, B01101101, B10010011 },
  {B11111111, B10000011, B01111101, B01111101, B01111101, B10111011 },
  {B11111111, B00000001, B01111101, B01111101, B01111101, B10000011 },
  {B11111111, B00000001, B01101101, B01101101, B01101101, B01111101 },
  {B11111111, B00000001, B01101111, B01101111, B01101111, B01111111 },
  {B11111111, B10000011, B01111101, B01111101, B01110101, B10110011 },
  {B11111111, B00000001, B11101111, B11101111, B11101111, B00000001 },
  {B11111111, B11111111, B01111101, B00000001, B01111101, B11111111 },
  {B11111111, B11111011, B11111101, B01111101, B00000011, B01111111 },
  {B11111111, B00000001, B11101111, B11010111, B10111011, B01111101 },
  {B11111111, B00000001, B11111101, B11111101, B11111101, B11111101 },
  {B11111111, B00000001, B10111111, B11001111, B10111111, B00000001 },
  {B11111111, B00000001, B11011111, B11101111, B11110111, B00000001 },
  {B11111111, B10000011, B01111101, B01111101, B01111101, B10000011 },
  {B11111111, B00000001, B01101111, B01101111, B01101111, B10011111 },
  {B11111111, B10000011, B01111101, B01110101, B01111011, B10000101 },
  {B11111111, B00000001, B01101111, B01100111, B01101011, B10011101 },
  {B11111111, B10011101, B01101101, B01101101, B01101101, B01110011 },
  {B11111111, B01111111, B01111111, B00000001, B01111111, B01111111 },
  {B11111111, B00000011, B11111101, B11111101, B11111101, B00000011 },
  {B11111111, B00000111, B11111011, B11111101, B11111011, B00000111 },
  {B11111111, B00000001, B11111011, B11100111, B11111011, B00000001 },
  {B11111111, B00111001, B11010111, B11101111, B11010111, B00111001 },
  {B11111111, B00011111, B11101111, B11110001, B11101111, B00011111 },
  {B11111111, B01111001, B01110101, B01101101, B01011101, B00111101 },     // LETTER Z
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // LEFT SQUARE BRACKET '['
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // BACKSLASH '\'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // RIGHT SQUARE BRACKET ']'
  {B11111111, B11011111, B10111111, B01111111, B10111111, B11011111 },    // POWER '^'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // UNDERSCORE '_'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // GRAVE ACCENT '`'
  {B10000001, B01111111, B01000011, B11111111, B10000011, B11111111 },    // LETTER a
  {B10000001, B01111111, B01000011, B01111111, B10011111, B10111111 },
  {B11111111, B10000011, B11111111, B10000001, B01111111, B01000011 },
  {B11111111, B10000011, B11111111, B10000011, B01111111, B01111111 },
  {B10000111, B01111111, B01111111, B10000011, B11100111, B11101111 },
  {B11111111, B11101001, B11001001, B00100111, B00101111, B01111111 },
  {B00011111, B01111111, B01111111, B11111111, B10011111, B01111111 },
  {B11100111, B11101111, B11100111, B11101111, B11100111, B00001111 },
  {B01001111, B01111111, B10000001, B01111111, B01111111, B10011111 },
  {B11111111, B10000011, B11111111, B10000011, B01111111, B01111111 },
  {B10000111, B01111111, B01111111, B10000011, B11100111, B11101111 },
  {B11111111, B00000001, B11111101, B11111101, B11111101, B11111101 },
  {B11111111, B00000001, B10111111, B11001111, B10111111, B00000001 },
  {B11111111, B00000001, B11011111, B11101111, B11110111, B00000001 },
  {B11111111, B10000011, B01111101, B01111101, B01111101, B10000011 },
  {B11111111, B00000001, B01101111, B01101111, B01101111, B10011111 },
  {B11111111, B10000011, B01111101, B01110101, B01111011, B10000101 },
  {B11111111, B00000001, B01101111, B01100111, B01101011, B10011101 },
  {B11111111, B10011101, B01101101, B01101101, B01101101, B01110011 },
  {B11111111, B01111111, B01111111, B00000001, B01111111, B01111111 },
  {B11111111, B00000011, B11111101, B11111101, B11111101, B00000011 },
  {B11111111, B00000111, B11111011, B11111101, B11111011, B00000111 },
  {B11111111, B00000001, B11111011, B11100111, B11111011, B00000001 },
  {B11111111, B00111001, B11010111, B11101111, B11010111, B00111001 },
  {B11111111, B00011111, B11101111, B11110001, B11101111, B00011111 },
  {B11111111, B01111001, B01110101, B01101101, B01011101, B00111101 },    // LETTER z
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // LEFT BRACE '{'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // VERTICAL LINE, '|'
  {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111 },    // RIGHT BRACE '}'
  {B11111111, B11110111, B11101111, B11110111, B11111011, B11110111 },    // APPROXIMATELY '~'
  {B11111111, B11111111, B11000011, B10111101, B01010110, B01111010 },    // HAPPY FACE '[]'
  {B01111010, B01010110, B10111101, B11000011, B11111111, B11111111 },
  {B11111111, B11111111, B11000011, B10111101, B01011010, B01110110 },    // SAD FACE '{}'
  {B01110110, B01011010, B10111101, B11000011, B11111111, B11111111 },
  {B11111111, B11111111, B10001111, B00000111, B00000011, B10000001 },    // HEART ',.'
  {B10000001, B00000011, B00000111, B10001111, B11111111, B11111111 }
};

Can you check this please? Have I got the sequence exactly the same as the ASCII set, not missing or inserting any?

bogdan666

they are the same, except for happy face, sad face and the heart, which we don't care too much.

PaulRB

Thanks, lets pick up again tomorrow.

bogdan666

No problem, my bench power supply is broken and i can't seem to find what's wrong with it, so i am distracted and it's kinda my priority because i feel like without hands.

PaulRB

#81
Apr 17, 2019, 09:39 pm Last Edit: Apr 17, 2019, 10:05 pm by PaulRB
Sorry to hear that, hope you get it fixed.

I've attempted to get the text scrolling:
Code: [Select]
#include <SPI.h>
#include "font.h"

#define LATCH 10
#define MULTIPLEX_PERIOD 1
#define SCROLL_PERIOD 200

SPISettings shiftRegister(10000000UL, LSBFIRST, SPI_MODE0);

unsigned int image[8];

char message[] = "BOGODAN666   ";

void setup() {
  SPI.begin(); //Start up the SPI hardware
  SPI.beginTransaction(shiftRegister); //Prepare to send data to shift registers
  pinMode(LATCH, OUTPUT);
}

void multiplexDisplay() {
  //"static" means these variables does not loose their value when multiplexDisplay() ends
  static byte currRow; //the row, 0 to 7, of the matrix that is currently lit
  static unsigned long lastUpdate; //time when this row was lit
  unsigned long millisNow = millis(); //Get time now
  if (millisNow - lastUpdate >= MULTIPLEX_PERIOD) { //Is it time to light next row of matrix?
    lastUpdate = millisNow; //Record time this row was lit
    SPI.transfer(128 >> currRow); //Send a byte indicating which row to light
    SPI.transfer(highByte(image[currRow])); //Send the required pattern for this row of the matrix
    SPI.transfer(lowByte(image[currRow]));
    digitalWrite(LATCH, LOW);
    digitalWrite(LATCH, HIGH); //Latch the data into the shift register outputs
    if (++currRow > 7) currRow = 0; //Get ready to light next row of matrix
  }
}

void scrollDisplay() {
  static unsigned long lastUpdate; //time when matrix was last scrolled
  static byte charPos;
  static byte dataPos;
  unsigned long millisNow = millis(); //Get time now
  if (millisNow - lastUpdate >= SCROLL_PERIOD) { //Is it time to scroll the matrix?
    lastUpdate = millisNow; //Record time the matrix was scrolled
    byte newData = font[message[charPos] - ' '][dataPos]; //Get new character pattern to be scrolled onto matrix
    for (byte row = 0; row < 8; row++) {
      image[row] <<= 1; //Scroll this line of the matrix
      bitWrite(image[row], 0, bitRead(newData, row)); //add in the bit of the character pattern to this row
    }
    if (++dataPos > 6) { //Move to next column of character pattern
      dataPos = 0;
      if (++charPos >= strlen(message)) charPos = 0; //Move to next character of message
    }
  }
}

void loop() {
  multiplexDisplay();
  scrollDisplay();
}


I put the font array from the last post into the "font.h" tab. To make the new tab in your IDE, press ctrl-shift-N and enter the name "font.h". Then paste the big array from the previous post into the new tab.

bogdan666

Ok, weird things are happening, the text begin to scroll from the middle, and is upside down.

PaulRB

the text begin to scroll from the middle
Can you explain more or post a youtube vid?

and is upside down.
Change this line as below:
Code: [Select]
      bitWrite(image[row], 0, bitRead(newData, 7 - row)); //add in the bit of the character pattern to this row

bogdan666

the change for the flip worked, what i meant by: it starts from the middle was this
the letter B starts from the 8th column and moves towards left
after it reaches column 1 it goes to column 16th and goes towards left
and it fades in column 9
basically, if i break my matrix in half, in order to make it work i have to put the right matrix in front of the left matrix. I think that the the data from 1st 595 should go to 2nd, and the data from 2nd 595 should go to 1st.

PaulRB

the letter B starts from the 8th column and moves towards left
after it reaches column 1 it goes to column 16th and goes towards left
and it fades in column 9
basically, if i break my matrix in half, in order to make it work i have to put the right matrix in front of the left matrix. I think that the the data from 1st 595 should go to 2nd, and the data from 2nd 595 should go to 1st.
And you did not see the same thing with the scrolling arrow? Did you change the wiring? If not, try swaping the highByte() and lowByte() functions again.

bogdan666

With the arrow it worked perfectly, for this i changed the highbyte and lowbyte and it works. Only problem is that the matrix starts with every led on, then as the letters scrolls they turn off(the unwanted leds), so at the 2nd time the text scrolls it's ok. Question, how does the code knows which is letter "B" , because i see no way of knowing this? the font.h tab has nowhere written that "this" is letter B.

PaulRB

#87
Apr 19, 2019, 05:58 pm Last Edit: Apr 19, 2019, 06:02 pm by PaulRB
Change this line
Code: [Select]
unsigned int image[8] = {~0, ~0, ~0, ~0, ~0, ~0, ~0, ~0};

The code knows where to find B in the font array because the array is in ASCII order. Characters are stored in the sketch as ASCII codes. But the font array starts with the space character at index zero, and the ASCII code for a space is 32. So when the code goes to find B in the array, it first subtracts the code for a space (32) from the code for B (66) giving an index of 66 - 32 = 34.

bogdan666

ok, i get it, also very tidy since the fonts are in a different tab

PaulRB

So, I think we got to the functions you wanted? Do you have any other questions about how the code works? Do you still think the matrix is bright enough? Can you post a youtube video? I would like to see it.

Go Up