Data Storage

When you give values to variables, the Arduino will only remember those values as long as the microcontroller has power. When you disconnect the board, your data is lost. But, as you may have guessed, there is a way to hang on to that data.


If the data you want to store never changes, then you can set up that data each time the Arduino starts. But if you need to save space you might want to use the 32K of flash memory. The PROGMEM Directive allows you to store data in the flash memory. To use it you need to include the avr/pgmspace.h library.

When using PROGMEM, you have to use certain data types. What this means is that you cannot use an array of char arrays:
#include <avr/pgmspace.h>

prog_char color_0[] PROGMEM = "Red";   
prog_char color_1[] PROGMEM = "Green";
prog_char color_2[] PROGMEM = "Blue";
prog_char color_3[] PROGMEM = "Yellow";
prog_char color_4[] PROGMEM = "Magenta";
prog_char color_5[] PROGMEM = "Cyan";
prog_char color_6[] PROGMEM = "White";

PROGMEM const char *color_table[] ={   
  color_0,color_1,color_2,color_3,color_4,color_5, color_6

char c_buffer[8];

#define btn 2

void setup(){
   pinMode(btn, INPUT);
void loop(){
        //this uses a buffer variable where the string is copied to so that it can be used as a regular char array);
        strcpy_P(c_buffer, (char*)pgm_read_word(&(color_table[random(7)])));
Using PROGMEM only works if the data is constant— not going to change during your program.

Make a toy that changes color based on some interaction.