Having trouble storing values within variables over time intervals

Hi I'm new to using Arduinos, I wish to record 5 values over time from an accelerometer to see a trend and map its movement from an equation between the 5 values. I cant seem to figure out how to write my float variables as values at different time intervals, they all seem to get updated together and equal the present value. Any help would be greatly appreciated.

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

const int LED = 13;

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();

float z1;
float z2;
float z3;
float z4;
float z5;

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

if (!accel.begin())
{
Serial.println("No ADXL345 sensor detected.");
while (1);
}
pinMode(LED, OUTPUT);
}

void loop(void)
{
sensors_event_t event;
accel.getEvent(&event);

z1 = event.acceleration.z ;
delay(10);
z2 = (event.acceleration.z);
delay(10);
z3 = (event.acceleration.z);
delay(10);
z4 = (event.acceleration.z);
delay(10);
z5 = (event.acceleration.z);
delay(10);
Serial.print(z1); Serial.print(","); Serial.print(z2); Serial.print(","); Serial.print(z3); Serial.print(","); Serial.print(z4); Serial.print(","); Serial.println(z5);
delay(1000);

}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Are you sure that the values read from the sensor is different each time you read it? Have you tried printing each one as it is read ?

A much better way to do what you want would be to use an array of values

Hi, thanks for the tips!

I have checked and it prints different values every time. I'm sure I have the accelerometer setup correctly, I'm just having trouble with the code. Il check out the array of values, Even with the array of values how would I map the smaller variables to time intervals? thanks

Lets say an array of floats is made like so

float anarrayofvalues[5] = {0.0f};

Then you might make an array cell pointer.

int arraycellpointer = 0;

Now say a value is stored once every millisecond. Now you have got a time of entry and a number of entry.

anarrayofvalues[arraycellpointer] = somethingnew;
arraycellpointer++;
if (arraycellpointer > 4 )
{
arraycellpointer = 0;
}

Never mind, see you got that part…

a7

Maybe not. Guessing, I suspect that it needs to be called for each variable.

Yes, getEvent has to be called or the variables will not be updated.

I looked back and saw @UKHeliBob for the first time

Are you sure that the values read from the sensor is different each time you read it?

and assumed that woukd put the OP on to the right track.

So TBC @goldenguy3, you have to call

accel.getEvent(&event);

before each assignment z1, z2 and so forth if you expect to get new values.

Arrays will be nice, but your code shoukd provide readings spaced by ~10 milliseconds if you make the getEvent calls…

a7

Thank you all for the tips I got it working using the array of values

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