Reading/Writing to multi-dimensional Array from multiple functions

Hello!

I am writing a program that will read&store analog values into a multi-dimensional array on an ArduinoMega.

I will be reading from this array as well to display onto an LCD, i have not written this function yet.

I dont have too much experience coding c/c++ so I am trying to figure out of my logic of accessing the array is correct.

Here is what my code currently is:

  // Make a Multi-demensional Array!
  // rawData[3][3] = { {V1,C1,T1},
  //                   {V2,C2,T2},
  //                   {V3,C3,T3} }
int rawData[3][3];

void setup() {
  //initialize the Storage arrays for reading data
}

void loop() {

  int getSensorData() {
    //Nested for Loop, going to go across 
    int k = 0;            //count for looping through analog pins
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        rawData[i][j] = analogRead(k);
        k++;

      }
    }
  }
}

I currently have rawData[3][3] being initialized outside of void setup(), I was reading that online you need to initialize outside of the main functions for it to be global.

I was thinking that there was maybe someway for me to pass the rawData into the getSensorData() function, but i just dont know.

Any help on general programming structure here would be great.

Thank you in advance.

Oops

Hey, not sure what you mean here

I mean four into three don't go.

Oh like it should be: i < 3, thanks

Any comment on how the function works in general? Im I correct to initialize it outside of the setup loop?

I'm not sure what you're asking, but rawData contains all zeroes before setup runs.

getSensorData appears to be defined inside the loop function, which isn't allowed.

You're only ever going to read from analogue pin A0.

yea i just realized that mistake, im going to edit this post with the updated code in a minute. Thanks for the feedback

Which one?

all 3

A: You can't define getSensorData() inside loop().

B: 'int getSensorData()' means it returns an int value but you don't have a 'return' statement to return a value. Either change the return type to 'void' or add a 'return' statement with the value to return.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.