Arduino Every and Adafruit VS1053

Hi All,

I am having an issue getting the Adafruit_vs1053 library to work with SDFAT.

I get this error on compile, although it all seems to work, the file doesn't actually seem to play.

#define PREFER_SDFAT_LIBRARY 1

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SdFat.h>
#include <Wire.h>
#include <hd44780.h>           // Main hd44780 library
#include <hd44780ioClass/hd44780_I2Cexp.h>  // I2C expander I/O class

// Declare the lcd object
hd44780_I2Cexp lcd;  // Automatically handles I2C backpack

// LCD geometry (20 columns and 4 rows)
const int LCD_COLS = 20;
const int LCD_ROWS = 4;

// Pin Definitions
#define BREAKOUT_RESET  9      // VS1053 reset pin
#define BREAKOUT_CS     10     // VS1053 chip select pin
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin
#define CARDCS 4               // SD Card chip select pin
#define DREQ 3                 // VS1053 Data request pin
#define NEXT_BUTTON_PIN 2      // Next button pin
#define PREV_BUTTON_PIN 5      // Previous button pin
#define PLAY_PAUSE_BUTTON_PIN 6 // Play/Pause button pin

// Adafruit VS1053 FilePlayer Object
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);

// Rename SdFat object to SD for consistency with the standard SD library
SdFat SD;
SdFile root;
SdFile currentFile;
char currentFileName[20];  // Reduced size to 20
bool isPlaying = false;
int currentIndex = 0;
int totalFiles = 0;
String mp3Files[50];  // Buffer to store up to 50 .mp3 file names

void setup() {
  Serial.begin(9600);
  
  // Initialize the LCD
  int status = lcd.begin(LCD_COLS, LCD_ROWS);
  if (status) {
    while (1);  // Hang if LCD initialization fails
  }
  lcd.backlight();  // Turn on the LCD backlight
  lcd.print("Initializing...");

  // Initialize buttons
  pinMode(NEXT_BUTTON_PIN, INPUT_PULLUP);
  pinMode(PREV_BUTTON_PIN, INPUT_PULLUP);
  pinMode(PLAY_PAUSE_BUTTON_PIN, INPUT_PULLUP);

  Serial.println(F("Initializing VS1053..."));
  if (!musicPlayer.begin()) {
    Serial.println(F("Couldn't find VS1053, check your wiring."));
    lcd.clear();
    lcd.print(F("VS1053 Error"));
    while (1);
  }
  Serial.println(F("VS1053 found"));
  lcd.clear();
  lcd.print(F("VS1053 OK"));

  Serial.println(F("Initializing SD card..."));
  if (!SD.begin(CARDCS, SPI_FULL_SPEED)) {  // Initialize SdFat with full speed
    Serial.println(F("SD card failed, or not present"));
    lcd.clear();
    lcd.print(F("SD Card Error"));
    while (1);
  }
  Serial.println(F("SD card initialized"));
  lcd.clear();
  lcd.print(F("SD OK"));

  // Open root directory and list .mp3 files
  if (!root.open("/")) {
    Serial.println(F("Failed to open root directory."));
    lcd.clear();
    lcd.print(F("Root Dir Error"));
    while (1);
  }
  Serial.println(F("Root directory opened successfully."));

  // List all .mp3 files and store them in the array
  listMP3Files();

  // Display the first file on the LCD
  displayFile(currentIndex);
}

void loop() {
  // Handle button presses
  if (digitalRead(NEXT_BUTTON_PIN) == LOW) {
    Serial.println("Next button pressed");
    nextFile();  // Scroll to the next file
    delay(300);  // Debounce
  }

  if (digitalRead(PREV_BUTTON_PIN) == LOW) {
    Serial.println("Previous button pressed");
    prevFile();  // Scroll to the previous file
    delay(300);  // Debounce
  }

  if (digitalRead(PLAY_PAUSE_BUTTON_PIN) == LOW) {
    Serial.println("Play/Pause button pressed");
    if (isPlaying) {
      Serial.println(F("Stopping playback"));
      musicPlayer.stopPlaying();
      lcd.setCursor(0, 1);
      lcd.print(F("Stopped         "));
      isPlaying = false;
    } else {
      playCurrentFile();  // Play the currently selected file
    }
    delay(300);  // Debounce
  }

  // Continue playing in background
  if (isPlaying && !musicPlayer.playingMusic) {
    Serial.println(F("Track finished"));
    lcd.setCursor(0, 1);
    lcd.print(F("Finished        "));
    isPlaying = false;
  }
}

void listMP3Files() {
  currentIndex = 0;
  totalFiles = 0;
  SdFile entry;
  
  // Loop through files in root directory
  while (entry.openNext(&root, O_RDONLY)) {
    if (!entry.isDir()) {
      entry.getName(currentFileName, sizeof(currentFileName));
      String filename = String(currentFileName);
      if (filename.endsWith(".mp3")) {
        mp3Files[totalFiles] = filename;
        Serial.print(F("Found file: "));
        Serial.println(mp3Files[totalFiles]);
        totalFiles++;
        if (totalFiles >= 50) break;  // Limit to 50 files
      }
    }
    entry.close();
  }
  root.rewind();  // Rewind to start
  Serial.print("Total .mp3 files: ");
  Serial.println(totalFiles);
}

void nextFile() {
  if (currentIndex < totalFiles - 1) {
    currentIndex++;
  } else {
    currentIndex = 0;  // Loop back to the first file
  }
  displayFile(currentIndex);
}

void prevFile() {
  if (currentIndex > 0) {
    currentIndex--;
  } else {
    currentIndex = totalFiles - 1;  // Loop to the last file
  }
  displayFile(currentIndex);
}

void displayFile(int index) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(mp3Files[index]);  // Display the file name on the LCD
}

void playCurrentFile() {
  String selectedFile = mp3Files[currentIndex];
  if (selectedFile.endsWith(".mp3")) {
    Serial.print(F("Playing file: "));
    Serial.println(selectedFile);
    musicPlayer.startPlayingFile(selectedFile.c_str());  // Play the file
    lcd.setCursor(0, 1);
    lcd.print(F("Playing...     "));
    isPlaying = true;
  }
}

The error:

/Users/joel/Documents/Arduino/libraries/SdFat/src/SdFat.h:459:16: note: type 'struct File' should match type 'struct File'
typedef FsFile File;
^
/Users/joel/Documents/Arduino/libraries/SD/src/SD.h:28:9: note: the incompatible type is defined here
class File : public Stream {
^
Not sure if this is actually an error, but looking at the limited documentation, using #define PREFER_SDFAT_LIBRARY 1 should make the Adafruit library use SDFAT library.

A few SdFat.h exist. Which did you use?
This one does not have your error on line 459.

1 Like

I am using the standard SDFat Library from the IDE.

Which one? Is it up-to-date? Who is the author?

I found one SdFat library, SdFat.h file, line 459 showing this:

typedef FsFile File;

There is no other reference to FsFile (the struct error) in that file, but if you auger down into the dependencies... which I did not... there will be the FsFile struct (I am certain).

I have a feeling you only have the .h file, and not the full library. Uninstall and reinstall SdFat library that you are using.

2.2.3 by Bill Greiman. Standard version for the Arduino library tools