I was trying something bigger when I hit a wall trying to 2D index PROGMEM.
The results prints out to Serial Monitor, tested on an UNO.
I don’t really know why the first method does not work, hoping that someone does.
Being able to index as the first way tries would be more beginner friendly.
byte flashData; // to read one table entry into at a time
byte indexI, indexJ; // these will do the job of 2 nested for-loop indexes
const byte PROGMEM PROGTBLDIMENS[ 2 ] = // table size and table both kept in flash
{
10, 10
};
const byte PROGMEM PROGTBL[ 10 ][ 10 ] = // this example data made for easy-check results
{
1, 11, 21, 31, 41, 51, 61, 71, 81, 91,
2, 12, 22, 32, 42, 52, 62, 72, 82, 92,
3, 13, 23, 33, 43, 53, 63, 73, 83, 93,
4, 14, 24, 34, 44, 54, 64, 74, 84, 94,
5, 15, 25, 35, 45, 55, 65, 75, 85, 95,
6, 16, 26, 36, 46, 56, 66, 76, 86, 96,
7, 17, 27, 37, 47, 57, 67, 77, 87, 97,
8, 18, 28, 38, 48, 58, 68, 78, 88, 98,
9, 19, 99, 39, 49, 59, 69, 79, 89, 99,
10, 20, 30, 40, 50, 60, 70, 80, 90, 100
};
void setup()
{
Serial.begin( 115200 ); // getting the serial queue to empty quicker
Serial.println( "\n Example of 2D Data Iteration\n" );
Serial.println( " Trying by indexing PROGMEM directly, doing something wrong!" );
for ( indexJ = 0; indexJ < 10; indexJ++ )
{
for ( indexI = 0; indexI < 10; indexI++ )
{
flashData = pgm_read_byte( PROGTBL[ indexJ ][ indexI ] );
Serial.print( flashData, DEC );
Serial.print( " " );
if ( ! (( indexI + 1 ) % 10 )) Serial.println( );
}
}
Serial.println( "\n" );
// PGM_P Addr; // define a const prog_char pointer named Addr
// Addr = (const prog_char *) PROGTBL; // cast the table address to fit Addr
Serial.println( " Using pointer into PROGEMEM works" );
for ( indexJ = 0; indexJ < 10; indexJ++ )
{
for ( indexI = 0; indexI < 10; indexI++ )
{
flashData = pgm_read_byte( (const prog_char *) PROGTBL + indexI + indexJ * 10 );
Serial.print( flashData, DEC );
Serial.print( " " );
if ( ! (( indexI + 1 ) % 10 )) Serial.println( );
}
}
Serial.println( "\n" );
}
void loop()
{
}