Hi, I'm in process of lowering memory footprint of little program of mine, main culprit being that I have need of quite a few arrays.
I thought I could put arrays into progmem, and then reading them with pgm_read_byte_near() as needed.
Example of array:
int shiftTimeMap[14][12] PROGMEM {
{999, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }, //shift pressure %
//-----------------------------------------------------------------------
{ -20, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 900, 800, 800 },
{ -10, 1100, 1100, 1100, 1100, 1100, 1100, 1100, 900, 900, 800, 800 },
{ 0, 1100, 1100, 1100, 1100, 1100, 1100, 900, 900, 900, 800, 800 },
{ 10, 1100, 1100, 1100, 1100, 1100, 1100, 900, 900, 900, 800, 800 },
{ 20, 1100, 1100, 1100, 1000, 1000, 1000, 900, 900, 900, 800, 800 },
{ 30, 1100, 1100, 1100, 1000, 1000, 900, 900, 800, 700, 700, 600 },
{ 40, 1000, 1000, 1000, 1000, 900, 800, 700, 700, 700, 500, 500 },
{ 50, 1000, 1000, 1000, 900, 900, 800, 700, 700, 500, 500, 500 },
{ 60, 1000, 1000, 1000, 900, 800, 700, 600, 500, 450, 450, 450 },
{ 70, 1000, 1000, 900, 900, 800, 700, 600, 500, 450, 320, 320 },
{ 80, 1000, 900, 900, 800, 800, 700, 600, 500, 320, 300, 300 },
{ 90, 1000, 900, 800, 800, 800, 700, 600, 500, 320, 300, 300 },
{ 100, 1000, 900, 800, 800, 800, 700, 600, 500, 320, 300, 300 }};
//oil temp
and I have general purpose readMap() function to fetch the data from array:
int readMap(int theMap[14][12], int x, int y) {
int xidx = 0; // by default near first element
int xelements = LEN(theMap[0]);
int distance = abs(pgm_read_byte_near(&theMap[0][xidx]) - x);
for (int i = 1; i < xelements; i++)
{
int d = abs(pgm_read_byte_near(&theMap[0][i]) - x);
if (d < distance)
{
xidx = i;
distance = d;
}
}
int yidx = 0; // by default near first element
int yelements = LEN(*theMap);
distance = abs(pgm_read_byte_near(&theMap[yidx][0]) - y);
for (int i = 1; i < yelements; i++)
{
int d = abs(pgm_read_byte_near(&theMap[i][0]) - y);
if (d < distance)
{
yidx = i;
distance = d;
}
}
int mapValue = pgm_read_byte_near(&theMap[yidx][xidx]);
return mapValue;
}
I still fail to see more free dynamic memory for some reason.
Have I understood something wrong with PROGMEM or does this make any sense whatsoever?
Thanks for your guidance:)