constdataTypevariableName[]PROGMEM={};// use this form
constPROGMEMdataTypevariableName[]={};// or this one
constdataTypePROGMEMvariableName[]={};// not this one
// save some unsigned ints
constPROGMEMuint16_tcharSet[]={65000,32796,16843,10,11234};// save some chars
constcharsignMessage[]PROGMEM={"I AM PREDATOR, UNSEEN COMBATANT. CREATED BY THE UNITED STATES DEPART"};unsignedintdisplayInt;charmyChar;voidsetup(){Serial.begin(9600);while(!Serial);// wait for serial port to connect. Needed for native USB
// put your setup code here, to run once:
// read back a 2-byte int
for(bytek=0;k<5;k++){displayInt=pgm_read_word_near(charSet+k);Serial.println(displayInt);}Serial.println();// read back a char
for(bytek=0;k<strlen_P(signMessage);k++){myChar=pgm_read_byte_near(signMessage+k);Serial.print(myChar);}Serial.println();}voidloop(){// put your main code here, to run repeatedly:
}
/*
PROGMEM string demo
How to store a table of strings in program memory (flash),
and retrieve them.
Information summarized from:
http://www.nongnu.org/avr-libc/user-manual/pgmspace.html
Setting up a table (array) of strings in program memory is slightly complicated, but
here is a good template to follow.
Setting up the strings is a two-step process. First define the strings.
*/#include<avr/pgmspace.h>constcharstring_0[]PROGMEM="String 0";// "String 0" etc are strings to store - change to suit.
constcharstring_1[]PROGMEM="String 1";constcharstring_2[]PROGMEM="String 2";constcharstring_3[]PROGMEM="String 3";constcharstring_4[]PROGMEM="String 4";constcharstring_5[]PROGMEM="String 5";// Then set up a table to refer to your strings.
constchar*conststring_table[]PROGMEM={string_0,string_1,string_2,string_3,string_4,string_5};charbuffer[30];// make sure this is large enough for the largest string it must hold
voidsetup(){Serial.begin(9600);while(!Serial);// wait for serial port to connect. Needed for native USB
Serial.println("OK");}voidloop(){/* Using the string table in program memory requires the use of special functions to retrieve the data.
The strcpy_P function copies a string from program space to a string in RAM ("buffer").
Make sure your receiving string in RAM is large enough to hold whatever
you are retrieving from program space. */for(inti=0;i<6;i++){strcpy_P(buffer,(char*)pgm_read_word(&(string_table[i])));// Necessary casts and dereferencing, just copy.
Serial.println(buffer);delay(500);}}