# LCD Arduino Looping, Problem with Algorthm

Hi all, I stuck at making this code into loop function. I am thinking of 2 Dimensional Loop but still I have no idea of making that.

This is my code:

``````  delay(500);

lcd.setCursor(0, 0);
lcd.print(test[test.length() - 1]);
delay(500);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print(test[test.length() - 2]);
lcd.setCursor(1, 0);
lcd.print(test[test.length() - 1]);
delay(500);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print(test[test.length() - 3]);
lcd.setCursor(1, 0);
lcd.print(test[test.length() - 2]);
lcd.setCursor(2, 0);
lcd.print(test[test.length() - 1]);
delay(500);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print(test[test.length() - 4]);
lcd.setCursor(1, 0);
lcd.print(test[test.length() - 3]);
lcd.setCursor(2, 0);
lcd.print(test[test.length() - 2]);
lcd.setCursor(3, 0);
lcd.print(test[test.length() - 1]);
delay(500);
lcd.clear();
``````

The goal is I want to make simple scrolling text like this.

t st est Test Test Test t Tes st Te est T

Thank you, if you are willing to help me :)

two loops, one nested within the other will be one solution.
Also you need to decide what scrolling format you want, your format would add complexity to an otherwise simple set of loops.

something like:

t
st
est
Test
Test
Test
Tes
Te
T

or

t Tes
st Te
est T
Test
Test
Test
t Tes
st Te
est T

have a read of circular buffers and different types of loops.
There should be many examples out there you can study.

you better shift the characters in the array and do just one print, far more efficient. something like this

``````char test[8] = "test ";  // char arrays are more efficient than strings..

char t = test[0];
for (int i=0; i< strlen(test)-2; i++) test[i] = test[i+1];
test[length-1] = t;

lcd.setCursor(0, 0);
lcd.print(test);
``````

The goal is I want to make simple scrolling text like this.

Have you considered using the inherent ability of the LCD controller to do this type of shifting?

For example, this part is done by dealing with the entry mode:

``````t
st
est
Test
``````

and this part is done by shifting (scrolling in Arduinospeak) the display:

``````Test
Test
Test
``````

Much, if not all, of this capability is implemented in the LiquidCrystal library.

Don

I have observed all of your solution. and came out with this code…

If the String less than the lcd display.

``````#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
String test = "Test";

void setup(){
lcd.begin(16, 2);
Serial.begin(9600);
}

void loop(){
for(int m = 0; m < 16; m++){
for(int i = 0; i < test.length(); i++){
lcd.setCursor(m + i, 0);
lcd.print(test[i]);

for(int a = 0; a < test.length() - 1; a ++){
if(m >= 16 - test.length() + a){
lcd.setCursor(m - (16 - test.length()) - a, 0);
lcd.print(test[i - a]);
}
}
}
delay(250);
lcd.clear();
}
}
``````

If the length of the String exceed the lcd display…

``````#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
String test = "Lalalalallaala";

void setup(){
lcd.begin(16, 2);
Serial.begin(9600);
}

void loop(){
for(int m = 0; m < test.length() + 2; m++){
for(int i = 0; i < test.length(); i++){
lcd.setCursor(m + i, 0);
lcd.print(test[i]);

for(int a = 0; a < test.length() - 1; a ++){
if(m >= test.length() + 2 - test.length() + a){
lcd.setCursor(m - (test.length() + 2 - test.length()) - a, 0);
lcd.print(test[i - a]);
}
}
}
delay(250);
lcd.clear();
}
}
``````

The problem if the String to long it will show up chinesse letter or something…

In your loop() function, will the length of the String ever change? If not, call test.length() once, and store the value in a variable.

Then, add Serial.print() statements to understand what is happening in each of the three for loops. Personally, I think it would be better to use some functions, and test the functions independently.

PaulS: In your loop() function, will the length of the String ever change? If not, call test.length() once, and store the value in a variable.

Then, add Serial.print() statements to understand what is happening in each of the three for loops. Personally, I think it would be better to use some functions, and test the functions independently.

Yes it will change because it will read data from some sensors. And also yes I put the code in lcd handler function in actual code.