I am adding several menus to a program that print serially (Bluetooth) on my PC using a simple terminal app. This included lots of Serial.println("xxxx") commands with menu options and some old school ascii art. I compiled it and saw that my memory for variables was 80% used. Took me a bit to figure out it was those serial prints. I did discover that changing
Serial.println("xxxx")
to Serial.println(F("xxxx"))
eliminated the problem.
So, can someone explain the difference? I know they are using memory differently but how?
Thanks, and I hope this discovery helps someone else out. I've not seen it on this site, though it is possible I just missed it.
Joe
The F() macro makes sure the constant literal strings stay in program memory, instead of being copied into RAM.
TheMemberFormerlyKnownAsAWOL:
The F() macro makes sure the constant literal strings stay in program memory, instead of being copied into RAM.
OK. So instead of going to RAM, where data is constantly changing, the "xxxx" information stays with the body of the program. Got it. Thanks!
Joe
Detailed explanation in tutorial:
The F macro is pointer to a char type in flash memory. Compiler recognize it an use overloaded function in appropriate type which takes the string from flash.
Normally, string constants are loaded from flash to RAM at program start. It is standard C/C++ behavior. They are prepared for use. The F macro is string using just in time.
So it may be "slower" to print than if not using the macro? Even if that is the case I doubt I'd notice it at 9600 baud.
Thanks again and also for the link that I missed. Or more likely didn't understand what it was or was for at the time I saw it
Joe
Set up your project to access an SD card. (pretty easy to do) Then you could store a couple gig of ascii art and jut read 'em out line at a time.
-jim lee
I don't have a lot of ascii art, just an opening graphic with the robot name and a picture of a robot, like a logo, about 20 lines long. Then I have a block for the commands menu which is about 15 lines, and assorted feedback prints for receipt of commands and what the bot is doing when running autonomously. I was shocked at how much memory that did use though. The SD card is a great idea though if I add more or want to start logging data.
Thanks
JoeWillson:
So it may be "slower" to print than if not using the macro? Even if that is the case I doubt I'd notice it at 9600 baud.
...
Yes, it is slower because of slower memory and more of instructions. However, it is not so time critical in compare to even much more bauds than 9600.