From 5aefed2f4b0db099cf14d7db92778c58c652c95b Mon Sep 17 00:00:00 2001 From: LeisureLadi <33247104+LeisureLadi@users.noreply.github.com> Date: Wed, 4 Dec 2019 13:14:14 +0100 Subject: [PATCH] Update PROGMEM.rst (#6872) * Update PROGMEM.rst Include array of strings in the description, since the examples in Arduino do not consider the right pointer size * Update PROGMEM.rst --- doc/PROGMEM.rst | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/doc/PROGMEM.rst b/doc/PROGMEM.rst index eb4402806..2b8138f6a 100644 --- a/doc/PROGMEM.rst +++ b/doc/PROGMEM.rst @@ -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 ----------