Simple, but not geting there yet - Text strings

Hi there,

Been reading this forum for a while. Got my arduino and love it.
Got my LCD working. But now want to improve it.
I have 16 chars in my LCD, but I want to display 24... hum.. scroling I thought!

Since the text I wana scroll is in byte variables, I first must convert them in to text, and the add it to a string to scroll in the LCD, Right?

Well it's easier to say than do... :frowning: Heeelppp Please :S

#include <LCD4Bit_mod.h> 

byte second=0;
byte minute=0;
byte hour=0;
byte dayofweek=1;
byte day=1;
byte month=1;
byte year=10;


void setup(){
  
    Serial.begin(19200);
 lcd.init();
 lcd.clear();
 lcd.printIn("***LCD ON***");
 delay(200);

}



void loop(){

char* dias [] ={" Domingo"," Segunda"," Terça"," Quarta"," Quinta"," Sexta"," Sábado"};
      
//day=0; RTC returns days as 0 to 7, Sunday, Monday..etc.



char temp="";
char MESSAGE[24];

sprintf(MESSAGE, "%i:%i %c %i-%i-%i", hour,minute, dias[dayofweek],day,month,year);
//  Desired result line below
// char MESSAGE[] = "12:12 Segunda 01-10-2010";





int MESSAGE_LENGTH = sizeof(MESSAGE) - 1;
const int DISPLAY_WIDTH = 15;
int g_nPosition = 0;

  int i;
     if(g_nPosition < MESSAGE_LENGTH - DISPLAY_WIDTH)
     {
           for(i=0; i<DISPLAY_WIDTH; i++)
           {
            lcd.cursorTo(i, 0);                 
            lcd.printIn(MESSAGE[g_nPosition + i]);
           }
     }
     else
     {
           int nChars = MESSAGE_LENGTH - g_nPosition;
           for(i=0; i<nChars; i++)
           {

            lcd.cursorTo(i, 0);                 
            lcd.printIn(MESSAGE[g_nPosition + i]);
           }

           for(i=0; i<(DISPLAY_WIDTH - nChars); i++)
           {
            lcd.cursorTo(i, 0);                 
            lcd.printIn(MESSAGE[g_nPosition + i]);
           }
     }

     g_nPosition++;
     if(g_nPosition >= MESSAGE_LENGTH)
     {
           g_nPosition = 0;
     }

     delay(500);
     
}

For now waiting to get home after work and see where we stand.. eehehhe :slight_smile:

char temp="";
char MESSAGE="";

Can't assign a string to a char.. Reserve space with something like:

char MESSAGE[24];
temp=char(hour);

Casting from byte to char doesn't do much for you here (both are 8-bit, only difference is that char can be negative). Take a look at sprintf

Anyone tried this yet?

Looks to have been added in v0019 and supports substring() which would make extracting parts of the string much easier.

The listed functionality appears to be the same as the String library available for 0018.

Meh. I never even knew that existed... how much easier my recent life would have been if I only I'd known. IF ONLY...

:stuck_out_tongue:

Thank you all for the tips :slight_smile:

drhex
Sprintf is a good solution. ;D

But now, is the sintax somethig like this?
temp=sprintf(MESSAGE, "%b:%b %c %b"-"%b"-"%b", hour,minute, dias[dayoftheweek],day,month,year);

:-?

// char MESSAGE[] = "12:12 Segunda 01-10-2010"; // Desired result :slight_smile:

Does sprintf handle byte with %b as %i for int ?

%b is not a valid format specifier. Use %i.

Thanks PaulS.

Notice that I'm using a byte to adress the index of the days variable. Is this alowed? doesn't it have to be a int?

Implicit casting will be performed whenever necessary. When the cast is from a smaller data type (byte) to a larger data type (int), the cast will always succeed, with no loss of information (as long as the same modifiers (signed/unsigned, etc.) are used.