Skip to content

Update PROGMEM.rst #6872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions doc/PROGMEM.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,42 @@ the value back.
}
}

How do I declare Arrays of strings in PROGMEM and retrieve an element from it.
------------------------------------------------------------------------------

It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is actually an example of a two-dimensional array.

These tend to be large structures so putting them into program memory is often desirable. The code below illustrates the idea.

.. code:: cpp

// Define Strings
const char string_0[] PROGMEM = "String 0";
const char string_1[] PROGMEM = "String 1";
const char string_2[] PROGMEM = "String 2";
const char string_3[] PROGMEM = "String 3";
const char string_4[] PROGMEM = "String 4";
const char string_5[] PROGMEM = "String 5";

// Initialize Table of Strings
const char* const string_table[] PROGMEM = { string_0, string_1, string_2, string_3, string_4, string_5 };

char buffer[30]; // buffer for reading the string to (needs to be large enough to take the longest string

void setup() {
Serial.begin(9600);
Serial.println("OK");
}

void loop() {
for (int i = 0; i < 6; i++) {
strcpy_P(buffer, (char*)pgm_read_dword(&(string_table[i])));
Serial.println(buffer);
delay(500);
}
}


In summary
----------

Expand Down