Arduino LCD Scroll Function For Opposite Directions And One Row Scroll

This article discusses scroll function for Arduino lcd 16*2 name of this function is scroller and it takes in certain arguments before it could function.

Scroller has the capability to scroll Left scroll Right and both rows in Opposite Directions it can also scroll only one row while keeping other row static.

scroller(Char Array 1, Size of Array 1, Char Array 2 , Size of Array 2 , Direction , Row , Delay) 

To See The Functionality Of it You May Like To See This Video



This is the function it requires following as arguments 

  • Two Char type arrays their sizes 
  • Direcrion which could be L = Left R= Right and B = Both Directions 
  • Row of LCD 0 or 1
  • Delay for speed of scroll

#include <LiquidCrystal_I2C.h> // Working with I2C LCD

char arr1[] = "Tech School";  // Char Array 1 
char arr2[] = "Scroll Function"; // Char Array 2
int col = 16;
int row = 2;
char dir=' '; // For Direction L = Left R = Right B =  Both

LiquidCrystal_I2C lcd(0x27, col, row); // Defining LCD

 

void setup() 
{
Serial.begin(9600);    // Baud Rate for Serial Communication 
lcd.init();                    // Initializing LCD
lcd.backlight();          // Enabling Back Light
// lcd.setCursor(0,1);  // For Print Single Row        
// lcd.print("Only One Row Moving");  
// For Print Single Row
}

 

void loop() 
{
int h1 = 0;                            // Variable for FOR loop 
dir = Serial.read();               // Serial Input for Direction of Scroll
if (dir == 'L' or dir == 'R' or dir == 'B')   // To Avoid Action on Garbage Input
{
for(h1 = 0;h1<2;h1++)                            // For loop for two rotations of Scroll Only
{
scroller(arr1, sizeof(arr1)-2,arr2, sizeof(arr2)-1,dir,0,300);  // Calling scroller function
// char array 1 ,  size of char array 1 , char array 2 , size of char array 2, direction L = left R = right B = both , Row and swaps for Both ,  Delay
}
}
}

 


void scroller(char array1[],int s, char array2[], int s1, char D,int r, int de)      // scroller function     
{
int n = 0;    
int m = 0;
int m1 = 0;
int sh = 0;  
int h = 0;
int k = 0;
String shift;
String shift1; 
String Blank = "                ";
int rop;
 

if (s == 0)                                // If length of array 1 is zero then copy array 2 on array 1 
{
for(h=0;h<s1+1;h++)
{  
array1[h] = array2[h];            // Copying array 2 on array 1
}
s = s1+1;
}
if (s1 == 0)                              // 
If length of array 2 is zero then copy array 1 on array 2
 
{
for(h=-1;h<s+1;h++)
{
array2[h] = array1[h];             
// Copying array 1 on array 2
 
}
s1 = s+1;
}  

if (r == 1)                                  // If r = 1 then rop = 0 for swap
{
rop = 0;
}
if (r == 0)                                  // if r = 0 then rop = 1 for swap 
{
rop = 1;  
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TOP ROW LEFT TO RIGHT BOTTOM ROW RIGHT TO LEFT
if(D == 'B')                                // For Both Directions 
{
if(s1>s)                                      //  If size of array 2 is greater than size of array 1 then variable k = size of array 2
{
k=s1;
}
else
{
k=s;                                            // Else k = size of array 1
}  
while(sh<16)                              // While sh is less than columns of LCD  do rotation
{
if(m!=k)
{  
for(n=k+1;n>0;n--)
{
shift1 = "";                                   //  Shift1 is string holding values of array 2
shift = "";                                     //   Shift is string holding values of array 1    
for(h=-1;h<m;h++)
{
if ((s) >= (n+h))
{
shift += array1[n + h];                 // Incrementing shift with addition of array 1 values one at a time     
}
}
for(h=0;h<m;h++)
{
if(h<s1)                                         // Until h is less than size of array 2
{  
shift1 += array2[h];                       // shift1 will increase with one value of array 2 at a time
}
}
lcd.setCursor(0,r);                        
lcd.print(shift);                              // Printing shift i-e values of char array 1 shifting it right     
lcd.setCursor(16-m,rop);               // 16-m is for moving left wards   
lcd.print(shift1);                            // printing shift1 moving leftwards
delay(de);
lcd.clear();
if(m<k)                                          // k is the size of biggest array from array 1 or 2 if variable m is less than k 
{
m=m+1;                                         // we will increase m by 1                     
m1 = m;                                         // and will also set m1 = m  
}
}
}
if(m==k)                                        // if this m is equal to size of greatest array  
{                                                     // This means that both the arrays are now displaying on LCD completely      
lcd.setCursor(sh+1,r);                    // No we will move whole string with complete array 1 towards right
lcd.print(shift);                               // Printing shift i-e char array1
sh = sh + 1;                                    // Increasing its positions in columns    
lcd.setCursor(15-m1,rop);             // Here we want to move our char array towards left therefore decreasing col 
if((16-m1) > 0 )                             // if col decrement is greater than zero   
{
m1 = m1 + 1;                                 // m1 will increase until it becomes zero    
lcd.print(shift1);                             //  Printing Shift 1 or array 2   
}
if((16-m1) == 0 )                           // when it becomes zero 16-m1 this means that shift1 if on extreme left on LCD     
{  
shift1.remove(0,1);                        // we will remove one variable at a time from string shift1 
lcd.setCursor(0,rop);                     
lcd.print(shift1);                            // Printing shift1 with 1 leftmost char less each time     
}
}
delay(de);
lcd.clear();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Moving Left to Right THIS IS DERIVED FROM BOTH
if(D == 'R')
{
while(sh<16)
{  
if(m!=s)
{  
for(n=s+1;n>0;n--)
{
shift = ""; 
for(h=-1;h<m;h++)
{
shift += array1[n + h];
}
lcd.setCursor(0,r);
lcd.print(shift);
delay(de);
lcd.setCursor(0,r);
lcd.print(Blank);   
if(m<s)
{
m=m+1;
}
}
}
if(m==s)
{  
lcd.setCursor(sh+1,r);
lcd.print(shift);   
sh = sh + 1;
}
delay(de);
lcd.setCursor(0,r);
lcd.print(Blank);                   
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// MOVING RIGHT TO LEFT THESE FUNCTIONS ARE DERIVED FROM BOTH 
if(D == 'L')
{
while(sh<16)
{  
if(m!=s1)
{  
for(n=s1+1;n>0;n--)
{
shift1 = "";    
for(h=0;h<m;h++)
{
if(h<s1+1)
{  
shift1 += array2[h];  
}
}
lcd.setCursor(16-m,r);
lcd.print(shift1); 
delay(de);
lcd.setCursor(0,r);
lcd.print(Blank);
if(m<s1)
{
m=m+1;
m1 = m;
}
}
}
if(m==s1)
{     
sh = sh + 1;
lcd.setCursor(15-m1,r);
if((16-m1) > 0 )
{
m1 = m1 + 1;
lcd.print(shift1);    
}
if((16-m1) == 0 )
{  
shift1.remove(0,1);   
lcd.setCursor(0,r);
lcd.print(shift1);
}
}
lcd.setCursor(0,r);
delay(de);
lcd.print(Blank);
}
}
}

NOTE : If size of character arrays increases above 15 then this might not work as expected without incorporating required changes in function. This program is written for I2C LCD you will have to make some changes if you are not using I2C LCD like using Library and for defining LCD. 


Output that you can expect from this code.



 

Comments

Popular posts from this blog

DS3231 Real Time Clock Interfacing With Arduino

FM Transmitter Project

4 Way Traffic Signal Using Flip Flops