EDIT: apologies thought I was posting in the programming forum, it's accidentally ended up in the project one and I can't find a delete button (?).
Hi I was wondering if someone could advise me on how to store the second the last reading within the code I've got. I was looking for somewhere to place this:
int prev ;
prev=(imu.calcMag(imu.mx),2);
Serial.println(prev,2);
But I feel like I'm confusing myself as I don't think there is a place to put it where it won't be the new reading. I was wondering whether I needed to get the coding to read the magnetometer twice to store the two variables and then calculate the difference but didn't know if there'd be a more efficient way, alongside the fact at the moment it's not even producing the right values. Even though I have it to 2dp it has come up with "111111111" when the reading was -1.08 or something like that.
The whole code is below:
#include <Wire.h>
#include <SPI.h>
#include <SparkFunLSM9DS1.h>
LSM9DS1 imu;
#define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
#define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW
#define PRINT_CALCULATED
//#define PRINT_RAW
#define PRINT_SPEED 2500
static unsigned long lastPrint = 0;
#define DECLINATION -0.7 // Declination (degrees) in Coventry, UK
void setup()
{
Serial.begin(115200);
imu.settings.device.commInterface = IMU_MODE_I2C;
imu.settings.device.mAddress = LSM9DS1_M;
imu.settings.device.agAddress = LSM9DS1_AG;
if (!imu.begin())
{
Serial.println("Failed to communicate with LSM9DS1.");
Serial.println("Double-check wiring.");
Serial.println("Default settings in this sketch will " \
"work for an out of the box LSM9DS1 " \
"Breakout, but may need to be modified " \
"if the board jumpers are.");
while (1)
;
}
}
void loop()
{
// Update the sensor values whenever new data is available
if ( imu.gyroAvailable() )
{
imu.readGyro();
}
if ( imu.accelAvailable() )
{
imu.readAccel();
}
if ( imu.magAvailable() );
{
imu.readMag();
}
if ((lastPrint + PRINT_SPEED) < millis())
{
printGyro(); // Print "G: gx, gy, gz"
printAccel(); // Print "A: ax, ay, az"
printMag(); // Print "M: mx, my, mz"
printAttitude(imu.ax, imu.ay, imu.az,
-imu.my, -imu.mx, imu.mz);
Serial.println();
lastPrint = millis(); // Update lastPrint time
}
}
void printGyro()
{
Serial.print("G: ");
#ifdef PRINT_CALCULATED
Serial.print(imu.calcGyro(imu.gx), 2);
Serial.print(", ");
Serial.print(imu.calcGyro(imu.gy), 2);
Serial.print(", ");
Serial.print(imu.calcGyro(imu.gz), 2);
Serial.println(" deg/s");
#elif defined PRINT_RAW
Serial.print(imu.gx);
Serial.print(", ");
Serial.print(imu.gy);
Serial.print(", ");
Serial.println(imu.gz);
#endif
}
void printAccel()
{
Serial.print("A: ");
#ifdef PRINT_CALCULATED
Serial.print(imu.calcAccel(imu.ax), 2);
Serial.print(", ");
Serial.print(imu.calcAccel(imu.ay), 2);
Serial.print(", ");
Serial.print(imu.calcAccel(imu.az), 2);
Serial.println(" g");
#elif defined PRINT_RAW
Serial.print(imu.ax);
Serial.print(", ");
Serial.print(imu.ay);
Serial.print(", ");
Serial.println(imu.az);
#endif
}
void printMag()
{
Serial.print("M: ");
#ifdef PRINT_CALCULATED
Serial.print(imu.calcMag(imu.mx), 2);
Serial.print(", ");
Serial.print(imu.calcMag(imu.my), 2);
Serial.print(", ");
Serial.print(imu.calcMag(imu.mz), 2);
Serial.println(" gauss");
if (imu.calcMag(imu.mx)<-1.5 && imu.calcMag(imu.mx)>-1.85)
Serial.println("2-4cm away from magnetometer on the x-axis");
int diff;
diff=prev-(imu.calcMag(imu.mx), 2);
Serial.println(diff,2);
#elif defined PRINT_RAW
Serial.print(imu.mx);
Serial.print(", ");
Serial.print(imu.my);
Serial.print(", ");
Serial.println(imu.mz);
#endif
}
void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
{
float roll = atan2(ay, az);
float pitch = atan2(-ax, sqrt(ay * ay + az * az));
float heading;
if (my == 0)
heading = (mx < 0) ? PI : 0;
else
heading = atan2(mx, my);
heading -= DECLINATION * PI / 180;
if (heading > PI) heading -= (2 * PI);
else if (heading < -PI) heading += (2 * PI);
// Convert everything from radians to degrees:
heading *= 180.0 / PI;
pitch *= 180.0 / PI;
roll *= 180.0 / PI;
Serial.println("Pitch, Roll: ");
Serial.print(pitch, 2);
Serial.print(", ");
Serial.println(roll, 2);
Serial.print("Heading: "); Serial.println(heading, 2);
}
Only really focusing on the magnetometer, this is what I added in trying to find the difference
int diff;
diff=prev-(imu.calcMag(imu.mx), 2);
Serial.println(diff,2);
Any advice greatly appreciated! Thanks.