Hi. Here's a simple program:
// 100 byte test array:
const byte TestArray[] = {1,2,1,2,1,2,1,2,1,2,\
1,2,1,2,1,2,1,2,1,2,\
1,2,1,2,1,2,1,2,1,2,\
1,2,1,2,1,2,1,2,1,2,\
1,2,1,2,1,2,1,2,1,2,\
1,2,1,2,1,2,1,2,1,2,\
1,2,1,2,1,2,1,2,1,2,\
1,2,1,2,1,2,1,2,1,2,\
1,2,1,2,1,2,1,2,1,2,\
1,2,1,2,1,2,1,2,1,2,};
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte a = Serial.read();
Serial.write(TestArray[a]); // Option 1
//Serial.write(TestArray[1]); // Option 2
}
The program receives a byte ('a') from the serial connection. Option 1 and 2 are alternative lines. Option 1 uses 'a' to look up a value from TestArray[] and sends it back. Option 2 ignores 'a' and sends back the same value every time.
In option 2 the program has no need to store TestArray[], as it only ever uses one of the values it contains. The compiler appears to optimise it out completely.
Option 1 requires the program to have every value in TestArray[] available. The program has a larger compiled size, which grows further if the size of TestArray[] is increased, and so is apparently storing the array in program memory, not RAM.
My question is: The const array appears to be working exactly like a PROGMEM declaration. Is there any difference in practice between the two? If not, is there every any need to use PROGMEM to store numbers, since a const array allows more elegant and readable code?