Looping through a multidimensional array

Hi.

I want to loop through a multidimensional array using the following code:

#include <avr/pgmspace.h>

prog_uint16_t tempVals[][2] PROGMEM = {{11,12},{13,14},{15,16},{17,18}};
int i = 0;


void setup()
{
   Serial.begin(9600); 
}

void loop()
{
    for(i = 0; i < 4; i++)
    {
        Serial.print(tempVals[i][0]);
        Serial.print(" :: ");
        Serial.println(tempVals[i][1]);
    }
}

Yet what I am getting in the serial monitor is:

771 :: 0
0 :: 0
0 :: 1
0 :: 0

Can anyone clue me into what I am doing wrong?

Thanks.

Louis

Instead of

Serial.print(tempVals[i][0]);

try

Serial.print(pgm_read_word(&tempVals[i][0]);

Cheers.

That worked perfectly! Thanks!

The problem was using progmem to store the array. If I had stored the array in regular SRAM, it would not have been a problem. (Just noting for future readers)

The Flash library by Mikal Hart removes almost all of the pain and adds some functionality:

/*
  flash_array_ints_example.pde
  Using the Flash library from http://arduiniana.org/libraries/flash/
  Copyright (C) 2009 Mikal Hart

  This example by davekw7x
 */

#include <Flash.h>

/*
  Use of Flash library for 2-D array of ints
 */

// A FLASH_TABLE is a 2-D array in program memory
FLASH_TABLE(
         int,  //<---Data type
    intTable,  //<-- Name of the 2-D array
           2,  //<-- You have to tell it the number of columns
    {11, 12},  //<-- Row 0
    {13, 14},  //<-- Row 1
    {15, 16},  //<-- Row 2
    {17, 18}   //<-- Row 3
);

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    // Show the geometry of the table
    Serial.print("Number of Rows: "); 
    Serial.println(intTable.rows());
    Serial.print("Number of Cols: "); 
    Serial.println(intTable.cols());
    Serial.println();

    //Print out the entire table
    Serial.print("The entire table in one swell foop:");
    Serial.println();

    intTable.print(Serial); // Print the entire 2-D array

    Serial.println();

    // Access individual rows using [] notation
    Serial.println("The rows of the table:");
    for (int i = 0; i < intTable.rows(); i++) {
        Serial.print("Row [");Serial.print(i);Serial.print("]: "); 

        intTable[i].print(Serial); // Print this entire row

        Serial.println();
    }
    Serial.println();

    
    // Access individual elements using [][] notation:
    Serial.println("The elements of the table:");
    for (int i = 0; i < intTable.rows(); i++) {
        for (int j = 0; j < intTable.cols(); j++) {

            int x = intTable[i][j]; // Note normal 2-D array notation

            Serial.print("intTable[");Serial.print(i);
            Serial.print("][");Serial.print(j);Serial.print("] = ");
            Serial.println(x);
        }
        Serial.println();
    }

    delay(10000);
}

Output:


Number of Rows: 4
Number of Cols: 2

The entire table in one swell foop:
11,12
13,14
15,16
17,18

The rows of the table:
Row [0]: 11,12
Row [1]: 13,14
Row [2]: 15,16
Row [3]: 17,18

The elements of the table:
intTable[0][0] = 11
intTable[0][1] = 12

intTable[1][0] = 13
intTable[1][1] = 14

intTable[2][0] = 15
intTable[2][1] = 16

intTable[3][0] = 17
intTable[3][1] = 18

Regards,

Dave

The problem was using progmem to store the array. If I had stored the array in regular SRAM, it would not have been a problem.

The two memory spaces (RAM and program memory) have completely different address spaces. You have to use program memory address space instructions to access anything stored on program memory.