Storing Real Time Accelerometer Data in Array

Hello,

I want to store 128 real-time readings of the z axis of an accelerometer into an array, and then print the array in the serial monitor. This is just a test before I manipulate the array with other calculations later. However, my current code below shows only constant numbers in the array that don't change.

#include <Wire.h>
#include <Adafruit_MSA301.h>
#include <Adafruit_Sensor.h>

Adafruit_MSA301 msa;
  

  
  void setup()
  {
  Serial.begin(115200);
  Serial.println("Ready");
  }
  
  void loop(){
  sensors_event_t event; 
  msa.getEvent(&event);
  
  float accelDataArray[128];  
  for(int i = 0; i < 128; i++)
{
  accelDataArray[i] = float(event.acceleration.z);
  Serial.println(accelDataArray[i]);
}

  delay(2000);
  
  }

And this is the result (it goes on forever):

0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38
0.38

You're not modifying or updating 'event.acceleration.z' in the for loop. So of course, it always has the same value.

Also, why are you casting it to a float?

Thanks. How would I change the code so that the acceleration reading updates in the array?

I used the float because I read that the float command is for numbers with decimal points, and acceleration is usually not an integer.

needhelpwitharduino:
Thanks. How would I change the code so that the acceleration reading updates in the array?

I used the float because I read that the float command is for numbers with decimal points, and acceleration is usually not an integer.

What float command, do you mean the explicit cast to float? It is not a "command". Can you supply a link to that deeply mistaken source? Isn't the variable already a float type? I think you will find that it is.

Okay, how would you change it, you ask? That is kind of like pointing at a pile of parts and bolts and asking, "how can I change it so that it's an automobile". Please, first make an attempt at fixing what I told you about, and then test and repost it if it still doesn't work. You are going to be the one to do the labour.

Engage your mind and consider what I said. On each pass, you can't use the same data. Obviously then, it has to be updated. How is it updated? I don't know, I didn't look. You should know if you wrote it. I have the impression (although I could be wrong) that it should come from the sensor.

How would I change the code so that the acceleration reading updates in the array?

First, go through your code and make sure that you understand what every line does.

Then, think about where you would best make use of a function call like:

  msa.getEvent(&event);

Finally, since the loop function loops, there is actually no need for a "for loop".

jremington:
Finally, since the loop function loops, there is actually no need for a "for loop".

Are you sure? The loop() function loops indefinitely, but the array is only 128 elements long. It's true that at the moment, nothing seems to be done with the array, but the OP did say there is some plan for the future...

But, "go through your code and make sure that you understand what every line does" I agree times 1,000!

Are you sure?

Of course! I would keep track of the array index within the loop() function and take appropriate action after it reaches 127. The array index must be static if declared within loop(), or global.

jremington:
Of course! I would keep track of the array index within the loop() function and take appropriate action after it reaches 127. The array index must be static if declared within loop(), or global.

Oh, yeah. You could. Maybe should, so the loop() isn't blocked.

@OP: here is how the loop function might look, avoiding use of an extra for loop:

void loop() {
  static int index=0;
  static int array[128];
  array[index++]=get_data();
  if (index>127) {
    process_data(array);
    index = 0;
   }
...
}