Problem with wave shield library interfering with SD card re

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
dschmitty90
 
Posts: 6
Joined: Wed Jul 30, 2014 10:26 am

Problem with wave shield library interfering with SD card re

Post by dschmitty90 »

I have a code that reads a .txt file off an SD card, then eventually plays a .wav file from the SD card.

The code works and a sound is played when using the code given in the tutorials here: https://learn.adafruit.com/adafruit-wav ... o/play6-hc

however when I try to import those libraries, then use a function to read the SD card. The initialization fails.

When I try the following code, where ParseData is a function defined later that uses the SD.open() and file.read() etc., the serial port outputs nothing at all:

Code: Select all

#include <SD.h>
#include <FatReader.h>
#include <SdReader.h>
#include "WaveUtil.h"
#include "WaveHC.h"

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

File myFile; //used with SD card reader

char wavArray[6][13];            //actArray[i][j] refers to ith row jth column
char actArray[6][13];            //actArray[i][j] refers to ith row jth column
char list[12][13]={"L1.txt"};    //actArray[l][j] refers to lth row jth column, l is the stack number
int k=0;                         //stack index, starts at 0, list[0]="L1"

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);

  if (!SD.begin()) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  char list[12][13]={
    "L1.txt","L2.txt","L3.txt","L22.txt","L23.txt","L24.txt"    };
  for (int k=0;k<6;k++){
    int n=ParseData(list[k]);        
    Serial.println("wavArray:  actArray:  current list: ");//print out wavArray, actArray, stack number, and current list name
    for(int q=0;q<n;q++){
      Serial.print(wavArray[q]);
      Serial.print("  ");
      Serial.print(actArray[q]);
      Serial.print("  ");
      Serial.println(list[k]);
    }
    delay(1000);        
  }
}
void loop()
{

}

}
but when I comment out the libraries other than the SD.h, like the following, the code works perfectly.

Code: Select all

#include <SD.h>
//#include <FatReader.h>
//#include <SdReader.h>
//#include "WaveUtil.h"
//#include "WaveHC.h"
//
//SdReader card;    // This object holds the information for the card
//FatVolume vol;    // This holds the information for the partition on the card
//FatReader root;   // This holds the information for the filesystem on the card
//FatReader f;      // This holds the information for the file we're play
//
//WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time
if I leave in any library along with the SD.h library, the serial port outputs with weird characters, for example all I did was delete the slashes in front of waveHC, and got the following:
Initializing NNR+‹¥…±¥é¥¹NNR+‹¥…±¥é¥¹NN—
as you can see, I am not even getting past SD.begin. Is there something in your libraries that prevent or overwrite the SD.h functions?

I have tried various combinations of libraries but they all seem to be incompatible with SD.h, I cant seem to find a good source for what these libraries actually define either so I'm having trouble knowing how to use them.

Thanks

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Problem with wave shield library interfering with SD car

Post by adafruit_support_rick »

The WaveShield library includes an earlier version of the SD library. So you can't use SD with it. However, you ought to be able to do regular SD reads and writes using the card, vol and root objects that are already declared.

dschmitty90
 
Posts: 6
Joined: Wed Jul 30, 2014 10:26 am

Re: Problem with wave shield library interfering with SD car

Post by dschmitty90 »

So how would I write the following function using the card, vol and root objects? The setup code This function uses the "File" variable, SD.Begin, SD.open, file.read, file.available, and file.close SD library functions.

Is there some documentation somewhere that will tell me how to do the same thing using what is defined in your library?

In some of your examples there are functions like vol.init(card, part) and !f.open(root, name) how do all of these work?

Thanks for the help.

Code: Select all


#include <SD.h>

File myFile;
String bufferString; //buffer string that contains data from the text file, temporarily

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);

  if (!SD.begin()) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

    int n=ParseData("L90.txt");        
    Serial.println(bufferString);
 
}
void loop()
{

}

int ParseData(char* name)
{

  String bufferString; //buffer string that contains data from the text file, temporarily 
  uint8_t j=0;//row variable

  myFile = SD.open(name); //open file for reading
  if (myFile) 
  {//if file is open
    while (myFile.available()) 
    {//repeat until there is nothing else to be read on the file

      char c=myFile.read(); //read data from the text file

        bufferString+=c; //copy data to buffer string

    }
    // close the file:
    myFile.close();
  }
  else 
  {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  if (j>0)return(j);//return positive value equal to number of entries on each array
  else return(-1); //return negative value if no data is parsed
}

dschmitty90
 
Posts: 6
Joined: Wed Jul 30, 2014 10:26 am

Re: Problem with wave shield library interfering with SD car

Post by dschmitty90 »

basically I need to know how to open, read, do a while available loop, and close a file on an SD card, then play a wav file off the card

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Problem with wave shield library interfering with SD car

Post by adafruit_support_rick »

I hacked this together, but I haven't run it. Still, it ought to be enough to get you started:

Code: Select all

#include <WaveHC.h>
#include <WaveUtil.h>

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the volumes root directory
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
dir_t dirBuf;     // buffer for directory reads

/*
 * Define macro to put error messages in flash memory
 */
#define error(msg) error_P(PSTR(msg))

FatReader myFile;
String bufferString; //buffer string that contains data from the text file, temporarily

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);

  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!)  
    error("Card init. failed!");  // Something went wrong, lets print out why
  }
  
  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);
  
  // Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {   // we have up to 5 slots to look in
    if (vol.init(card, part)) 
      break;                           // we found one, lets bail
  }
  if (part == 5) {                     // if we ended up not finding one  :(
    error("No valid FAT partition!");  // Something went wrong, lets print out why
  }
  
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(), DEC);     // FAT16 or FAT32?
  
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    error("Can't open root dir!");      // Something went wrong,
  }
    int n=ParseData("L90.txt");        
    Serial.println(bufferString);
 
}

void loop()
{

}

int ParseData(char* name)
{
  FatReader myFile;
  
  String bufferString; //buffer string that contains data from the text file, temporarily
  
  char charBuf[1];
  uint8_t j=0;//row variable

  if (myFile.open(root, name)) //open file for reading
  {//if file is open
    while (myFile.read(charBuf, 1)) 
    {//repeat until there is nothing else to be read on the file

        bufferString+=charBuf; //copy data to buffer string

    }
    // close the file:
    myFile.close();
  }
  else 
  {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  if (j>0)return(j);//return positive value equal to number of entries on each array
  else return(-1); //return negative value if no data is parsed
}

/////////////////////////////////// HELPERS
/*
 * print error message and halt
 */
void error_P(const char *str) {
  PgmPrint("Error: ");
  SerialPrint_P(str);
  sdErrorCheck();
  while(1);
}
/*
 * print error message and halt if SD I/O error, great for debugging!
 */
void sdErrorCheck(void) {
  if (!card.errorCode()) return;
  PgmPrint("\r\nSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  PgmPrint(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}

dschmitty90
 
Posts: 6
Joined: Wed Jul 30, 2014 10:26 am

Re: Problem with wave shield library interfering with SD car

Post by dschmitty90 »

This is perfect thank you.

One question if you don't mind, what is the second parameter in the while (myFile.read(charBuf, 1)) statement? The first parameter is where you're going to store what you read, is the second how many spaces to read, which partition to use, a modifier of the function? Just in case.

Thanks again this is exactly what I needed

dschmitty90
 
Posts: 6
Joined: Wed Jul 30, 2014 10:26 am

Re: Problem with wave shield library interfering with SD car

Post by dschmitty90 »

one more question actually, does file.peek() work the same way in this older library?

that is, can you look at the the file without advancing it?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Problem with wave shield library interfering with SD car

Post by adafruit_support_rick »

dschmitty90 wrote:One question if you don't mind, what is the second parameter in the while (myFile.read(charBuf, 1)) statement?
That's the max number of characters to read (i.e., the size of the buffer). read returns the number of characters it actually managed to get (0 if it read nothing).
dschmitty90 wrote:one more question actually, does file.peek() work the same way in this older library?

that is, can you look at the the file without advancing it?
It doesn't have a peek function, but you can write your own, using read, readPosition and seekSet. Something like this:

Code: Select all

bool peek(char* charBuf)
{
  bool OK = false;
  if (myFile.read(charBuf, 1))       //read a character
  {
    myFile.seekSet(myFile.readPosition() - 1);      //seek back to the previous file position
    OK = true;
  }
  return OK:
}

dschmitty90
 
Posts: 6
Joined: Wed Jul 30, 2014 10:26 am

Re: Problem with wave shield library interfering with SD car

Post by dschmitty90 »

sounds good, thanks a lot for your help.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”