0

i have a sketch that is using very memory RAM of my Atmega2560 (is a giant project), and i need use various "println" because i have a TFT screen and i need to show data on the screen to user see. So, i'm using macro F() to use less RAM memory (example: Serial.println("Hi Naruto-kun") to Serial.println(F("Hi Naruto-kun")). But, it only work if the data entered in F() were to Const String, when i need, by example, show a array of integers, nothing work.

Example:

tft.print(F(mediaTotPg[1]));

This do not work because mediaTotPg is a Integer Array and the errors returned are:

" #define F(string_literal) (reinterpret_cast(PSTR(string_literal))) "

Finally, is possible show integers not const with Macro F()?

2 Answers 2

1

For a constant array of integers you need to use PROGMEM and the various pgm_read_*() functions.

You can read more, and find examples, on the Arduino PROGMEM manual page.

2
  • Only const array?
    – luke cross
    Jan 29, 2020 at 18:39
  • Yes. You can't change things in PROGMEM, so it has to be const. It doesn't have to be an array though, you could use single values, but that makes no sense since if they are const the compiler will optimise them out into inline literals anyway.
    – Majenko
    Jan 29, 2020 at 18:45
1

F() puts the constant string literal in the flash part of your device. This way it does not use the RAM space otherwise needed.

But your mediaTotPg array is most probably (you didn't say anything about it) a non-const variable, and can't be put in flash memory.

If the values to be shown are in fact const, you need to put them in the flash memory. As Majenko's answer points out in short, you need to use the PROGMEM section for the values, as described on the Arduino PROGMEM page.

The answer to your question is: No, it is not possible to use the macro F() with any type of variable that is not const, and it is not possible to use it for any other type than a string literal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.