Which space (RAM or Flash) to choose for array initialisation?

  1. unsigned char myArray[ ]= {0x3F, 0x6B}; creates a read/write array in RAM with execution time
    #2 Clock cycles.

  2. unsigned const char myArray[] = {0x3F, 0x6B}; creates read only array in Flash with execution time
    #3 Clock cycles.

Which one of the above should I choose?

No, #2 won't do what you think it does. On AVRs you need to use the PROGMEM attribute to put variables in Flash.

Will you ever change the values in the array while the program is running?

If yes, RAM or EEPROM.

If no, Flash or EEPROM.

The following codes are not compiled. The error message is:
exit status 1
assignment of read-only location 'lupTab[0]'

So, I have concluded that the array must have been initialised in Flash (EEPROM Code Memory). It is possible to read byte data from this 16-bit organised memory using LPM instruction. SPM allows erasing and then writing in Flash.

But, in EEPROM (Data Memory) we can perform byte oriented data read/write operation using program instructions. Therefore, my belief is this: unsigned const char lupTab[] creates protected array in Flash and not in EEPROM.

unsigned const char lupTab[] = {
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71
};
void setup()
{

}

void loop()

{

lupTab[0] = 0x40; // an attempt to change the content of location lupTab[0]

}

That will get a compile error, yes, because in C it is invalid to assign a value to a variable declared as const. Regardless of what you're compiling for, or what memory types it has available to it.

This has no bearing on where it is stored - since it is not declared PROGMEM it will be in RAM (of course, it's stored in flash too, so it can initialize it to that value).

In order to obtain a clearer picture of the storage area (RAM or Flash or EEPROM) of the variables in AVR programming, I have executed few codes on ArduinoUNO board. The results tell:

  1. (Header File avr/pgmspace.h, keywords PROGMEM and const are not included)
    The variable is initialized in Flash.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
lcd.begin(16, 2); //2-line 16 chracters
lcd.clear();
lcd.print(0,1); // cursor position
unsigned int x =0x34;
unsigned int y1, y2;
unsigned int *z;
//--------------
y1 = &x;
lcd.print(y1); // LCD shows 2298; It is a Flash Space
lcd.print(' ');
lcd.print(x, 16); // LCD shos: 34
lcd.print(' ');
//--------------
x = 0x56;
z = &x;
y2 = *z;
lcd.print(y2, 16); // LCD shows: 56
//----------------
}

void loop()
{

}

  1. (Header File avr/pgmspace.h, keyword PROGMEM are not included. Keyword const is included)
    The variable is initialized in Flash. It can be accessed, but it can't be modified. However, using
    pointer the value of the vaiable can be modified!

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
lcd.begin(16, 2); //2-line 16 chracters
lcd.clear();
lcd.print(0,1); // cursor position
unsigned const int x =0x34;
unsigned int y1, y2;
unsigned int *z;
//--------------
y1 = &x;
lcd.print(y1); // LCD shows 2298; It is a Flash Space
lcd.print(' ');
lcd.print(x, 16); // LCD shos: 34
lcd.print(' ');
//--------------
//x = 0x89; not allowed
z = &x;
*z = 0x89;
y2 = *z;
lcd.print(y2, 16); // LCD shows: 89
//----------------
}

void loop()
{

}

  1. (Header File avr/pgmspace.h, keywords PROGMEM and const are included)
    The variable is initialized in Flash. It can be accessed and modified using pointer.

#include <avr/pgmspace.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
lcd.begin(16, 2); //2-line 16 chracters
lcd.clear();
lcd.print(0,1); // cursor position
unsigned const int x PROGMEM =0x34;
unsigned int y1, y2;
unsigned int *z;
//--------------
y1 = &x;
lcd.print(y1); // LCD shows 2298; It is a Flash Space
lcd.print(' ');
lcd.print(x, 16); // LCD shos: 34
lcd.print(' ');
//--------------
// x = 0x89; // not allowed
z = &x;
*z = 0x89;
y2 = *z;
lcd.print(y2, 16); // LCD shows: 89
//----------------
}

void loop()
{

}

  1. Is there anyway to initialise variable in RAM in the Arduino IDE?
  1. Yes, do that in the code before setup()
    unsigned long timeDuration = 300UL;
    for example. Can be 300ms (0.3s) or 300uS (0.3mS)