Interrupts and Timers

I am trying to write a code for arduino due which starts a 32bit timer on external interrupt , saves the value in a variable and then resets the value when a new external interrupt comes and does this in loop.
Can someone please help me with the sketch.

The problem is in the code you didn't post. Please use code tags otherwise the forum software eats some of your code and turns it into smileys. 8)

okay so this is the sketch
what it does is , it writes value in t1 whenever interrupt is generated and then stores the previous value in t2. "X" stores the difference value i.e x=t1-t2
By this i can find the time difference between two interrupts.
I have generated the interrupt by using delay function and digitalWrite (as you can see in the sketch) and connected the output to pin2 (int pin of arduino).
by using this i get a resolution of 2^23 bit in arduino due . As arduino due is a 32 BIT uc i could use its resolution to count the same. If i could use the internal timers of arduino due that would help me a lot.
Help is highly appreciated.

volatile unsigned long t1;
volatile unsigned long t2;
volatile unsigned long x;
int flashpin = 10;

void isr()

void setup()

void loop()

void flash()

Do you need this to be a blocking function? That is it sits and waits until the input pin rises? Then just use pulseIn() or the NewPing library

The code you have seems to have no way to tell if the interrupt has happened. It just delays for thousands of milliseconds and then you may or may not have had an interrupt. There may have been several. Additionally, you re-attach the interrupt on every loop, which is wasteful, and you never zero out t1 for the next loop so you might be looking at the previous event, not the current one.

To do this with the highest possible resolution, you should be using the "input capture" mode of the timer. I have not successfully made this work myself, although I have tried. Look for some tutorials on input capture.

Thanks for the reply MorganS :slight_smile: , but i have got something else to ask.
okay framing my question in different way now.
I want to read the time between two input impulse sensed by using interrupt function of arduino and the time can be read by starting a counter on rising edge and storing the value just after a new rising edge comes and reseting the counter. The resolution of the time depends on the bits of counter whether it is 16 or 32.
so far i am not able to use the internal counters directly.
Could you please help me with the sketch?

I don't have a good sketch that demonstrates input capture. Maybe Nick Gammon does. Use Google. It knows him well. The Arduino playground might have an example. Google works on that too. Anything in "instructables" should be avoided though.

You did not answer my question: Can this block and wait or does your sketch have other work to do while it's waiting for the rising edge?

Then just use pulseIn() or the NewPing library

Does NewPing support Due? In the bitbucket notes for V1.7 it says:

“Removed support for the Arduino Due and Zero because they’re both 3.3 volt boards and are not 5 volt tolerant while the HC-SR04 is a 5 volt sensor. Also, the Due and Zero don’t support pin manipulation compatibility via port registers which can be done (see the Teensy 3.2).”

Good catch. I started following links from the old Ping library and I did read that line, but at that point I had forgotten that the OP had quite clearly specified "for Arduino Due".

Curious ... I don't see 5V intolerance as a good reason to remove Due support from the library. After all there's many many ways to damage an Arduino and components (don't ask ! :slight_smile: ), and it would be easy enough to build a 5V tolerant circuit to drive the HC-SR04