Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <SoftwareSerial.h>
#include <SdFat.h>
#include <TinyGPS.h>
//SD
const int chipSelect = 4;
// MOSI - pin 11 yellow middle
// MISO - pin 12 yellow right
// CLK - pin 13 blue
// CS - pin 4 green
SdFat sd;
SdFile myFile;
//GPS
TinyGPS gps;
SoftwareSerial uartgps(2,3);
float flat, flon, fkmph;
unsigned long fix_age;
int x, y, z, c, i, year, sats;
byte month, day, hour, minute, second, hundredths;
void setup() {
i = 0;
Serial.begin(115200);
uartgps.begin(4800);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
}
void loop() {
//read in GPS data
while(uartgps.available()){
c = uartgps.read();
if(gps.encode(c)){
gps.f_get_position(&flat, &flon);
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &fix_age);
fkmph = gps.f_speed_kmph();
sats = gps.satellites();
}
}
//printing
if(i > 19200){
//reset counter
i = 0;
// open the file for write at end like the Native SD library
if (!myFile.open("data.txt", O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening data.txt for write failed");
}
//print
myFile.print(flat, DEC);
myFile.print(",");
myFile.print(flon, DEC);
myFile.print(",");
myFile.print(fkmph);
myFile.print(",");
myFile.print(hour, DEC);
myFile.print(":");
myFile.print(minute, DEC);
myFile.print(":");
myFile.print(second, DEC);
myFile.print(",");
if (fix_age == TinyGPS::GPS_INVALID_AGE){
myFile.print("fix_age_error");
}else{
myFile.print(fix_age);
}
myFile.print(",");
myFile.print(sats);
myFile.println();
//close SD file
myFile.close();
}
x = analogRead(A0);
y = analogRead(A1);
z = analogRead(A2);
Serial.print(x); Serial.print(y); Serial.println(z);
i++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment