Hey guys,
I have a problem with the Arduino Giga using an USB stick to write the measured values into a file. It all works when connected to the PC. But it doesn't update the file when i power the board with a powerbank. I have formatted the USB Drive to FAT32 and MBR, but it still does not work.
Here is my code:
#include <Arduino_USBHostMbed5.h>
#include <DigitalOut.h>
#include <FATFileSystem.h>
#include <Wire.h>
#include <SparkFun_u-blox_GNSS_Arduino_Library.h>
#include <LiquidCrystal_I2C.h>
SFE_UBLOX_GNSS myGNSS;
long lastTime = 0;
USBHostMSD msd;
mbed::FATFileSystem usb("VOLUME");
void setup() {
Serial.begin(115200);
pinMode(PA_15, OUTPUT);
digitalWrite(PA_15, HIGH);
while (!Serial);
while (!msd.connect()) {
delay(1000);
}
Wire.begin();
if (myGNSS.begin() == false) {
Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing."));
while (1);
}
Serial.println("Mounting USB device...");
int err = usb.mount(&msd); // Mount USB device
if (err) {
Serial.print("Error mounting USB device ");
Serial.println(err);
while (1);
}
Serial.println("USB device mounted.");
}
void loop() {
if (!msd.connected()) {
msd.connect(); // Reconnect USB device if disconnected
}
if (millis() - lastTime > 1000) {
lastTime = millis();
byte fixType = myGNSS.getFixType();
Serial.print(F(" Fix: "));
if(fixType == 0) Serial.print(F("No fix"));
else if(fixType == 1) Serial.print(F("Dead reckoning"));
else if(fixType == 2) Serial.print(F("2D"));
else if(fixType == 3) Serial.print(F("3D"));
else if(fixType == 4) Serial.print(F("GNSS + Dead reckoning"));
else if(fixType == 5) Serial.print(F("Time only"));
long latitude = myGNSS.getLatitude();
long longitude = myGNSS.getLongitude();
Serial.print("Latitude: ");
Serial.println(latitude);
Serial.print("Longitude: ");
Serial.println(longitude);
long speed = myGNSS.getGroundSpeed();
long altitude = myGNSS.getAltitude();
long altitudeMSL = myGNSS.getAltitudeMSL();
long heading = myGNSS.getHeading();
// Open file on USB stick
FILE *f = fopen("/VOLUME/gps_data.txt", "a"); // Open file in append mode
if (f == NULL) {
Serial.println("Error opening file!");
} else {
// Write all GPS data to file
fprintf(f, "Latitude: %ld, Longitude: %ld, Speed: %ld, Altitude: %ld, AltitudeMSL: %ld, Heading: %ld\n",
latitude, longitude, speed, altitude, altitudeMSL, heading);
fclose(f); // Close the file
Serial.println("Data written to USB stick.");
}
}
}