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)