Go Down

Topic: using char array for strings with unknown length (Read 1 time) previous topic - next topic


i did however figure a way to do it (somewhat)

There is, you know, no reason to do that.

You can print the "Welcome, " string with one call to lcd.print() and the name with another call to lcd.print(). Combining the two strings into one, to make one call to lcd.print() wastes a lot of resources, and accomplishes nothing.

there is no where in that example sketch i posted that i even do a lcd.print(welcome)

just curious then have you even looked at the rest of the sketch i posted. it scrolls the message accross the LCD from right to left, message can be longer than the lcd, and i print both messages one after the other then part of the message will roll over to second line of screen so i have to make sure i only send a total number of characters that do not exceed the lcd display length.
also Dlength can be changed to make it work on longer displays

if the message is long then there are point at which i am only lcd.print ing the middle of the message ie chars 2-18, if i am sending to strings to the routine would that not require more resources

i cannot use the built in scrolling routines of the lcd since the built in routines scroll both lines at the same time and i do not wish for both lines to scroll

I am still open to suggestions to imrove the above routine but saying there is no reason to do something and not provideing that solution for instance actually showing a modified sketch or a line to replace or the correct code to replace it with does us no good. especially when the suggestion work what so ever otherwise why even ask for suggestions.

here is the working part of the sketch

when this runs the following will be the unkowns in the sketch
cardallin =0;
Zsize = 6;
name = "Robert"

Code: [Select]
if (cardallin == 0)
    strcpy(name, "Insert Card");
    Zsize = 11;
  char welcome[(9 + Zsize)];
  strcpy(welcome,"Welcome ");
  strcat(welcome, name);
  if (delayMilliSeconds(1,200))
    lcd.setCursor(0, 0);
    for (byte CC = 0; CC < (Dlength-pass);CC++)
      lcd.print(" ");
    for (byte CC = max(0,min(Mlength,pass-Dlength)); CC < min(Mlength,pass);CC++)
    for (byte DD = 0; DD < (Dlength-(min(Mlength,pass)- max(0,pass-Dlength)+ max(0,Dlength-pass) ));DD++)
      lcd.print(" ");
    pass = pass++;
    if (pass >Mlength + Dlength)
      pass = 1;


The original message was deleted as it used more memory.

If your project and board allow for the memory required my second thought is something along the line of this untested function.

Code: [Select]

void display_message(const char* pszMessage, const char* pszName)
    int     cch = strlen(pszMessage) + 1 + strlen(pszName);
    char    buff[(3 * cch) + 1];

    char*   ptr = buff;
    memset(ptr, '\0', sizeof(buff));

    ptr    += cch;
    strcpy(ptr, pszMessage);

    ptr    += strlen(pszMessage);
    *ptr++  = ' ';

    strcpy(ptr, pszName);

    for ( int o_buff = 0; o_buff < (2 * cch); o_buff++ )
        for ( int n = 0; n < cch; n++ )
            lcd.print(buff[o_buff + n]);

Go Up