Arduino Due timer 3 interrupt problem

Hello everbody,
First of all I am new to arduino programing,

I am using timer 3 interrupt on arduino due.
my code is below,.

my problem is when I print (number of interrupt - number of Readings) i get offset of about 170 for 1mS period of time.

If I reduce period , then offset increases.
for further increase it is not constant. I can understand why it is not constant for very small period,.
but why offset for 1mS.

My idea is read analog value fast and store in Sd card.

my Code is below

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

File myfile;
unsigned long mytime=0;
boolean Read=false;
boolean Save=false;
const int arraySize=512;
int numberOfintrupt=0;
int numberOfread=0;
int i=0;
int k=0;
int j=0;
boolean one=true;

int sensVal=0;
byte evenArray[arraySize];
byte oddArray[arraySize];
boolean even = false;
const int chipSelect = 4;

void setup() {

Serial.begin(250000); //BUAD RATE

Serial.print(“Initializing SD card…”);

if (!SD.begin(4)) {
Serial.println(“initialization failed!”);
return;
}
Serial.println(“initialization done.”);

if(SD.exists(“sensVal.bin”)){SD.remove(“sensVal.bin”);}

myfile=SD.open(“sensVal.bin”, FILE_WRITE);

digitalWrite(13,LOW);

Timer3.attachInterrupt(myIsr);
Timer3.start(1000);

}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

void myIsr(){

numberOfintrupt++;
Read=true;

}

void AnalogRead(){

numberOfread++;

if(even){
sensVal=analogRead(0);
evenArray*=sensVal;*

  • i++;*
    _ evenArray*=sensVal>>2;}_
    _
    else {_
    _
    sensVal=analogRead(0);_
    _ oddArray=sensVal;
    i++;
    oddArray=sensVal>>2;
    }*_

* i++;*

* if(i==512){*
* even=!even;*
* Save=true;*
* j++;*
* i=0;*
* }*

*} *

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void loop() {

* if(Read){*
* Read=false;*
* AnalogRead();*
* }*
* if(Save){*

* SavetoCard();*
* Save=false;*
* k++;*
* Serial.println(numberOfintrupt-numberOfread);*
* // Serial.println(j-k); *
* }*

* }*

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void SavetoCard(){
* if (even){*
* if(myfile){*
* myfile.write(oddArray,512);*
* }}*
else{
* if(myfile){*
* myfile.write(evenArray,512);*
* }}*

* myfile.write(0x0A);*
* myfile.write(0x0A);*

if(k==5){
* myfile.close();*
* digitalWrite(13,HIGH);}*
}

Edit: corrected the code.

Please, read this (#7) use code tags to post your code, so it would look

like this

Is an interrupt really necessary? You could remove your Interrupt handle and modify the loop to be:

uint32_t SampleTimer;

void loop() {

  if (millis() - SampleTimer > 1000) {
    AnalogRead();
    SampleTimer = millis();
  }

  if (Save) {

    SavetoCard();
    Save = false;
    k++;
    Serial.println(numberOfintrupt - numberOfread);
    // Serial.println(j-k);
  }
}

and it will read every 1000 milliseconds.

Thank you for your reply. but I really need to use interrupt to make sampling period constant. could you please tell me my method is correct?

i count number of interrupt and number of readings, after every 256 readings i save to card, so i count number of savings.

I hope if differences of those parameters are constant then process goes well.

correct?

thanks