Hi, as I recently learned that lcd.print function consumes some space from RAM every time it is processed, I decided to convert all my lcd.print and serial.print functions with lcd.print(F) and serial.print(F) functions. The main reason is Arduino is freezing after some time code runs(as low as 70 seconds).
My question is that it is easy to use lcd.print(F) for printing constant values on screen(Ex: lcd.print(F("0")) ) however it does not work for lcd.print(F(digits)); in below code. How can I do it?
void twoDigits(int digits) //Check if the value is single digit, add leading zero if so
{
if(digits < 10)
{
lcd.print(F("0")); //This works well with F function
}
lcd.print(F(digits)); //This does not work
}
void displayDate() //Display Date
{
twoDigits(day());
lcd.print('/');
twoDigits(month());
lcd.print('/');
twoDigits(year());
lcd.print(' ');
lcd.print(daysOfTheWeek[weekday()]);
}
void displayTime() //Display Time
{
twoDigits(hour());
lcd.print(':');
twoDigits(minute());
lcd.print(':');
twoDigits(second());
}
This twodigits function is called at least 6 times a second in my loop when I want to show date and time. I was imagining that using F macro would decrease RAM usage.
I know this is true if you want to print a word or a sentence via lcd or serial, I tried it and see that it increases memory use a little bit while decreasing ram usage.
Just wanted to do the same for wherever twodigits function is used.
The string that you put inside the F() macro to print must be known at compile time. The value of a variable passed to a function as a parameter is obviously not known at compile time. Therefore, you can' do this.
gunaygurer:
This twodigits function is called at least 6 times a second in my loop when I want to show date and time. I was imagining that using F macro would decrease RAM usage.
I know this is true if you want to print a word or a sentence via lcd or serial, I tried it and see that it increases memory use a little bit while decreasing ram usage.
Just wanted to do the same for wherever twodigits function is used.
how it is important how often the function is called?
The reason why I think it would worth using it is because I was guessing that every time you print something it fills your buffer and it might overflow after some time.