Need help figuring out if my code is bad or my Arduino board is bad...

I am working on a project for my senior thesis using Arduino. I wanted to make sure Arduino was working correctly so I used Data logging code from the examples as well as converting my analog signal to voltage. I am putting a sin wave into Arduino through a function generator and it seems like there is some phase shifting going on. I have tried changing the Baud rate from 9600 to 19200, I have tried changing the delay, I have also tried just running Arduino without opening the Serial monitor because I noticed there seems to be a pause for half a second on the Serial monitor screen every once in a while(which is where I believe the phase shifting is coming from). I have attached my code as well as some pictures that I took from plotting the data on MATLAB. I have also verified using an Oscilloscope that the function generator is working properly. Can anyone help me figure out why I am getting this phase shifting and if it is from my code or from Arduino itself?

Datalogger_Analog_Voltage.ino (2.5 KB)

Opening and closing files on an SD card has a reputation for being slow. Do you have to open and close the file every round through loop? I don't recall how slow, but I remember someone else complaining about this recently.

Serial is slow relative to the Arduino, and if the serial output buffer fills up, calls to Serial.print/println will block until there's room in the buffer.

9600 baud takes just over 1 ms to send each byte (need 10 bits - start bit, 8 data bits, stop bit) - so if you're printing a several-character voltage, plus CR/LF sequence every round through, that's like 5 characters or so per loop iteration. Crank serial speed up to 115200 and serial should be able to keep up.

I tried putting the baud at 115200. I was still having the same issue. Could it be the SD card itself? I am using an ADATA 8GB SD card. I did a search on that specific card and it said that it can read/write at 4Mb/s.

Update!

So I went out and bought a new SD card that reads/writes at 80Mb/s and I am not having that issue anymore. For future, if anybody is having this same problem, it could be that your SD card is not reading/writing fast enough.

Cheers!