// The SFE_LSM9DS1 library requires both Wire and SPI be
// included BEFORE including the 9DS1 library.
#include <Wire.h>
#include <SPI.h>
#include <SparkFunLSM9DS1.h>
#include "SdFat.h"
//////////////////////////
// LSM9DS1 Library Init //
//////////////////////////
// Use the LSM9DS1 class to create an object. [imu] can be
// named anything, we'll refer to that throught the sketch.
LSM9DS1 imu;
SdFat SD;
const uint8_t chipSelect = SS;
#define SD_CS_PIN SS
#define error(s) SD.errorHalt(PSTR(s))
ArduinoOutStream cout(Serial);
#include "Adafruit_FONA.h"
// GPS
float latitude;
float longitude;
float kph;
boolean gpsSuccess;
float mmms;
float distance;
float z;
float x;
float y;
float xy;
unsigned long timeStart;
unsigned long time2;
unsigned long time3;
File myFile;
// We default to using software serial. If you want to use hardware serial
// (because softserial isnt supported) comment out the following three lines
// and uncomment the HardwareSerial line
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(3, 2);
SoftwareSerial *fonaSerial = &fonaSS;
// Hardware serial is also possible!
// HardwareSerial *fonaSerial = &Serial1;
Adafruit_FONA fona = Adafruit_FONA(4);
void setup()
{
Serial.begin(115200);
//sd
Serial.println();
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
if (!SD.begin(4,SPI_HALF_SPEED)) SD.initErrorHalt();
Serial.println("initialization done.");
//imu
Wire.begin();
imu.settings.accel.sampleRate = 0;
if (imu.begin() == false) // with no arguments, this uses default addresses (AG:0x6B, M:0x1E) and i2c port (Wire).
{
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);
}
myFile = SD.open("log.txt"); // clear file after transmission eventually
SD.remove("log.txt");
myFile.close();
timeStart = millis();
time3 = millis();
//fona
while (! Serial);
Serial.begin(115200);
Serial.println(F("Adafruit FONA 808 & 3G GPS demo"));
Serial.println(F("Initializing FONA... (May take a few seconds)"));
fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) {
Serial.println(F("Couldn't find FONA"));
while(1);
}
Serial.println(F("FONA is OK"));
// Try to enable GPRS
Serial.println(F("Enabling GPS..."));
fona.enableGPS(true);
}
void loop()
{
/* if (!fona.enableGPS(true)) // 'O'
Serial.println(F("Failed to turn on fone"));
*/
myFile = SD.open("log.txt",FILE_WRITE);
gpsSuccess = fona.getGPS(&latitude,&longitude,&kph);
if ( imu.accelAvailable() )
{
time2 = millis();
imu.readAccel();
z = imu.calcAccel(imu.az);
x = imu.calcAccel(imu.ax);
y = imu.calcAccel(imu.ay);
xy = sqrt((x*x)+(y*y));
if(gpsSuccess){ // Calculate distance traveled
mmms = kph/3.6;
distance = mmms * (time2-time3);
if(((double)distance) > 300.0){
myFile.println("New Profile");
time3 = millis();
}
}else {
Serial.println("Waiting for FONA GPS 3D fix...");
}
if(xy < .3 && xy > -0.3){ // Arbitraty bounds to verify vehicle is travelling at constant speed.
Serial.print(x);
Serial.print(' ');
Serial.print(y);
Serial.print(' ');
Serial.println(z);
myFile.print(time2-timeStart);
myFile.print(' ');
myFile.print(xy);
myFile.print(' ');
myFile.println(z);
}
else
{
myFile.println("out of bounds");
}
}
myFile.close();
}
Using the Arduino UNO.
Ok so I am currently sitting at 75% program space, and 99% dynamic memory. I've already begun to attempting to reduce the size of the libraries. But I am still struggling to reduce to a workable amount.
Is there any Arduino tips that will save memory somewhere? Little overview of the program: reads GPS/IMU data and writes to an SD card, so I believe all the libraries are necessary. I am using the Fona library for GPS, but the cellular component isn't being used. I am unsure if I can use the GPS module with other, perhaps cheaper, libraries, but I would like to continue using the FONA library for the timebeing if possible. Am I overlooking something obvious? Any help appreciated.
Looking for general help.