Need help understanding analogread into arrays

Hi guys! I'm relatively new to the arduino platform and recently I've been trying to store the analogRead values into an array, and based on the array I aim to perform some calculations.

However I realised the values that I'm receiving from the analogRead and the values that I get from the array is different. Is it possible that I've missed out something crucial? I believe that my code to store the analog values into the array should be correct, but since I'm unable to see the same values when I access the array could this be caused by any hardware limitations of the Arduino?

I've attached my codes below and I apologize in advance if this seems to be a noob-ish question :stuck_out_tongue:

Thanks!

const int numReadings = 500;
int threshold = 500;
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int inputPin = A0;


void setup() {
  Serial.begin(9600);        // initialize serial communication with computer:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) 
  {
    readings[thisReading] = 0;    //   initialize all the readings to 0:
  }
}

void loop() {
  
  readings[readIndex] = analogRead(inputPin);   // read from the sensor

  Serial.println(readings[readIndex]);

 
  readIndex = readIndex + 1;       // advance to the next position in the array

  if (readIndex >= numReadings) // if we're at the end of the array...
  {
    for(int i=0;i<readIndex;i=i+1)
    {
      Serial.println(readings[i]);
    }
    readIndex = 0;     // ...wrap around to the beginning:
    delay(2000);
  }
 
}

The code that your presented works; for testing, I did limit the number of readings to 10 and increased the delay to 10 seconds so one has the time to compare the numbers :wink:

However, 500 integers is 1 kB of SRAM space. If other code requires lots of (or big) variables, you might run into problems (at least on a 328 based board).

Yes. I have the same question as to the poster. Do you have any ideas that can make it work for an array whose length is 500?

I suggest you post your code.

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