Over some years I've developed a big sketch to monitor and display things like motion and electrics on an electric scooter. It works real well on a Mega with an Adafruit Ultimate GPS Logger shield logging time, position, speed and much more to SD. The whole sketch compiles to about 65,000 bytes.
Recently I've been trying to do a cut down version for UNO but even after cutting out as much as I can it's a squeeze. Oh and I can't get it to work yet, the SD card simply fails to open the file on the microSD ![]()
Here's the routine that writes a header into the CSV file. It only fails on UNO. The bits commented out just to reduce sketch size all work on a MEGA, or if the GPS code and activity is disabled:
void logHeader(){ // write header rows to CSV file
dataString = CJ_ID;
//String columnHeadings = "";
//columnHeadings = "#,millis,MM,DD,hh,mm,ss,Day,Lat,Long,kph,course,ALTg,ALTb,Amps,Volts,Watts,C_Ard,C-Motor,C-Brake,kJrun,kJchg,gap,Mode,Hits";
File dataFile = SD.open(logFileName, FILE_WRITE);
if (dataFile) {dataFile.println(dataString); // if the file is available, write to it:
Serial << " Hdr:" << dataString << "\n"; // and report
//dataFile.println(columnHeadings); // write the row of column headings
}
else {Serial << "Hdr OPEN FAIL [" << logFileName << "]\n";
SD_OK = false;}
dataFile.close();
}
One possibility is it's still simply too big for UNO, but I'm not sure. Here's the compile data for my most stripped down version:
Sketch uses 24,524 bytes (76%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,748 bytes (85%) of dynamic memory, leaving 300 bytes for local variables. Maximum is 2,048 bytes.
I added a Check_mem() routine that reports free memory of only around 100 bytes, and stack pointer minus heap pointer of only around 200 bytes. I'm not sure if these are too low / tight.
If I comment out all the GPS functionality the SD works, but I can't get them working together. And it's not hardware as I've tried on 2 UNOs and 2 GPS shields, same results. Obviously the sketch is much smaller when GPS commented out so that suggests it's a size problem.
But I think there may be another possible cause: a timer/counter or interrupt conflict between the various libraries. I am using the libraries:
/* -- Included libraries ------------------------------------------------------------ */
#include <SoftwareSerial.h>
#include <Adafruit_GPS.h>
Adafruit_GPS GPS(&GPSport); // initalise GPS object
#include <SPI.h> // for microSD comms
#include <SD.h> // microSD library
#include <Streaming.h> // for simplified serial string commands
#include <TimedAction.h> // for loop timing
I have used derived GPS handling from the Adafruit "Parsing" example which depends on an "Output Compare" interrupt from Timer/Counter 0.
The whole sketch is spread over 8 tabs so too big to post inline. But here is a dropbox link to the sketch in a ZIP for those who are keen
I'm hoping someone will point me to a reference guide somewhere describing which timer/counters are used in background by the common Arduino libraries and functions (like Serial.print), for UNO and MEGA.
Any and all suggestions welcomed! TIA