Reading in hex/binary values from an SD Card

Hi,

I was trying to output audio out of my Arduino UNO through PORTD but I was having trouble reading in the data from an SD card. I have a wav file converted to a list of bytes. I have got the audio to output correctly when I saved the bytes to the internal program memory using "PORTD = pgm_read_byte(&(data[i++]));" For the new code (on the SD), I'm trying to use "PORTD = myFile.read()." Does anyone know what I'm doing wrong? The current format of my txt file is a list of hex values separated by a comma.

Does anyone know what I'm doing wrong?

Yes; you're not giving us enough information.

The current format of my txt file is a list of hex values separated by a comma.

And you're converting the (presumably) ASCII hex values to binary before writing them, right?

No, I’m not converting the hex values to binary. I thought .read() would read the hex value from the text file and output to the binary values to PORTD. When I stored the hex values onto the internal memory, I used PORTD = pgm_read_byte(&(data[i++])) and it worked fine.

This is what my data array looks like:

const unsigned char data[11] PROGMEM ={0x52,0x49,0x46,0x46,0x44,0x24,0x00,0x00,0x57,0x41,0x56};
(but with way more values of course).

For my second program where I’m trying to read these values from a text file, this is what the code looks like:

#include <SPI.h>
#include <SD.h>

void setup()
{
Serial.begin(9600);
DDRD = 255;
pinMode(10, OUTPUT);
File myFile = SD.open(“sample.txt”);
if (myFile) {
while (myFile.available()) {
PORTD =myFile.read()
delayMicroseconds(68);
}
myFile.close();
} else {
// if the file didn’t open, print an error:
Serial.println(“error opening sample.txt”);
}
}

I know the program doesn’t loop, I kept it in Setup just to test if it works. What other information do I need?

I thought .read() would read the hex value from the text file and output to the binary values to PORTD

The current format of my txt file is a list of hex values separated by a comma.

I think you've answered your own question.

I still don't get it, I tried it but it doesn't work. The documentation for .read() says that it reads a byte but my audio doesn't sound right. I don't believe it properly sends out the byte to PORTD.

The documentation for .read() says that it reads a byte

I'd say that's right. A comma is a byte, a '0' is too, as is a 'x'. So, a comma delimited list of hex values is going to need a little work to get into a state you can write directly to a bunch of pins.

I understand the problem now. Thanks AWOL!!