I have a code that fetches tweets from Twitter and displays it on my LCD. the problem is i have a 16x2 LCD so i can only display a maximum of 32 characters.
how do i change my tweet from my program to a string of 16 characters per line? tried a lot of methods but it's not working. will post my working program code out so i can get some help.
StartHere is an array of char variables. How would you go about printing (to the LCD) the 1st 16 of those 1 at a time? And then the next 16?
Or you could torture yourself and your code by setting up extra smaller char arrays then put 16 chars in one and the rest in the next and call that efficient or (Apple-talk) "elegant" while in fact it would be neither.
Given your C++ String class use, I'd say your next probable problem will show up not long after you fix this one. That problem is the incompatibility between Arduino RAM limit and the C++ String class written to take advantage of computers having lots and lots of RAM.
basically right now my code takes the whole tweet which is to a max limit of 140 chars. i want it to take the first 32 chars, dispose the rest off and split the 32 into two lines of 16.
You can view string functions as complicated black boxes or you can know that arrays are simple and work from there. The things you need to program around are bounds limits (don't write 20 chars to a 16 char array) and where the terminating zero is, which you can use strlen() or check each char and check if it's zero.
char StartHere[ 142 ]; // make as a global, stuff your incoming string here
....
// -after- filling StartHere with the twitter data, if the length is < 32 chars, pad to 32 with spaces
if ( strlen( StartHere ) < 32 )
{
for ( byte i = strlen( StartHere); i < 32; i++ )
{
StartHere[ i ] = ' ';
}
}
// you could do this or you can check lengths during print to LCD which would be more efficient
// hey, this is just example anyway --- I would use pointers but array indexes work as well.
....
// so now that we KNOW that StartHere has 32 characters even if the last few are blank spaces...
setCursor(0,0);
// lcd.println(string1);
for ( byte i = 0; i < 16; i++ )
{
lcd.print( StartHere[ i ] );
}
setCursor(0,1);
//lcd.println(string2);
for ( byte i = 16; i < 32; i++ )
{
lcd.print( StartHere[ i ] );
}
// mostly I just want you to see that C string arrays are nothing but arrays of chars.
// inside string functions they all deal with 1 char at a time, that's how the hardware works.