//
/*CHECK SD PINS
* TEST OTHER LIBRARY
*/
// The SFE_LSM9DS1 library requires both Wire and SPI be
// included BEFORE including the 9DS1 library.
#include <Wire.h>
#include <SPI.h>
#include <Arduino_LSM9DS1.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.
SdFat SD;
const uint8_t chipSelect = SS;
#define SD_CS_PIN SS
#define error(s) SD.errorHalt(PSTR(s))
// GPS
float latitude;
float longitude;
float kph;
float heading;
float altitude;
//etc
unsigned long timeSampleStart;
#include "Adafruit_FONA.h"
// 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(FONA_TX, FONA_RX);
//SoftwareSerial *fonaSerial = &fonaSS;
SoftwareSerial *fonaSerial = &SoftwareSerial(3, 2);
// Hardware serial is also possible!
// if this gives compilation errors, try switching "Serial1" for "Serial"
//HardwareSerial *fonaSerial = &Serial1;
Adafruit_FONA fona = Adafruit_FONA(4);
void setup()
{
//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"));
Serial.println(F("Enabling GPS..."));
fona.enableGPS(true);
delay(1000);
Serial.println(F("delay"));
//sd
Serial.println();
Serial.print(F("Initializing SD card..."));
if (!SD.begin(7)) {
Serial.println(F("initialization failed!"));
return;
}
if (!SD.begin(7,SPI_HALF_SPEED)) SD.initErrorHalt();
Serial.println(F("initialization done."));
delay(1000);
Serial.println(F("delay"));
//imu
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
delay(1000);
Serial.println(F("delay"));
File myFile;
myFile = SD.open("f.txt"); // clear file after transmission eventually
SD.remove("f.txt");
myFile.close();
}
void setupGPS()
{
while (!fona.getGPS(&latitude,&longitude,&kph,&heading,&altitude)) {
Serial.println(F("Waiting for FONA GPS 3D fix..."));
delay(2000);
}
//Serial.println(F("fuck"));
//timeSampleStart = millis();
delay(2000);
return;
}
void loop()
{
float distance;
float x;
float y;
float z;
unsigned long time2;
File myFile;
myFile = SD.open("f.txt");
myFile.println("help");
setupGPS();
myFile = SD.open("f.txt",FILE_WRITE);
if ( IMU.accelerationAvailable() )
{
IMU.readAcceleration(x,y,z);
time2 = millis();
// Calculate distance traveled
distance = (kph/3.6) * (time2-timeSampleStart);
if(distance > 10.0){
myFile.println();
myFile.print(latitude);
myFile.print(' ');
myFile.print(longitude);
myFile.print(' ');
myFile.println(kph);
timeSampleStart = millis();
}
if((x < .3) && (x > -0.3) && (y < 0.3) && (y > -0.3)){ // Arbitraty bounds to verify vehicle is travelling at constant speed.
myFile.print(time2);
myFile.print(' ');
myFile.println(z);
}
else
{
myFile.println('x');
}
}
myFile.close();
}
I am using an Arduino Uno with an accelerometer, Fona GPS/GSM, and a microSD slot. The code runs correctly through the initialization. The GPS finds satellites before proceeding in the loop. After getting GPS coordinates, I attempt to read values from the IMU. At "IMU.readAcceleration(x,y,z)", the program resets and goes through initialization again, and we never get further into the loop.
All components work correctly on their own. Is the Arduino crashing when it resets? What about reading acceleration values after acquiring a 3D fix is causing this to crash? Could it be a power issue? I don't have the equipment to check, but I don't believe that is the case. Any tips?
Edit: Sitting at 73% dynamic memory. Can a memory issue cause resets like this?