Go Down

Topic: silly LCD scrolling issue (Read 1 time) previous topic - next topic

mitchelwb

I'm very new to Arduino, and it's been several years since I did any coding in C++ so I'm a little rusty.

I have an HD44780 16x2 LCD that I've been playing with.  I'm attempting to get it to repeatedly scroll a message longer than the 16 characters available on the LCD.

I have it scrolling, but not quite like I want.  The sketch is currently just scrolling an all caps alphabet, and I'm trying to code it such that it would reach the end and immediately start back at the beginning and at some point would look like:

"...XYZABC..."

Instead, what I'm getting is:
"...XYZ        "
and eventually all the way down to:
"Z               "

Once the final Z scrolls off, then it will start back at the beginning, however, it starts with A all the way to the left like:
"ABC..."

I've rewritten the code several different ways, and continually get the same result although the code seems to get tighter each time.  here is the current version:

Quote


#include <LiquidCrystal.h>
#include <String.h>

// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int  delayTime = 300;
char string[]  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
char string2[] = "ABCDEFGHIJKLMNOP                        QRSTUVWXYZ            ";
char string3[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //sizeof = 27



int pos = 0;
char display[16];
int i = 0;
int grab = 0;

void setup()
{
 // Print a message to the LCD.
 //lcd.print(sizeof(string3));
 
}

void loop()
{
 generateDisplay();
 lcd.home();
 lcd.clear();
 lcd.print(display);
 delay(delayTime);

}

void generateDisplay()
{
 grab = pos;
 for(i=0; i<16; i++)
 {
   grab=(grab >= sizeof(string3))?0:grab;      
   display = string3[grab];
   grab++;
 }
 pos++;
 if(pos > sizeof(string3))
 {
    pos = 0;
 }
}




What am I missing?

Thanks
Mitch

mitchelwb

*minor footnote

I started with a different piece of sample code that expected it to be wired slightly different than how I already had it wired.  So the comments and the code don't match, but the code is correct and comments are outdated.

mitchelwb

Found it.  was as simple as changing it to check that grab was less than sizeof my string MINUS ONE.  

Even though there are only 26 characters in the array, the sizeof is 27.  I knew that, never occurred to me that there was some form of value there.  

It now does exactly what I wanted it to do.

estranged

Can you post the corrected code?  Scrolling text on my LCD is something I've wanted to play with, but haven't gotten around to yet.

mitchelwb

happy to.  

I'm sure there are probably better ways of doing this... But the best way for me to learn some of this stuff is to just brute force my way through and reinvent some small wheels for a while.  That said, I don't think it's a half bad chunk of code... especially not for someone who got his Arduino last week and his LCD yesterday! :)

Quote


#include <LiquidCrystal.h>


// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int  delayTime = 500;
char string1[]  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
char string2[] = "ABCDEFGHIJKLMNOP                        QRSTUVWXYZ            ";
char string3[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //sizeof = 27
char string[] = "I love PINBALL!! ";



int pos = 0;
char display[16];
int i = 0;
int grab = 0;

void setup()
{

}

void loop()
{
 generateDisplay();
 lcd.home();
 lcd.clear();
 lcd.print(display);
 delay(delayTime);
}

void generateDisplay()
{
 grab = pos;
 for(i=0; i<16; i++)
 {
   grab=(grab >= sizeof(string)-1)?0:grab;      
   display = string[grab];
   grab++;
 }
 pos++;
 if(pos > sizeof(string))
 {
    pos = 0;
 }
}



Go Up