Hi everyone,
I came across an interesting problem. I want to read a file from a USB thumb stick which worked perfectly fine with a previous sketch of mine. However, now I modified the code so that the program checks periodically whether a USB stick is attached and initialised or not. This part works fine, but now I get an error and the file cannot be opened and read anymore. I tried a couple of things (switching from if statements to while loops, existence of file, file path), but nothing helped. Any ideas??
I declared all variables globally and created the function:
void usbConnection() {
unsigned long connectMillis = millis();
if (connectMillis - tUSBCheck >= tUSBCheckInterval) { // Check USB status periodically
tUSBCheck = connectMillis;
if (!msd.connected()) {
lv_label_set_text(label, "No USB drive found.");
static lv_style_t style;
lv_style_init(&style);
lv_style_set_text_color(&style, lv_palette_main(LV_PALETTE_RED));
lv_obj_add_style(label, &style, 0);
Serial.println("MSD not connected");
usbConnected = false; // Update flag to false if not connected
msd.connect(); // Attempt to connect to USB drive
delay(500);
}
else if (msd.connected() && !usbConnected) {
lv_label_set_text(label, "USB drive found.");
static lv_style_t style;
lv_style_init(&style);
lv_style_set_text_color(&style, lv_palette_main(LV_PALETTE_GREEN));
lv_obj_add_style(label, &style, 0);
Serial.println("MSD connected");
usbConnected = true; // Update flag to true if connected
}
}
}
I call this function in void loop() { usbConnection(); }
and to read the file on the USB stick, I created:
void btnLoadFileClicked(lv_event_t * e) {
Serial.println("Load file button clicked");
if (!msd.connected()) {
Serial.println("USB not connected. Please connect a USB drive.");
return;
}
// Opening .txt file for loading of parameters
delay(2500);
settings = fopen("/usb/settings.txt", "r");
if (settings) {
Serial.println("Opened file");
// logic for file handling
fclose(settings);
Serial.println("Closed file");
} else if (!settings) {
Serial.println("Failed to open file");
}
}
I'm always getting "Failed to open file" in the serial monitor...
I am wondering if the problem actualy occurrs because the USB port is basically busy doing something else (msd.connected()) and thus the file cannot be opened. I also tried changing/adding/removing delays in the code snippets.
Follow this guide's Guide to Arduino GIGA USB Features USB Mass Storage section.
If you have issue with the example, do the setps below before trying the examples again.
Add the Arduino Mbed OS Giga Boards (newest version) to Arduino IDE 2 using the Boards Manager.
I have a new GIGA R1 WiFi and I cannot access a USB drive. I am using:
IDE v2.3.6
Arduino_USBHostMbed5 v 0.3.1
Arduino Mbed OS GIGA Boards v 4.4.1
When I load the provided Arduino_USBHostMbed examples ( I have tried DirList and FileRead) the msd.connect() always returns a False indicating a failure to connect to the USB device that is plugged into to USB-A port. The USB drive is formatted FAT32 with MSBR. I have tried several USB devices - same result.
I have also done a WiFi Firmware update and reset the flash memory as indicated above. I have not done a Burn of the bootloader (this looks a bit complex for me), but I can load and run other programs on to the GIGA without problems.
Hi Geoff, it is very fussy about the particular drive being used. see here Unreliable file writing on connected flash drive
I'm currently using Kingston 64GB DataTraveler Micro Metal Flash Drive USB 3.2 without issue.
I would suggest getting a drive known to work before investigating further
Don't know what else to suggest other than my previous recommendation to try a stick known to work. If that doesn't, then I'd RMA the board as defective.