Basic Scrolling Text Code

Hello, I am new to Arduino and hope that I am posting in the right section. I have constructed an 8 x 8 LED Matrix controlled by two 74HC595 shift registers and a ULN2803 driving the cathodes. I used this code from InsideGadgets ;

http://www.insidegadgets.com/2010/11/07/fun-with-8x8-led-matrix/

The code for scrolling text is attached as Doc 1
I got it to work ok but it only shows you how to code for three characters (two being the same character) and was wondering if some kind person here could show me how to code it for more characters. I understand how to make additional arrays but am not sure on what and where I have to alter the code other than how the original author does with the three characters. Also why at “int LEDs[8][17]” is there a 17 when there are only 16 columns of data?

Hoping that someone can help me,

thank you BluesBoy. :slight_smile:

Doc 1.doc (26 KB)

No sensible person would download a .doc file. Can you copy and paste it between Code markers or save it as .txt instead.

I apologise for my indiscretion Riva. I have reattached the code as a txt file as requested. How do I paste the code between code markers? Thank you for your patience,

BluesBoy

Txt1.txt (2.75 KB)

Go into your Arduino IDE and select and copy the code, then in the online editor select the little "#" key and and where the cursor is between the code /code paste your program code.

it will look like this when done -

/*
 LED Matrix - Display scrolling text
 Version: 1.0
 Author: electricteardown (http://insidegadgets.wordpress.com)
 Created: 6/11/2010
 Last Modified: 6/11/2010
 
*/

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
//Pin connected to DS of 74HC595
int dataPin = 11;

int counter = 0;
int movement = 0;

int LEDs[8][17] = {   
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}      
};

int letterH[8][8] = {   
  {0, 1, 0, 0, 0, 0, 1, 0},
  {0, 1, 0, 0, 0, 0, 1, 0},
  {0, 1, 0, 0, 0, 0, 1, 0},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 1, 0, 0, 0, 0, 1, 0},
  {0, 1, 0, 0, 0, 0, 1, 0},
  {0, 1, 0, 0, 0, 0, 1, 0}      
};

int letterI[8][8] = {   
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},     
};


void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  
  if (movement == 0) {
    int x;
    int y;
    for (x = 0; x < 8; x++) {
      for (y = 0; y < 8; y++) {
        LEDs[x][y+8] = letterH[x][y];
      }
    }
  } 
  if (movement == 8) {
    int x;
    int y;
    for (x = 0; x < 8; x++) {
      for (y = 0; y < 8; y++) {
        LEDs[x][y+8] = letterI[x][y];
      }
    }
  } 
  
  if (counter % 25 == 0) {
    movement++;
    int x;
    int y;
    for (x = 0; x < 8; x++) {
      for (y = 0; y < 16; y++) {
        LEDs[x][y] = LEDs[x][y+1];
      }
    }  
  }
  if (movement >= 16) {
    counter = 0;
    movement = 0;
  }
  
  lightLED();
  counter++;
}

void lightLED() {
  byte columnbitsToSend = 0;
  byte rowbitsToSend = 0;
  
  int x = 0;
  int y = 0;
  for (x = 0; x < 8; x++) {
    columnbitsToSend = 0;
    rowbitsToSend = 0;
      
    for (y = 0; y < 8; y ++) {
      if (LEDs[x][y] == 1) {
        bitWrite(rowbitsToSend, y, HIGH);
      } 
    }
    
    digitalWrite(latchPin, LOW);
    bitWrite(columnbitsToSend, x, HIGH);
    shiftOut(dataPin, clockPin, MSBFIRST, columnbitsToSend);
    shiftOut(dataPin, clockPin, MSBFIRST, rowbitsToSend);
    digitalWrite(latchPin, HIGH);   
    delay(2);
  }
}

Why the LEDs array is 17?
Because of this little bit of code -
for (x = 0; x < 8; x++) {
for (y = 0; y < 16; y++) {
LEDs[y] = LEDs[y+1];
}
Because in this code it looks like he was using 16 elements, but the y+1 (that he never writes to, probably should be filled with zeros) makes the 17th element. elements 0-15 make for 16 elements. element 16 is the 17th element. (don’t you just love C…) When you have that little quirk mastered you will be a level 1 C programmer.

int LEDs[8][17]

will consume 8*2 bytes for something that should be stored in in a byte. With other words, you store each bit in 2 bytes. In addition you store constant arrays in SRAM which also not the best idea. I recommend to do it more along the lines of my POV scripts: http://blog.blinkenlight.net/experiments/basic-effects/pov-reloaded/

kf2qd,

thanks for your reply and excuse my ignorance but where is the online editor where you select the hash key. I am at work with no access to IDE . I have noticed a "copy for forum" option in the edit menu of the IDE. Also, am I correct in assuming from the general consensus of the replies to my initial question that this piece of code is not particularly well written and if so can you suggest any other scrolling code examples that could get me started in writing my own. Thanks for your input and also the other forum members who responded to me,

BluesBoy

I'm also at work today so cannot access suitable code for you at the moment but will be off tomorrow so should get time then. Do you just want pointers on how to do it or some example code to adapt?

The Hash (#) key is on this forum. You see it just above the row of smiley faces when you reply to messages. Hover your mouse over any of the buttons to see what they mean.

The code is not very efficient on it's use of memory. Firstly each letter consumes 128 bytes of RAM where it could be re-written to consume only 8 bytes per letter. It also spends a lot of time converting those 128 bytes to the 8 bytes that need sending to the shift registers and yet more time moving those 128 bytes along to do the scrolling.

I'm not sure how useful this is, but here's a library for controlling an 8x8 matrix. It includes code to scroll (in the printRoutine function, under "else if (printType == SCROLL) {"

Thank you for your kind offer Riva. I would love some examples of good efficient code, because I have found that looking at some basic code helps me to understand how to change it do something a bit more involved. For example since I posted my initial question in this forum thread, I have worked out how to expand on the number of characters it will display. Admittedly the way that I have done this would be far from "an elegant solution" it has still expanded my meager knowledge base. In saying that I would also appreciate any pointers on how to develop my own code. Thanks again Riva,

BluesBoy.

Hello WizenedEE, I am not quite sure I understand what you mean when you say quote - "but here's a library for controlling an 8x8 matrix It includes code to scroll (in the printRoutine function, under "else if (printType == SCROLL) {" " Which library? Thanks BluesBoy.

I would love some examples of good efficient code, because I have found that looking at some basic code helps me to understand how to change it do something a bit more involved.

As I don’t need to write code I can send this sample code to you sooner.
I’m not sure seasoned C++ programmers would class this example as ‘good efficient code’ but it uses a lot less RAM than your example and this can be further reduced by putting the matrix table in PROGMEM. The font is 5x7 instead of your 8x8 one and this was written to test scrolling messages on a 16x8 LED matrix driven by MAX7219 chips but you will get the idea on scrolling and all you need to do is alter the ‘printBufferLong()’ to suit your multiplexing chips. I have commented the code fairly well (by my standards) but if your lost about how it works then ask me.

ScrollTest2.ino (13.3 KB)

Thanks a lot Riva I will have a good look at that and see what I can do with it, and thank you for your offer of help, BluesBoy.

BluesBoy: Hello WizenedEE, I am not quite sure I understand what you mean when you say quote - "but here's a library for controlling an 8x8 matrix It includes code to scroll (in the printRoutine function, under "else if (printType == SCROLL) {" " Which library? Thanks BluesBoy.

WHAT? If you were looking at my screen it would be quite clear...

anyway, here's the link: https://github.com/WizenedEE/arduino/tree/master/libraries/LEDArray

Sorry for my ignorance WizenedEE and thank you very much for the link to your obviously well written library and code. It is clear to me that you "know your stuff" so to speak whereas I am just starting out with the Arduino and this coding is for me a steep learning curve (some say that I am a bit slow on the uptake :) ) In reference to; "WHAT? If you were looking at my screen it would be quite clear..." I still fail to see how I could deduce the location of your link from "your screen"

WizenedEE: I'm not sure how useful this is, but here's a library for controlling an 8x8 matrix. It includes code to scroll (in the printRoutine function, under "else if (printType == SCROLL) {"

I am obviously missing something quite basic so if you could humour me I would appreciate it. But quite sincerly thanks again and even though I am a complete novice I can still learn something from looking at your code, BluesBoy.

BluesBoy: Sorry for my ignorance WizenedEE and thank you very much for the link to your obviously well written library and code. It is clear to me that you "know your stuff" so to speak whereas I am just starting out with the Arduino and this coding is for me a steep learning curve (some say that I am a bit slow on the uptake :) ) In reference to; "WHAT? If you were looking at my screen it would be quite clear..." I still fail to see how I could deduce the location of your link from "your screen"

WizenedEE: I'm not sure how useful this is, but here's a library for controlling an 8x8 matrix. It includes code to scroll (in the printRoutine function, under "else if (printType == SCROLL) {"

I am obviously missing something quite basic so if you could humour me I would appreciate it. But quite sincerly thanks again and even though I am a complete novice I can still learn something from looking at your code, BluesBoy.

I was making fun of myself.

Like they say "laughter is the best medicine" 8) Have a happy and humourous day WizenedEE