Sketch too big. Help reducing storage

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!

Sketch uses 54206 bytes (111%) of program storage space. Maximum is 48640 bytes.
Global variables use 1456 bytes (23%) of dynamic memory, leaving 4688 bytes for local variables. Maximum is 6144 bytes.
Sketch too big; see https://support.arduino.cc/hc/en-us/articles/360013825179 for tips on reducing it.
Error compiling for board Arduino Uno WiFi Rev2.

Which Arduino are you using? How much flash memory does your sketch consume?

I am using Arduino UNO WiFi Rev 2. Output says this:
Sketch uses 54206 bytes (111%) of program storage space. Maximum is 48640 bytes.

this is really strange that a sketch 120 lines short needs more than 54000 bytes

I compiled your code for ESP32 successfully
I compiled your code for Seeeduino XIAO successfully
I compiled your code for ESP8266 successfully

seems that the SparkFun_u-blox_GNSS_Arduino_Library which is 18000 ! lines long
eats up all memory

Is using a Arduino UNO WiFi Rev 2-board a must?
You seem to only use the ublox thing but no IO-pins
so a $6 Seeeduino XIAO would be sufficient

CPU ARM Cortex-M0+ CPU(SAMD21G18) running at up to 48MHz
Flash Memory 256KB
SRAM 32KB

best regards Stefan

read this from their GitHub

Memory Usage

The u-blox GNSS library has grown considerably over the years and now exceeds the available program memory on platforms like the ATmega328 (Arduino Uno). If you want to reduce the amount of memory used by the library, you can edit the header file (SparkFun_u-blox_GNSS_Arduino_Library.h) and uncomment lines 60 and 63:

#define SFE_UBLOX_REDUCED_PROG_MEM // Uncommenting this line will delete the minor debug messages to save memory
#define SFE_UBLOX_DISABLE_AUTO_NMEA // Uncommenting this line will disable auto-NMEA support to save memory

Please note: the debug messages are automatically deleted and auto-NMEA support is automatically disabled on ARDUINO_AVR_UNO platforms. For other platforms, you will need to uncomment those lines manually.

On Windows, you will normally find SparkFun_u-blox_GNSS_Arduino_Library.h in:

  • Documents\Arduino\libraries\SparkFun_u-blox_GNSS_Arduino_Library\src
1 Like

I had been using the code for months now but I added the SD module (datalogging) that made the storage full (I agree that the SparkFun_u-blox_GNSS_Arduino_Library takes up most of the storage, though).

I have deleted the lines but still exceed the storage. Thank you for the help, though.

what do you mean by "deleted the lines" ??

you want SFE_UBLOX_REDUCED_PROG_MEM to be defined as the source code does things like

#ifndef SFE_UBLOX_REDUCED_PROG_MEM
        if ((_printDebug == true) || (_printLimitedDebug == true)) // This is important. Print this if doing limited debugging
        {
          _debugSerial->print(F("process: ZERO LENGTH packet received: Class: 0x"));
          _debugSerial->print(packetBuf.cls, HEX);
          _debugSerial->print(F(" ID: 0x"));
          _debugSerial->println(packetBuf.id, HEX);
        }
#endif

so if SFE_UBLOX_REDUCED_PROG_MEM is defined, then the whole debug statement and all the F("...") flash memory strings go away

Oh right! I deleted the actual lines 60 and 63 instead of uncommenting them earlier. It works now! It reduced the program storage from 111% to 87%. Thanks!

Great to hear!
Have fun

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.