GPS, Acceleration, and temperature datalogger fail

I have a project where I am trying to get gps data, acceleration data , and temperature data all on an sd card. I put together a code but when I use it the gps comes out weird every time. Either it comes out as zeros or it will post but not update or change for awhile. The time also does not change whenever it does show up. I’m not sure what the problem is. Here is the setup and loop of my code.

void setup()
{
Serial.begin(115200);
Serial.println(“Adafruit GPS library basic test!”);
GPS.begin(9600);

GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);

// GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);

GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);

GPS.sendCommand(PGCMD_ANTENNA);

delay(1000);

GPSSerial.println(PMTK_Q_RELEASE);
///////////////////////////////////

Serial.begin(9600);
Serial.println(“Accelerometer Test”); Serial.println("");

if(!accel.begin())
{
Serial.println(“Ooops, no ADXL343 detected … Check your wiring!”);
while(1);
}

/* Set the range to whatever is appropriate for your project */
//accel.setRange(ADXL343_RANGE_16_G);
// accel.setRange(ADXL343_RANGE_8_G);
// accel.setRange(ADXL343_RANGE_4_G);
accel.setRange(ADXL343_RANGE_2_G);

accel.printSensorDetails();
displayDataRate();
displayRange();
Serial.println("");

/////////////////////////////////////////
Serial.begin(115200);
Serial.println(“ADT7410 demo”);

if (!tempsensor.begin()) {
Serial.println(“Couldn’t find ADT7410!”);
while (1);
}
delay(250);

///////////////////////////////////////////
Serial.print(“Initializing SD card…”);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
while (1);
}
Serial.println(“card initialized.”);
}

void loop() {
// make a string for assembling the data to log:
String dataString = “”;
String longit = “”;
String latit = “”;
String altit = “”;
String accelX = “”;
String accelY = “”;
String accelZ = “”;
float cel;

// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += “,”;
}
}
sensors_event_t event;
accel.getEvent(&event);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open(“dataquin.csv”, FILE_WRITE);
char d = GPS.read();
// if you want to debug, this is a good time to do it!
if (GPSECHO)
if (d) Serial.print(d);
// if a sentence is received, we can check the checksum, parse it…
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trying to print out data
Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}

// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
Serial.print("\nTime: ");
if (GPS.hour < 10) { Serial.print(‘0’); }
Serial.print(GPS.hour, DEC); Serial.print(’:’);
if (GPS.minute < 10) { Serial.print(‘0’); }
Serial.print(GPS.minute, DEC); Serial.print(’:’);
if (GPS.seconds < 10) { Serial.print(‘0’); }
Serial.print(GPS.seconds, DEC); Serial.print(’.’);
if (GPS.milliseconds < 10) {
Serial.print(“00”);
} else if (GPS.milliseconds > 9 && GPS.milliseconds < 100) {
Serial.print(“0”);
}
Serial.println(GPS.milliseconds);
Serial.print(“Date: “);
Serial.print(GPS.day, DEC); Serial.print(’/’);
Serial.print(GPS.month, DEC); Serial.print(”/20”);
Serial.println(GPS.year, DEC);
Serial.print("Fix: “); Serial.print((int)GPS.fix);
Serial.print(” quality: "); Serial.println((int)GPS.fixquality);
if (GPS.fix) {
Serial.print("Location: “);
Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
Serial.print(”, ");
Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
Serial.print("Speed (knots): "); Serial.println(GPS.speed);
Serial.print("Angle: "); Serial.println(GPS.angle);
Serial.print("Altitude: "); Serial.println(GPS.altitude);
Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
}
}

// if the file is available, write to it:
if (dataFile) {

dataFile.print(“Date: “); dataFile.print(GPS.month, DEC); dataFile.print(”/”);
dataFile.print(GPS.day, DEC); dataFile.print("/"); dataFile.print(GPS.year, DEC);
dataFile.print(",");
dataFile.print(“Time: “);
dataFile.print(GPS.hour, DEC); dataFile.print(’:’);
dataFile.print(GPS.minute, DEC); dataFile.print(’:’);
dataFile.print(GPS.seconds, DEC); dataFile.print(’:’);
dataFile.print(”,”);

dataFile.print(“Lon: “);
dataFile.print(GPS.lon, 5);//dataFile.print(GPS.lon);
dataFile.print(”,”);
dataFile.print(“Lat: “);
dataFile.print(GPS.lat, 5);//dataFile.print(GPS.lat);
dataFile.print(”,”);
dataFile.print(“Alt: “);
dataFile.print(GPS.altitude, 5);
dataFile.print(”,”);

// print to the serial port too:
Serial.print(GPS.lon, 5);
Serial.print(GPS.lat, 5);
Serial.print(GPS.altitude, 5);
}

// if the file isn’t open, pop up an error:
else {
Serial.println(“error opening datalog.txt”);
}

/* Display the results (acceleration is measured in m/s^2) */
Serial.print("X: “); Serial.print(event.acceleration.x); Serial.print(” ");
Serial.print("Y: “); Serial.print(event.acceleration.y); Serial.print(” ");
Serial.print("Z: “); Serial.print(event.acceleration.z); Serial.print(” ");Serial.println("m/s^2 ");
delay(500);

if (dataFile) {
dataFile.print(“X: “);
dataFile.print(event.acceleration.x);
dataFile.print(”,”);
dataFile.print(“Y: “);
dataFile.print(event.acceleration.y);
dataFile.print(”,”);
dataFile.print(“Z: “);
dataFile.print(event.acceleration.z);
dataFile.print(”,”);
}
Serial.print(event.acceleration.x);
Serial.print(event.acceleration.y);
Serial.print(event.acceleration.z);

float c = tempsensor.readTempC();
float f = c * 9.0 / 5.0 + 32;
Serial.print(“Temp: “); Serial.print(c); Serial.print(”*C\t”);
Serial.print(f); Serial.print("*F");

if (dataFile) {
dataFile.print(“Temp: “);
dataFile.print(c);
dataFile.print(”*C”);
dataFile.print(",");
dataFile.print(f);
dataFile.println("*F");
dataFile.close();

}
}

Please edit your post and change your 'quote' text to 'code' tags. Also, post your entire sketch since what you posted can not compile. What arduino are you using?

Actually, this is a duplicate

Not exactly a duplicate lol. I got farther on my project and got both to work. Now I kind of have a different problem. Anyways how do I change my qoute to code? I'll post the entire thing. It was just past maximum characters.

7quin:
Anyways how do I change my qoute to code?

You read the sticky post at the top of the forum. It explains how to do it, along with a lot of other helpful information that will help you get the most out of this forum.

.... and then you post your code [or edit your post since you have already posted it as plain text]

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include <SD.h>

Adafruit_SSD1306 display = Adafruit_SSD1306(128,32,&Wire);
const int chipSelect = 4;

#include <Adafruit_GPS.h>
#define GPSSerial Serial1
Adafruit_GPS GPS(&GPSSerial);
#define GPSECHO false

uint32_t timer = millis();

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL343.h>
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);

//Adafruit_ADXL343 accel = Adafruit_ADXL343(12345, &Wire1);

void displayDataRate(void)
{
 Serial.print  ("Data Rate:    ");

 switch(accel.getDataRate())
 {
   case ADXL343_DATARATE_3200_HZ:
     Serial.print  ("3200 ");
     break;
   case ADXL343_DATARATE_1600_HZ:
     Serial.print  ("1600 ");
     break;
   case ADXL343_DATARATE_800_HZ:
     Serial.print  ("800 ");
     break;
   case ADXL343_DATARATE_400_HZ:
     Serial.print  ("400 ");
     break;
   case ADXL343_DATARATE_200_HZ:
     Serial.print  ("200 ");
     break;
   case ADXL343_DATARATE_100_HZ:
     Serial.print  ("100 ");
     break;
   case ADXL343_DATARATE_50_HZ:
     Serial.print  ("50 ");
     break;
   case ADXL343_DATARATE_25_HZ:
     Serial.print  ("25 ");
     break;
   case ADXL343_DATARATE_12_5_HZ:
     Serial.print  ("12.5 ");
     break;
   case ADXL343_DATARATE_6_25HZ:
     Serial.print  ("6.25 ");
     break;
   case ADXL343_DATARATE_3_13_HZ:
     Serial.print  ("3.13 ");
     break;
   case ADXL343_DATARATE_1_56_HZ:
     Serial.print  ("1.56 ");
     break;
   case ADXL343_DATARATE_0_78_HZ:
     Serial.print  ("0.78 ");
     break;
   case ADXL343_DATARATE_0_39_HZ:
     Serial.print  ("0.39 ");
     break;
   case ADXL343_DATARATE_0_20_HZ:
     Serial.print  ("0.20 ");
     break;
   case ADXL343_DATARATE_0_10_HZ:
     Serial.print  ("0.10 ");
     break;
   default:
     Serial.print  ("???? ");
     break;
 }
 Serial.println(" Hz");
}

void displayRange(void)
{
 Serial.print  ("Range:         +/- ");

 switch(accel.getRange())
 {
   case ADXL343_RANGE_16_G:
     Serial.print  ("16 ");
     break;
   case ADXL343_RANGE_8_G:
     Serial.print  ("8 ");
     break;
   case ADXL343_RANGE_4_G:
     Serial.print  ("4 ");
     break;
   case ADXL343_RANGE_2_G:
     Serial.print  ("2 ");
     break;
   default:
     Serial.print  ("?? ");
     break;
 }
 Serial.println(" g");
}

#include <Wire.h>
#include "Adafruit_ADT7410.h"

Adafruit_ADT7410 tempsensor = Adafruit_ADT7410();

void setup()
{
Serial.begin(115200);
 Serial.println("Adafruit GPS library basic test!");
 GPS.begin(9600);

 GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);

 GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);

 GPS.sendCommand(PGCMD_ANTENNA);
 delay(1000);

 GPSSerial.println(PMTK_Q_RELEASE);

 Serial.begin(9600);
 Serial.println("Accelerometer Test"); Serial.println("");

 if(!accel.begin())
 {
   Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
   while(1);
 }

 /* Set the range to whatever is appropriate for your project */
 //accel.setRange(ADXL343_RANGE_16_G);
 // accel.setRange(ADXL343_RANGE_8_G);
 // accel.setRange(ADXL343_RANGE_4_G);
  accel.setRange(ADXL343_RANGE_2_G);

 accel.printSensorDetails();
 displayDataRate();
 displayRange();
 Serial.println("");

Serial.begin(115200);
 Serial.println("ADT7410 demo");

 if (!tempsensor.begin()) {
   Serial.println("Couldn't find ADT7410!");
   while (1);
 }
 delay(250);

 Serial.print("Initializing SD card...");

 // see if the card is present and can be initialized:
 if (!SD.begin(chipSelect)) {
   Serial.println("Card failed, or not present");
   // don't do anything more:
   while (1);
 }
 Serial.println("card initialized.");
}

void loop() {
 String dataString = "";
 String longit = "";
 String latit = "";
 String altit = "";
 String accelX = "";
 String accelY = "";
 String accelZ = "";
 float cel;

 for (int analogPin = 0; analogPin < 3; analogPin++) {
   int sensor = analogRead(analogPin);
   dataString += String(sensor);
   if (analogPin < 2) {
     dataString += ",";
   }
 }
sensors_event_t event;
 accel.getEvent(&event);

 // open the file. note that only one file can be open at a time,
 // so you have to close this one before opening another.
 File dataFile = SD.open("dataquin.csv", FILE_WRITE);
char d = GPS.read();
 // if you want to debug, this is a good time to do it!
 if (GPSECHO)
   if (d) Serial.print(d);
 // if a sentence is received, we can check the checksum, parse it...
 if (GPS.newNMEAreceived()) {
   // a tricky thing here is if we print the NMEA sentence, or data
   // we end up not listening and catching other sentences!
   // so be very wary if using OUTPUT_ALLDATA and trying to print out data
   Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
   if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
     return; // we can fail to parse a sentence in which case we should just wait for another
 }

 // approximately every 2 seconds or so, print out the current stats
 if (millis() - timer > 2000) {
   timer = millis(); // reset the timer
   Serial.print("\nTime: ");
   if (GPS.hour < 10) { Serial.print('0'); }
   Serial.print(GPS.hour, DEC); Serial.print(':');
   if (GPS.minute < 10) { Serial.print('0'); }
   Serial.print(GPS.minute, DEC); Serial.print(':');
   if (GPS.seconds < 10) { Serial.print('0'); }
   Serial.print(GPS.seconds, DEC); Serial.print('.');
   if (GPS.milliseconds < 10) {
     Serial.print("00");
   } else if (GPS.milliseconds > 9 && GPS.milliseconds < 100) {
     Serial.print("0");
   }
   Serial.println(GPS.milliseconds);
   Serial.print("Date: ");
   Serial.print(GPS.day, DEC); Serial.print('/');
   Serial.print(GPS.month, DEC); Serial.print("/20");
   Serial.println(GPS.year, DEC);
   Serial.print("Fix: "); Serial.print((int)GPS.fix);
   Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
   if (GPS.fix) {
     Serial.print("Location: ");
     Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
     Serial.print(", ");
     Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
     Serial.print("Speed (knots): "); Serial.println(GPS.speed);
     Serial.print("Angle: "); Serial.println(GPS.angle);
     Serial.print("Altitude: "); Serial.println(GPS.altitude);
     Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
   }
 }

 // if the file is available, write to it:
 if (dataFile) {

   dataFile.print("Date: "); dataFile.print(GPS.month, DEC); dataFile.print("/");
   dataFile.print(GPS.day, DEC); dataFile.print("/"); dataFile.print(GPS.year, DEC);
   dataFile.print(",");
   dataFile.print("Time: ");
   dataFile.print(GPS.hour, DEC); dataFile.print(':');
   dataFile.print(GPS.minute, DEC); dataFile.print(':');
   dataFile.print(GPS.seconds, DEC); dataFile.print(':');
   dataFile.print(",");
 
   dataFile.print("Lon: ");
   dataFile.print(GPS.lon, 5);//dataFile.print(GPS.lon);
   dataFile.print(",");
   dataFile.print("Lat: ");
   dataFile.print(GPS.lat, 5);//dataFile.print(GPS.lat);
   dataFile.print(",");
   dataFile.print("Alt: ");
   dataFile.print(GPS.altitude, 5);
   dataFile.print(",");

   // print to the serial port too:
   Serial.print(GPS.lon, 5);
   Serial.print(GPS.lat, 5);
   Serial.print(GPS.altitude, 5);
 }

 // if the file isn't open, pop up an error:
 else {
   Serial.println("error opening datalog.txt");
 }

 /* Display the results (acceleration is measured in m/s^2) */
 Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print("  ");
 Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print("  ");
 Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print("  ");Serial.println("m/s^2 ");
 delay(500);

if (dataFile) {
 dataFile.print("X: ");
 dataFile.print(event.acceleration.x);
 dataFile.print(",");
 dataFile.print("Y: ");
 dataFile.print(event.acceleration.y);
 dataFile.print(",");
 dataFile.print("Z: ");
 dataFile.print(event.acceleration.z);
 dataFile.print(",");
}
 Serial.print(event.acceleration.x);
 Serial.print(event.acceleration.y);
 Serial.print(event.acceleration.z);

 float c = tempsensor.readTempC();
 float f = c * 9.0 / 5.0 + 32;
 Serial.print("Temp: "); Serial.print(c); Serial.print("*C\t");
 Serial.print(f); Serial.print("*F");

if (dataFile) {
 dataFile.print("Temp: ");
 dataFile.print(c);
 dataFile.print("*C");
 dataFile.print(",");
 dataFile.print(f);
 dataFile.println("*F");
 dataFile.close();
}
}

Thanks for editing your post and adding code tags. A click, copy & paste and the code is in my editor. You call Serial.begin() multiple times and different baud rates. There is no way all that initial stuff in setup() displays properly on the serial monitor. Just start Serial once and be done.

As to your problem, you are writing to your data file every time through loop but I think you should only be writing to the file when you receive a new GPS message?

It also appears you are using an arduino Mega? (based on Serial1 being used) and if so, you should get rid or your String variables you use to build up all your analog read values. It will eventually fragment your memory and crash. It is much better to learn how to use C strings (null terminated char arrays)

I forgot to tell you that the board I am using is the Adafruit Feather MO Adalogger. Along with the GPS featherwing and ADXL343+ADT7410 featherwing. I will try to clean up my code and see if it helps.

blh64:
As to your problem, you are writing to your data file every time through loop but I think you should only be writing to the file when you receive a new GPS message?

How would I ask for GPS only when I get a new message?

You already have that test in your code

 if (GPS.newNMEAreceived()) {
   // a tricky thing here is if we print the NMEA sentence, or data
   // we end up not listening and catching other sentences!
   ...

You just need to move all of your data logging inside this if() statement.