I am trying to smooth my headings coming from my LSM303 sensor. I get get get the tilt compensated values, but I just want to smooth them. Please help!
trying something like this...
#include <Wire.h>
#include <LSM303.h>
LSM303 compass;
// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = Compass.heading;
void setup() {
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
Wire.begin();
compass.init();
compass.enableDefault();
// Calibration values. Use the Calibrate example program to get the values for
// your compass.
compass.m_min.x = -646; compass.m_min.y = -689; compass.m_min.z = -454;
compass.m_max.x = +338; compass.m_max.y = +450; compass.m_max.z =+586;
}
void loop() {
compass.read();
int heading = compass.heading((LSM303::vector){0,-1,0});
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
Serial.println(average);
delay(100);
}
using these links as a guide...