trying to data log to sd every millisecond and having problems

Hi, quick post. I’m trying to log data from two analog inputs called with analogRead and their inputs printed every millisecond. I also am printing the time in millis besides them for a time reference. I’m getting data on the sd card but its every 11 milliseconds. Anyhelp would much be appreciated. Here’s my code:

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

const int chipselect = 4;
File mysd;

int val;
int val2;
int volts;
int volts2;
unsigned long Time;

void setup() {
pinMode (10,OUTPUT);
pinMode (A0,INPUT);
pinMode (A1,INPUT);
SD.begin (chipselect);

void loop() {
Time = millis();
mysd = (“Data.txt”, FILE_WRITE);

if (mysd){
val = analogRead(A0);
volts = (val/1040);
val2 = analogRead(A1);
volts2 = (val2/1040);

if (volts > 2.5){

if (volts2 > 2.5){



How do I speed it up to every millisecond. I actually really need it at .5 millisecond speed but I’ll settle at 1 millisecond. Btw, I’m using a sd shield that goes ontop of my Uno. Thanks!

Mm, I don't know if you can. analogRead() already takes some time. You might want to look into that. Same for SPI speed of the SD library. But the biggest thing, why do you keep opening and closing the damn file if you want to write fast? If you want to make a grocery list fast, do you put the note book back into the drawer after every letter?

Couple of remakes:
-Please use code tags next time :wink:

if (volts2 > 2.5){

You do know volts2 is an int and int's don't have decimals. Aka, that line is the same as:

if (volts2 > 2){

Use code tags when posting, so that your code looks like this:

mysd = ("Data.txt", FILE_WRITE);

DO NOT open the file every pass through loop(), then close it again. This wastes a HUGE amount of time.

Open the file ONCE in setup() and then close the file when you are all done collecting data.

ok ok lol. I was wondering that about putting opening the sd in the void setup. Ill try that. I made a simpler code to test the speed that just had a single println. in the void loop and it got the speed down to 6 millis but not 1 milli. Is it the sd card that is my problem?

btw, turning the arduino off, will not close the SD cleanly.
Perhaps a button to close & stop - then show a LED when the SD is ready for removal.

I made a simpler code

Post the code, using code tags.

What is your thinking here? Do you not realize that the integer divide will always give 0 as a result?

 val = analogRead(A0);
  volts = (val/1040);

Most people use statements similar to these:

 val = analogRead(A0);
 float volts = 5.0*val/1024.;

What is your thinking here? Do you not realize that the integer divide will always give 0 as a result?

 val = analogRead(A0);

volts = (val/1040);

Missed that! :o

Mixing float in is not going to make it faster. Don't let the Arduino do math just because you need understandable units. Of at least do it the other way around so the compiler can do the heavy calculation:

if(analogRead(TestPin) >= (1024 * 2.5 / 5)){

But if you just want to detect >2,5V the analog comparator is faster.