Go Down

### Topic: Help neede with ADXL345 low pass filter (Read 430 times)previous topic - next topic

#### MarkCase

##### Jan 13, 2018, 12:38 pm
Hello, im pretty new to Arduino and coding them, i need a little help with my project.
What I am doing is building a gear shift indicator for my motorcycle. It consists of an OLED display and two ADXL345's, one will be fixed to the motorcycle chassis to measure the angle of the motorcycle and the other to the gear shift lever all connected via i2c.
Currently  have both ADXL345's connected and working and are both outputting data that I can access and print to the serial monitor (ill worry about the display later).
I have written code that will change gear when the angle of one sensor is X amount greater that the other, this does indeed work fine so I have a working prototype as such. The issue I have now is that these sensors are very sensitive even a small tap will give a large spike in the readings so I need to smooth the outputs.
I have done some googling and searching, what I need to do is to take for example 4 readings from the sensor  then average them together.
This is where I get stuck, how do I take 4 readings? I assume ill be using a FOR loop counting from 1 to 4, is it possible to have the variable A then add the number from the for loop to it so I get A1, A2,A3,A4?
Is there a better way?

#### jremington

#1
##### Jan 13, 2018, 08:53 pmLast Edit: Jan 13, 2018, 09:03 pm by jremington
To average four values, you just add them up and divide by four.
Example:

Code: [Select]
`   long sum = 0;   for (int i=1; i<5; i++) sum = sum + read_adxl();   sum = sum/4;`

#### MarkCase

#2
##### Jan 13, 2018, 09:14 pm
Thanks for your reply, I understand how to calculate an average, I guess what I'm asking is how do I read the sensor 4 times and put the result into 4 separate variables?

Currently my first sensor reads into variables Xa and Ya and my second sensor reads into Xb and Yb.

So for this example how would I read Xa four times and write the result into Xa1 Xa2 Xa3 and Xa4?

#### jremington

#3
##### Jan 13, 2018, 09:21 pm
Put the values in an array.

#### MarkCase

#4
##### Jan 13, 2018, 09:34 pm
This is what I don't understand, I am a novice at programming but I'm good on the electronics side of things.
Do you have an example code of how I can read Xa four time and put the results into four separate variables?
Thank you

#5
##### Jan 13, 2018, 10:06 pm
Code: [Select]
` Add these before setup():byte i;long reading[4]; // or unsigned long, if they are only positive numbers.long will support ~ +/2.14 billion, unsigned long will support 0 to 4.29 billionThen in loop();   for (int i=0; i<4; i=i+1) {    reading[i] = read_adxl();   }reading[0], reading[1], reading[2], and reading[3] will have your 4 values.`
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### PieterP

#6
##### Jan 14, 2018, 01:43 pm
https://playground.arduino.cc/Main/RunningAverage

However, an exponential moving average might be more appropriate here.

Pieter

#### MarkCase

#7
##### Feb 06, 2018, 08:36 pm
Thank you for your replies, ill have a play with this and see if I can implement it.

Go Up