trying to create an autonomous function on simple LCD scroll

Hi!
as I'm starting with arduino, I'm stuck on every inch of C language code!
This time I'd like to build a simple scroll and clean up this code, moving one line out of the loop function without success.

Any suggestion? (i'm surely missing some important basic concept)

#include <LiquidCrystal.h>

long previousMillis = 0;
unsigned long currentMillis = 0;
String str = "Hello this text should be scrolling";
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void lcd_print(String str=""){
  lcd.clear();
  lcd.setCursor(0, 0); lcd.print(str.substring(0,8));
  if(str.length()>8){
    lcd.setCursor(0, 1); lcd.print(str.substring(8,16));
  }
}

void lcd_scroll(String reqstr="", long interval=500){
  if(currentMillis - previousMillis > interval) {
    str = reqstr.substring(1,reqstr.length()) + reqstr.substring(0,1);
    lcd_print(str);
    previousMillis = currentMillis;
  }
  //lcd_scroll(str, interval); // <- would like uncomment this
}

void lcd_start_scroll(String reqstr="", long interval=500, int initDelay = 0){
  if(reqstr.length() > 8){
    str = reqstr + " - ";
    lcd_print(str); delay(initDelay);
    lcd_scroll(str, 300);
  }
}

void setup() {
  lcd.begin(16, 2);
  //delay for 1 sec then start scrolling
  lcd_start_scroll(str, 300, 1000);
}

void loop() {
  currentMillis = millis();
  lcd_scroll(str, 300); // <- would like to remove this
}

If you call lcd_scroll() from inside lcd_scroll(), you create a recursive situation. As designed, there is no limit to the recursive depth. Quickly you will consume all stack space and crash.

If that's what you want to do, feel free to uncomment that line.

Otherwise, some comments in that function that describe what you think you are doing would be useful.

It appears that you pass a string to the function, then disassemble and reassemble that string in a global variable. Since the string as shown at any given time is of interest only to the function that set it up (lcd_scroll) and the function that displays it (lcd_print, which gets the string as an argument), there is no reason for that string to be global.

When do you want to stop scrolling?

Hello PaulS,
As you pointed I'd like to have a recursive function I could call any time in any part of my code (endless loop is fine for now).
I tried clieaning up this code simplifying at minimum the code for testing (now no LCD involved) and no global string declaration..
But still, there is some sort of buffer overflow occures, and it never calls "I'm here". How come this?
Adit

long previousMillis = 0;
unsigned long currentMillis = 0;

void lcd_scroll(String reqstr="", long interval=500){
  // why almost every 30 times this writes something like "currentMillisµ¬?É?¹Ñ5¥±±¥Í5" ?
  // Some sort of buffer overflow?
  Serial.println("currentMillis");
  if(currentMillis - previousMillis > interval) {
    Serial.println("i'm here"); // why this is never called?
    previousMillis = currentMillis;
  }
  lcd_scroll(reqstr, interval);
}

void setup() {
  Serial.begin(9600);
  lcd_scroll("Hello this text should be scrolling", 300);
}

void loop() {
  currentMillis = millis();
  delay(100);
}

I really don't think recursion is the answer. Recursion is useful when there is a way to end the recursion, and return a value.

For instance, If I ask you what 6! is, the answer is easy. It's 6 * 5!. So, what is 5!? That's easy. It's 5 * 4!. This keeps going until you get to what is 2!? That's easy. It's 2 * 1!. The special case of 1! = 1 provides an end to the recursion.

In your case, there is no limit to the recursion depth. You just keep calling the function over and over, until you run out of stack space.

Define what you want lcd_scroll to do, in English, not C/C++. Then, we can figure out how to write the code to do that.