Thanks a lot for the replies. This has been really helpful. I think I have a better understanding of what I need to do to get this thing working.
I understand why I'd bias the signal but how would I do that? would I be adding a 'DC' bias to the signal? if it is DC bias, would it need to be at 2.5 volts?
I was only trying to make a temporary file since I wasn't able to get the data to write directly from the analog pin. I was able to figure out that part earlier today with one of the guys in the lab but now I'm not so sure I will need it!
I did have two other questions about the write function in the code; after the very last line, is there anything I need to add to signify that its 'done'? I feel like I may be missing something here but I really don't know what. The other question was about the min/ max data values. What exactly is determining these values? Should they be preset to an amount based on my frequency range?
Thanks again for the assistance. A different version of the code is below
#include <SD.h>
#include <SPI.h>
const int chipSelect = 10;
int MIN_DATA_VALUE = 0;
int MAX_DATA_VALUE = 1024;
// from .wav writing file
/// The first 4 byte of a wav file should be the characters "RIFF" */
char chunkID[4] = {'R', 'I', 'F', 'F'};
/// 36 + SubChunk2Size
uint32_t chunkSize = 36; // You Don't know this until you write your data but at a minimum it is 36 for an empty file
/// "should be characters "WAVE"
char format[4] = {'W', 'A', 'V', 'E'};
/// " This should be the letters "fmt ", note the space character
char subChunk1ID[4] = {'f', 'm', 't', ' '};
///: For PCM == 16, since audioFormat == uint16_t
uint32_t subChunk1Size = 16;
///: For PCM this is 1, other values indicate compression
uint16_t audioFormat = 1;
///: Mono = 1, Stereo = 2, etc.
uint16_t numChannels = 1;
///: Sample Rate of file
uint32_t sampleRate = 44100;
///: SampleRate * NumChannels * BitsPerSample/8
uint32_t byteRate = 44100 * 2;
///: The number of byte for one frame NumChannels * BitsPerSample/8
uint16_t blockAlign = 2;
///: 8 bits = 8, 16 bits = 16
uint16_t bitsPerSample = 16;
///: Contains the letters "data"
char subChunk2ID[4] = {'d', 'a', 't', 'a'};
///: == NumSamples * NumChannels * BitsPerSample/8 i.e. number of byte in the data.
uint32_t subChunk2Size = 2116800; // You Don't know this until you write your data
File wavFile;
const char* filename = "fre110.wav";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// wait for Serial Monitor to connect. Needed for native USB port boards only:
while (!Serial);
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed.");
while (true);
}
Serial.println("initialization done.");
if (!SD.begin(10))
while (1);
wavFile = SD.open("freq110.wav", FILE_WRITE);
if (!wavFile)
while (1);
}
void writeWavHeader()
{
wavFile.write(chunkID,4);
wavFile.write((byte*)&chunkSize,4);
wavFile.write(format,4);
wavFile.write(subChunk1ID,4);
wavFile.write((byte*)&subChunk1Size,4);
wavFile.write((byte*)&audioFormat,2);
wavFile.write((byte*)&numChannels,2);
wavFile.write((byte*)&sampleRate,4);
wavFile.write((byte*)&byteRate,4);
wavFile.write((byte*)&blockAlign,2);
wavFile.write((byte*)&bitsPerSample,2);
wavFile.write(subChunk2ID,4);
wavFile.write((byte*)&subChunk2Size,4);
}
void loop()
{
int analogdata = analogRead(A2);
Serial.println(analogdata);
writeDataToWavFile(analogdata);
delay(500);
}
void writeDataToWavFile(int analogdata)
{
int16_t sampleValue = map(analogdata, MIN_DATA_VALUE, MAX_DATA_VALUE, -32767, 32767);
subChunk2Size += numChannels * bitsPerSample/8;
wavFile.seek(40);
wavFile.write((byte*)&subChunk2Size,4);
wavFile.seek(4);
chunkSize = 36 + subChunk2Size;
wavFile.write((byte*)&chunkSize,4);
wavFile.seek(wavFile.size()-1);
wavFile.write((byte*)&sampleValue,2);
}
Thanks again!