Hi,
Would someone be able to help me reduce the amount of storage for my code?
Here's the code:
#include <SD.h>
#include <Wire.h> //Needed for I2C to GNSS
#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_u-blox_GNSS
SFE_UBLOX_GNSS myGNSS;
#define PIN_SPI_CS 4
long lastTime = 0; //Simple local timer. Limits amount if I2C traffic to u-blox module.
File myFile;
void setup()
{
Serial.begin(57600);
while (!Serial)
; //Wait for user to open terminal
if (!SD.begin(PIN_SPI_CS)) {
Serial.println(F("SD CARD FAILED, OR NOT PRESENT!"));
while (1); // don't do anything more:
}
Serial.println(F("SD CARD INITIALIZED."));
Wire.begin();
myGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
//myGNSS.saveConfiguration(); //Optional: Save the current settings to flash and BBR
}
void loop()
{
//Query module only every second. Doing it more often will just cause I2C traffic.
//The module only responds when a new position is available
if (millis() - lastTime > 60000)
{
lastTime = millis(); //Update the timer
// Print date and time
Serial.print(myGNSS.getYear());
Serial.print(F("-"));
Serial.print(myGNSS.getMonth());
Serial.print(F("-"));
Serial.print(myGNSS.getDay());
Serial.print(F(","));
/* Serial.print(myGNSS.getHour());
Serial.print(F(":"));
Serial.print(myGNSS.getMinute());
Serial.print(F(":"));
Serial.print(myGNSS.getSecond());
Serial.print(F(","));*/
// Collect the position data
int32_t latitude = myGNSS.getLatitude();
int32_t longitude = myGNSS.getLongitude();
// Defines storage for the lat and lon units integer and fractional parts
int32_t lat_int; // Integer part of the latitude in degrees
int32_t lat_frac; // Fractional part of the latitude
int32_t lon_int; // Integer part of the longitude in degrees
int32_t lon_frac; // Fractional part of the longitude
// Calculate the latitude and longitude integer and fractional parts
lat_int = latitude / 10000000; // Convert latitude from degrees * 10^-7 to Degrees
lat_frac = latitude - (lat_int * 10000000); // Calculate the fractional part of the latitude
if (lat_frac < 0) // If the fractional part is negative, remove the minus sign
{
lat_frac = 0 - lat_frac;
}
lon_int = longitude / 10000000; // Convert latitude from degrees * 10^-7 to Degrees
lon_frac = longitude - (lon_int * 10000000); // Calculate the fractional part of the longitude
if (lon_frac < 0) // If the fractional part is negative, remove the minus sign
{
lon_frac = 0 - lon_frac;
}
// Print the lat and lon
Serial.print(lat_int); // Print the integer part of the latitude
Serial.print(".");
printFractional(lat_frac, 9); // Print the fractional part of the latitude with leading zeros
Serial.print(",");
Serial.print(lon_int); // Print the integer part of the latitude
Serial.print(".");
printFractional(lon_frac, 9); // Print the fractional part of the latitude with leading zeros
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.close();
// print to the serial port too:
}
}
// Pretty-print the fractional part with leading zeros - without using printf
// (Only works with positive numbers)
void printFractional(int32_t fractional, uint8_t places)
{
if (places > 1)
{
for (uint8_t place = places - 1; place > 0; place--)
{
if (fractional < pow(10, place))
{
Serial.print("0");
}
}
}
Serial.print(fractional);
}
Thank you!