Pages: 1 [2] 3   Go Down
Author Topic: Sketch examples of scrolling text on 8x8 LED matrix w/ MAX72xx  (Read 13157 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
now my letter is scrolling seamlessly

The prior routine suffers from setRow() not wrapping around - if you see its source code, it simply returns when the "row" index is greater than 7 / less than 0. Makes sense for what it does but not for what you want to do.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code has two for loops and it can be time consuming. How about this:

Code:
  static unsigned char col_starting=0; //starting column
  spiTransfer(addr, 0, a[(col_starting + 0) & 0x07]);
  spiTransfer(addr, 1, a[(col_starting + 1) & 0x07]);
  spiTransfer(addr, 2, a[(col_starting + 2) & 0x07]);
  ...
  spiTransfer(addr, 7, a[(col_starting + 7) & 0x07]);
  col_starting = (col_starting==7)?0:(col_starting+1); //increment index

no loop but 8 transfers. you can modify it for your requirement.
Logged

New York
Offline Offline
Sr. Member
****
Karma: 4
Posts: 366
xronosclock.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code has two for loops and it can be time consuming. How about this:

Code:
  static unsigned char col_starting=0; //starting column
  spiTransfer(addr, 0, a[(col_starting + 0) & 0x07]);
  spiTransfer(addr, 1, a[(col_starting + 1) & 0x07]);
  spiTransfer(addr, 2, a[(col_starting + 2) & 0x07]);
  ...
  spiTransfer(addr, 7, a[(col_starting + 7) & 0x07]);
  col_starting = (col_starting==7)?0:(col_starting+1); //increment index

no loop but 8 transfers. you can modify it for your requirement.
smiley-eek  Thanks!  Honestly I don't really understand any of it (it's a little too advance for me at this stage), but I will try! smiley I need to read some more about SPI...
Logged

Xronos Clock - A talking arduino based alarm clock is now available. Check out xronosclock.com for pictures, source code, schematics, and purchasing info smiley

Norfolk UK
Offline Offline
Edison Member
*
Karma: 65
Posts: 2453
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Brattan,

I just realised that these two code examples do not use Ledcontrol library so when I get home from work I will see what else I have found online and post the link or code for them. But in the meantime -

Scrolltest2 .ino, by Riva on this forum, is attached to the fourth post in this thread http://arduino.cc/forum/index.php/topic,123787.msg939503.html#msg939503
The MAX7219 library I use in Scrolltest2.ino is just a subset of the LedControl library that I hacked to reduce size as original project ran on a ATmega8 and I was running out of space. If you replace the MAX7219 references with LedControl then it should still work okay.
#include <MAX7219.h> becomes
#include <LedControl.h>
and
MAX7219 lc=MAX7219(DIN,CLK,LOAD,numDevices); becomes
LedControl lc=LedControl (DIN,CLK,LOAD,numDevices);

EDIT: I have attached the latest test version of my scrolling code that does kerning that has been modified for the MAX7219 from the Rainbowduino it used to work on. It compiled okay but I don't have a matrix to test it on so beware. Maybe some kind soul will test it and confirm it works okay.

* ScrollTest4MAX.ino (14.18 KB - downloaded 71 times.)
« Last Edit: October 18, 2012, 05:03:24 am by Riva » Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

New York
Offline Offline
Sr. Member
****
Karma: 4
Posts: 366
xronosclock.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It compiled okay but I don't have a matrix to test it on so beware. Maybe some kind soul will test it and confirm it works okay.[/color]
Thanks Riva, I will give it a try tonight!  I soldered 2 matricies with MAX7219 chips and will see it runs on it.
I'm still trying to come up with my own code, but due to lack of programming knowledge, it's hard but fun smiley
Logged

Xronos Clock - A talking arduino based alarm clock is now available. Check out xronosclock.com for pictures, source code, schematics, and purchasing info smiley

Norfolk UK
Offline Offline
Edison Member
*
Karma: 65
Posts: 2453
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Riva, I will give it a try tonight!  I soldered 2 matricies with MAX7219 chips and will see it runs on it.
I'm still trying to come up with my own code, but due to lack of programming knowledge, it's hard but fun smiley
I have a couple of clocks using 16x8 LED matrix controlled by a pair of MAX7219 chips but one is 400 miles away and the other is not to be messed with #so says the wife smiley I'm sure you will get the hang of it. I've only been programming C++ since February 2012 though I have used other languages for about 30 years.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

New York
Offline Offline
Sr. Member
****
Karma: 4
Posts: 366
xronosclock.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

30 years!  smiley-eek
Trying to understand your code, I think I'm starting to get it. Right off the bet, I found tiny issue: need to initialize second display (lc.shutdown(1,false);  etc).
Also why bufferLong is 7?  Should it be 14 if using 2 displays?  Or it doesn't matter?
What does this this code exactly performs?
Code:
pgm_read_byte_near(font5x7 + ((ascii - 0x20) * 8) + a);
I understand you are reading it from prog memory and offsetting by 20 characters to get right ASCII, but not sure what "*8" is for and what "font5x7 +" does? Your adding something to an array name not array's element?   smiley-confuse
Sorry for stupid questions smiley

Edit: Ok I hooked up my LED matrices, and your code totally works... except it looks like I have mine wired/arranged differently from yours smiley-sad   Instead of scrolling from one matrix to another, it scrolls as if they arranged one on top of each other (i.e. image is rotated 90 degrees clockwise, so it's not going from right to left, but from bottom to the top on each). I need to figure how to to rotate image 90 degrees CW (i.e. rows become columns, etc.).
« Last Edit: October 18, 2012, 07:05:21 pm by bratan » Logged

Xronos Clock - A talking arduino based alarm clock is now available. Check out xronosclock.com for pictures, source code, schematics, and purchasing info smiley

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Riva,

I just tried your ScrollTest 4 MAX code on my Max 7219, 8 x 8 Matrix set up and I can report that it works perfectly. I am not 100% sure, but when I compared it with your ScrollTest 2 version I think that there are two column spaces between chars with the ScrollTest 2 version but only one column space with the ScrollTest 4 version but I may be mistaken as it scrolls quite quickly and I couldn't find the code section that would slow down the scroll speed to verify this. I linked a short video of the 4 version so you can see what you think or if you tell me how to adjust the scroll speed code I will verify my suspicions. Once again excellent nice clean fonts,

 Pedro.

p.s. I hope you did not mind me posting this on my youtube channel so I could show you, and if you wish me to remove it just ask and I'll take it off.

Logged


Norfolk UK
Offline Offline
Edison Member
*
Karma: 65
Posts: 2453
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also why bufferLong is 7?  Should it be 14 if using 2 displays?  Or it doesn't matter?
I am using a 5x7 font (5 bits wide x 7 bits deep) but it is stored as 8 bits wide (byte) as this is what processors like and it makes the maths easy  smiley-lol. The font is only 7 bits tall so I only need 7 buffers but these buffers are longs so are 32 bits wide. What happens when you scroll a character is it's matrix data (8 bits wide) is loaded from font5x7 and places in the lower 8 bits of the 7 bufferLong's (keeping the upper 24 bits) it then reads the 8th byte from the font5x7 matrix data that is the number of bits to scroll this particular character (kerning). It rotates the bufferLong's by 1 bit and then displays the bits 16-23 in the first (left) LED matrix and bits 8-15 in the right LED matrix. It keeps rotating one bit at a time and displaying until the character width has rotated out of the lower 8 bits of the buffer and then goes back to get the next character so scroll.

Quote
What does this this code exactly performs?
Code:
pgm_read_byte_near(font5x7 + ((ascii - 0x20) * 8) + a);
I understand you are reading it from prog memory and offsetting by 20 characters to get right ASCII, but not sure what "*8" is for and what "font5x7 +" does? Your adding something to an array name not array's element? 
The font data is stored as a single dimension byte array (a contiguous block of memory), I need 7 bytes to describe the font data for a single character and the 8th byte is the width of that character in bits (used for kerning). font5x7 point to the starts of this memory so for each increment in the Ascii number I have to jump 8 bytes of this contiguous memory to find the start of that characters font data.
Quote
Edit: Ok I hooked up my LED matrices, and your code totally works... except it looks like I have mine wired/arranged differently from yours smiley-sad   Instead of scrolling from one matrix to another, it scrolls as if they arranged one on top of each other (i.e. image is rotated 90 degrees clockwise, so it's not going from right to left, but from bottom to the top on each). I need to figure how to to rotate image 90 degrees CW (i.e. rows become columns, etc.).
To do this you would need to re-define the font data and alter the way scrolling works, I'm sure someone else has done scrolling this way so if you find the fonts and code it could be adapted to the MAX7219.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Norfolk UK
Offline Offline
Edison Member
*
Karma: 65
Posts: 2453
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just tried your ScrollTest 4 MAX code on my Max 7219, 8 x 8 Matrix set up and I can report that it works perfectly. I am not 100% sure, but when I compared it with your ScrollTest 2 version I think that there are two column spaces between chars with the ScrollTest 2 version but only one column space with the ScrollTest 4 version but I may be mistaken as it scrolls quite quickly and I couldn't find the code section that would slow down the scroll speed to verify this. I linked a short video of the 4 version so you can see what you think or if you tell me how to adjust the scroll speed code I will verify my suspicions. Once again excellent nice clean fonts,
Adjust this to alter the scroll speed.
Code:
const long scrollDelay = 70;

This version is a slight improvement (in my opinion) on ScrollTest2 as it does character kerning. It only scrolls the width of the character instead of a fixed width of the old version. This makes the display look neater when text has mix of wide and narrow characters like M & I respectfully.
A quick an easy hack to alter the overall spacing between characters is to add the below highlighted line
Quote
void loadBufferLong(int ascii){
    if (ascii >= 0x20 && ascii <=0x7f){
        for (int a=0;a<7;a++){                      // Loop 7 times for a 5x7 font
            unsigned long c = pgm_read_byte_near(font5x7 + ((ascii - 0x20) * smiley-cool + a);     // Index into character table to get row data
            unsigned long x = bufferLong [a];       // Load current scroll buffer
            x = x | c;                              // OR the new character onto end of current
            bufferLong [a] = x;                     // Store in buffer
        }
        byte count = pgm_read_byte_near(font5x7 +((ascii - 0x20) * smiley-cool + 7);     // Index into character table for kerning data
        count++
        for (byte x=0; x<count;x++){
            rotateBufferLong();
            printBufferLong();
            delay(scrollDelay);
        }
    }
}
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Riva, I don't know how I missed that scroll speed code. Nice touch with the code allowance for varying char widths. It's attention to detail like that that differentiate good from "top shelf" IMHO  smiley-cool
Edit - where would I / we be without Google "Kerning is an adjustment of the space between two letters" If I may ask you Riva since you obviously have a long career in programming. How does someone new to all this, learn coding particuarly with Arduino in mind. The general consensus on the forum seems to be to work through all the projects from simple to complex but from how I see it, I feel that there is a better way. Along with progressively trying more complex projects and looking at the code and trying to see how it works, I am doing a little online study in C++.  What are your thoughts if I may.
« Last Edit: October 20, 2012, 04:59:26 pm by Pedro147 » Logged


New York
Offline Offline
Sr. Member
****
Karma: 4
Posts: 366
xronosclock.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And I'm done! more or less smiley
I wrote simplified code (as my C++ knowledge is poor), but it works with my setup of 2 matrix displays!

As I was trying to adopt other people's code (Riva yours is brilliant, but didn't work in my hardware arrangement) I realized that I wired my matrices different from most setups. Pins go in one long row (i.e. Pin 12 of matrix 1 is next to Pin 1 of matrix 2), and I actually think this setup easier to wire on breadboards (probably the only way to wire on breadboard) so I'm at loss to understand why mine don't work the same, maybe I wired it different (after all I had to figure out pinouts becuase I had no datasheet, and these are not the same as more popular Sure's displays).
Anyway it doesn't matter, as I figured out code and algorithm of animation. Writing code from scratch is a great educational experience, but it took me almost a week smiley-sad
It currently works great with 2 devices, but I'm not sure how much modification will be needed to support 3 or more, I will try to enhance it later to see if I can make it universal for any number of displays.
Code:
Here's my algorithm:
Legend:
B1 bufer 1
B2 bufer 2
NextChar = Font of next character
1. Fill Buffer B1
2. Begin Loop . If Looped for 8 times, go to 11
3. Display B1
4. Display B2 (it's empty 1st time)
5. Rotate B1: B1[7]=B1[6], B1[6]=B1[5]...B1[1]=B1[0], B1[0]=NextChar[7]
6. Rotate B2: B2[7]=B2[6], B2[6]=B2[5]...B2[1]=B2[0], B2[0]=B1[7]
7. Display B1
8. Display B2
9. Delay (animation delay)
10. Go To 2 (End Loop)
11. Go to 1
I'm also attaching code (it has font file as well in the rar archive), feel free to use and modify it.
I know font is crappy, I had to do everything from scratch because my characters are rotated 90CCW. Pial's web based 8x8 matrix font generator helped a lot!  I will draw a prettier font in the future smiley  I would use other's people nice fonts, if someone can suggest how I can rotate them 90 CCW.
I'm also attaching image that shows how animation works in my code.

* Bratan_LED_ScrollTest_version1.rar (3.11 KB - downloaded 46 times.)

* animation.png (19.31 KB, 336x1116 - viewed 41 times.)
Logged

Xronos Clock - A talking arduino based alarm clock is now available. Check out xronosclock.com for pictures, source code, schematics, and purchasing info smiley

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 518
Posts: 26371
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Got my first youtube video loaded!

http://youtu.be/hwYqgyMc5S4

Scroll test of 8x32 LED matrix. Can hold up to 233 characters in SRAM. Will be ~40 more once I push 5x8 fonts out to PROGMEM.
« Last Edit: October 21, 2012, 12:30:02 pm by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Lookin' good there CR  smiley-cool Once I build up my 8 x 32 board that I have been planning for a while now, I would love to try your code. Its amazing how many people are "scared" of SPI
Logged


Norfolk UK
Offline Offline
Edison Member
*
Karma: 65
Posts: 2453
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well done for getting it sorted.
Quote
It currently works great with 2 devices, but I'm not sure how much modification will be needed to support 3 or more, I will try to enhance it later to see if I can make it universal for any number of displays.
The advantage of the way you address the MAX7219 is for scrolling you only need to add 1 byte from the vertically defined font to scroll where with the horizontal way I used you need to do a lot of AND & OR on 8 bytes to achieve the same thing. The current code I use could theoretically do 24x8 matrix with little modification but to go beyond that needs a bit more work. The vertical font way is a lot easier to expand to larger matrix arrays and could scroll a lot faster. I like the idea of using the 8x8 matrix blocks, beats the hell out of hand soldering 128 individual LED's. I might be tempted to do a smaller wordclock using these.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Pages: 1 [2] 3   Go Up
Jump to: