Multiple Sensors with GPS

Hello all, Ive been working on a project and im trying to integrate several sensors and gps but have been having some trouble syncing everything.

components:

2x DS18B20 temperature sensors

1x ADXL335 accelerometer

1x EM 406 Gps with sparkfun’s gps shield

1x logger shield from adafruit

I wrote all the code (heavily based on the sparkfun gps shield examples) and got everything working and logging to a CSV file but the gps doesnt always work the way id like it to. Everything is logging at about 1hz but when it comes time to ask for GPS it doesnt always return values. My guess is that theres some syncing issues as the main sketch is running at 115200 baud and the EM406 at 4800 using soft serial, and im asking for data at the wrong time. the sketch asks for date & time, then temperatures, then accelerometer values and then gps and lays it all in one nice line but most of the time the gps is missing. when i run just the gps it reports about every second, so i figure im just not asking at the right time. Im new to electronics so I havent been able to diagnose and dont truly understand how to interface different components with different baudrates. If someone could just point me in the right direction it would be a lot of help, in the meantime i will continue googling and experimenting.

thank you

Alan

code:

/*---Alan Sanchez ---*/

//LIBRARIES
#include <NewSoftSerial.h>     //GPS
#include <TinyGPS.h>            //GPS
#include <OneWire.h>           //DS18B20
#include <DallasTemperature.h> //DS18B20
#include "SdFat.h"             //talk to SD card
#include <Wire.h>              //logger
#include "RTClib.h"            //talk to clock

//GPS
#define RXPIN 2
#define TXPIN 3
#define GPSBAUD 4800 //GPS baud rate
TinyGPS gps; //TinyGPS object
NewSoftSerial uart_gps(RXPIN, TXPIN); //Initialize library to RX/TX pins
void logGPS(TinyGPS &gps);

//DS18B20
#define ONE_WIRE_BUS 4 //Data wire D4
#define TEMPERATURE_PRECISION 12
OneWire oneWire(ONE_WIRE_BUS); //Setup a oneWire instance to communicate with any OneWire devices.
DallasTemperature sensors(&oneWire); //Pass oneWire reference to Dallas Temperature. 
DeviceAddress Sensor00, Sensor01; // arrays to hold device addresses

//ADXL335
int x, y, z; 

//Logger
#define LOG_INTERVAL  1000 // mills between entries
#define ECHO_TO_SERIAL   0 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()
#define SYNC_INTERVAL 2000 // mills between calls to sync()
uint32_t syncTime = 0;     // time of last sync()
RTC_DS1307 RTC; //define the Real Time Clock object
//The objects to talk to the SD card
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

void setup(){
      Serial.begin(115200);
      uart_gps.begin(GPSBAUD);
      sensors.begin(); //Start DS18B20 library
      
  
      // initialize the SD card
        if (!card.init()) error("card.init"); 
      // initialize a FAT volume
        if (!volume.init(card)) error("volume.init"); 
      // open root directory
        if (!root.openRoot(volume)) error("openRoot");
      // create a new file
      char name[] = "LOGGER00.CSV";
          for (uint8_t i = 0; i < 100; i++) {
          name[6] = i/10 + '0';
          name[7] = i%10 + '0';
          if (file.open(root, name, O_CREAT | O_EXCL | O_WRITE)) break;
          }
          if (!file.isOpen()) error ("file.create");
              Serial.print("Logging to: ");
              Serial.println(name);
  
       file.writeError = 0; // clear print error
  
       Wire.begin();  
       if (!RTC.begin()) {
           file.println("RTC failed");
       #if ECHO_TO_SERIAL
           Serial.println("RTC failed");
       #endif  //ECHO_TO_SERIAL
       }
       
       //&&&&&&&&&&&&&&&&//FILE HEADER//&&&&&&&&&&&&&&&&&&  
       file.println("REACH_ONE LOGGER"); 
       DateTime now;
       now = RTC.now();
       file.print(" ");
       file.print(now.year(), DEC);
       file.print("/");
       file.print(now.month(), DEC);
       file.print("/");
       file.print(now.day(), DEC);
       file.print(" ");
       file.print(now.hour(), DEC);
       file.print(":");
       file.print(now.minute(), DEC);
       file.print(":");
       file.print(now.second(), DEC);
       file.println(" ");
       file.println(" "); 
       //--------------------------------------------------

       #if ECHO_TO_SERIAL
       Serial.println("REACH_ONE LOGGER");
       Serial.println(" ");
       if (file.writeError || !file.sync()) {
       error("write header");
       }
       #endif
}


void loop(){
  
  //DS18B20
      sensors.requestTemperatures(); // Send the command to get temperatures
      logTemps();
  //ADXL335
      logAccel();
  //GPS 
      while(uart_gps.available()){  
      int c = uart_gps.read();   // load the data into a variable
            if(gps.encode(c)){   // if there is a new valid sentence
                logGPS(gps);     // then log the data.
            }
      }
  //EndLine
      file.println("  ");
  //SYNC          
      if (file.writeError) error("write data");
      if ((millis() - syncTime) <  SYNC_INTERVAL) return;
      syncTime = millis();
      if (!file.sync()) error("sync");
}


void logTemps(){
      DateTime now;
      file.writeError = 0; // clear print error
      uint32_t m = millis();
      now = RTC.now();// fetch the time
  
// Date/Time Stamp Data    
      file.print(now.year(), DEC);
      file.print("/");
      file.print(now.month(), DEC);
      file.print("/");
      file.print(now.day(), DEC);
      file.print(", ");
      file.print(now.hour(), DEC);
      file.print(":");
      file.print(now.minute(), DEC);
      file.print(":");
      file.print(now.second(), DEC);
      file.print(", "); 
//SENSOR00
      file.print("Temp Sensor00: ");
      file.print(", "); 
      float TSensor00 = sensors.getTempCByIndex(0);
      file.print(TSensor00);
      file.print(", "); 
      file.print("C"); 
      file.print(", "); 
      file.print(DallasTemperature::toFahrenheit(TSensor00));
      file.print(", "); 
      file.print("F");
      file.print(", "); 
//SENSOR01
      file.print("Temp Sensor01: "); 
      file.print(", "); 
      float TSensor01 = sensors.getTempCByIndex(1);
      file.print(TSensor01);
      file.print(", "); 
      file.print("C"); 
      file.print(", "); 
      file.print(DallasTemperature::toFahrenheit(TSensor01));
      file.print(", "); 
      file.print("F");
      file.print(", "); 
      
      Serial.println ("logged date & temp");
}
      
void logAccel(){
      x = analogRead(1);       // read analog input pin 0
      y = analogRead(2);       // read analog input pin 1
      z = analogRead(3);       // read analog input pin 2
  
      x = (x*10-3365)/0.675;
      y = (y*10-3335)/0.675;
      z = (z*10-3370)/0.67;
  
      file.print("Acceleretations x y z: ");
      file.print(", ");       
      file.print(x, DEC);    // print the acceleration in the X axis
      file.print(", ");       
      file.print(y, DEC);    // print the acceleration in the Y axis
      file.print(", ");       // prints a space between the numbers
      file.print(z, DEC);  // print the acceleration in the Z axis
      file.print(", "); 
      
      Serial.println("logged accelerations");
}

void logGPS(TinyGPS &gps){
 //DATE & TIME
      int year;
      byte month, day, hour, minute, second, hundredths;
      gps.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
      file.print(month, DEC); 
      file.print("/"); 
      file.print(day, DEC);
      file.print("/");
      file.print(year);
          file.print(", "); 
      file.print(hour, DEC);
      file.print(":"); 
      file.print(minute, DEC); 
      file.print(":"); 
      file.print(second, DEC); 
      file.print(".");
      file.print(hundredths, DEC);
          file.print(", ");
  //POSITION LAT&LONG
      float latitude, longitude;
      gps.f_get_position(&latitude, &longitude);
      file.print("Lat/Long: "); 
          file.print(", "); 
      file.print(latitude,5); 
          file.print(", "); 
      file.print(longitude,5);
          file.print(", "); 
  //ALTITUDE,COURSE,SPEED   
      file.print("Altitude (meters): "); 
          file.print(", ");
      file.print(gps.f_altitude());
          file.print(", ");  
      file.print("Course (degrees): "); 
          file.print(", ");
      file.print(gps.f_course());
          file.print(", "); 
      file.print("Speed (kmph): ");
          file.print(", ");     
      file.print(gps.f_speed_kmph());
          file.print(" ; ");   
       
      Serial.println("logged GPS data");   
}

void error(char *str) {  //function to print out errors throughout code
      Serial.print("error: ");
      Serial.println(str);
      while(1);
      }

Try reversing the serial ports.

NewSoftSerial is probably missing data cause there’s no buffer.

Try using the hardware port for GPS and the NewSoftSerial port to send data to the terminal.

The hardware buffer will grab what you throw at it.