你应该用 F() 将文本存储在FLASH存储器中,你无法避免这种情况。
F()
您可以定义宏:
#define FPRINT(x) print(F(x)) Serial.FPRINT("text");
甚至是这样的:
#define SFPRINT(x) Serial.print(F(x)) SFPRINT("test");
当然,你可以更换 FPRINT 你想要的任何东西都没有预先定义(在这种情况下你会得到编译器警告)。
FPRINT
你也可以使用 printf_P 功能来自 <stdio.h> 和 PSTR 来自的宏 <avr/pgmspace.h> (默认情况下,它们应包含在Arduino IDE中的程序中)。
printf_P
<stdio.h>
PSTR
<avr/pgmspace.h>
存储在RAM中的文本的典型用法:
int a = 5; printf("This is my variable: %d", a);
结果:
Sketch uses 1946 bytes (6%) of program storage space. Global variables use 39 bytes (1%) of dynamic memory
与存储在FLASH中的文本一起使用:
int a = 5; printf_P(PSTR ("This is my variable: %d"), a);
Sketch uses 1958 bytes (6%) of program storage space. Global variables use 15 bytes (0%) of dynamic memory