Problems with keeping time with millis

Hi, I am using a SD card to store the values of 3 analog input pins. Ideally, I want to take the value of the analog pins every 1 ms. Right now I have my code set up to take values every 50ms, just for safe measure with computing speed and what not ( I am using a Arduino Due and have tested so the speeds should work out). I am using a simply loop function to keep time.

void loop() {
  File dataFile = SD.open("gooon.csv", FILE_WRITE);
  looprate = 50;
  starttime = millis();
  endtime = starttime;
  interval = 10000;
  nextRefresh = millis();
  if (dataFile) {
    while((endtime-starttime) <= interval){
      if(millis() >= nextRefresh){
        nextRefresh = nextRefresh + looprate;
         // read three sensors and append to the sd:
          for (int analogPin = 0; analogPin < 3; analogPin++) {
            int sensor = analogRead(analogPin);
            dataFile.println(sensor);
              if(analogPin == 2){
                t=millis();
                dataFile.println(t);
               }
          }
          endtime=millis();
    }else{
    }
  }
  }else{
    Serial.println("Having problems retrieving the chosen file");
  } 
    dataFile.close();
    Serial.println("Done.");
    int v=1;
    while(v==1){}
}

This code should take analog pin values once every 50ms for a total of 10 seconds.
A few problems I have with the code are the following: For the first ~300ms, the code is retrieving the values at random rates, like every 7 ms then every 8ms or every 1ms, its random. Then after the first 70ms, the code works nearly perfectly, taking values every 50ms (although sometimes it gets off track by 2ms or so). Then the last problem is that it doesnt seem to stop at 10 secons. It starts at 3433ms then it ends at 10078ms, thats only ~7s.

I will attach a .csv file that shows the data from a sample run. It is organized in one column with 3 analog values then the millis clock value, then 3 analog values, then the millis clock value, etc etc.

Thank you for any help, I have been tweeking this for the last few hours and cant seem to fix it.

GOON.zip (1.11 KB)

What data type are the timing variables ?
How long does it take to write to the SD card ?

You are not using millis() properly. Have a look at Several Things at a Time

Don't use single character names for variables - for example 't'. It makes it impossible to highlight all the occurrences of the variable. Instead, ever damned 't' in the program gets highlighted. Use meaningful names.

...R

This code should take analog pin values once every 50ms for a total of 10 seconds.

Indeed it does. When I turn your snippet into code which compiles and runs I get data at 50 ms intervals over 10 seconds.

#include <SPI.h>
#include <SD.h>
byte looprate = 50;
unsigned long starttime = millis();
unsigned long endtime = starttime;
const int interval = 10000;
unsigned long nextRefresh = millis();
unsigned long t = millis();


void setup(){
  SD.begin(4); 
  Serial.begin(9600); 
}

void loop() {
  File dataFile = SD.open("gooon.csv", FILE_WRITE);
 
  
  if (dataFile) {
    while ((endtime - starttime) <= interval) {
      if (millis() >= nextRefresh) {
        nextRefresh = nextRefresh + looprate;
        // read three sensors and append to the sd:
        for (int analogPin = 0; analogPin < 3; analogPin++) {
          int sensor = analogRead(analogPin);
          dataFile.print(sensor);
          dataFile.print(',');
          if (analogPin == 2) {
            t = millis();
            dataFile.println(t);
          }
        }
        endtime = millis();
      }else {
      }
    }
  }  else {
    Serial.println("Having problems retrieving the chosen file");
  }
  dataFile.close();
  Serial.println("Done.");
  int v = 1;
  while (v == 1) {}
}

ForumTest.txt (3.32 KB)