Wrote a timeMeasurement sketch that uses rising pulses on pin 2 and 3 as resp start and stop signal.
Might help to build your system
//
// FILE: timeMeasurement.ino
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.0.1
// PURPOSE: timeMeasurement Arduino UNO between 2 interrupts on pin 2 and 3 using timer1
// URL: http://forum.arduino.cc/index.php?topic=473669
//
// HISTORY:
// 0.0.1 - 2017-04-28 initial version
//
volatile bool inprogress = false;
volatile bool dataReady = false;
volatile uint32_t ticks = 0;
volatile uint32_t overflows = 0;
const float ticksPerSecond = 16000000.0;
const float sensorDistance = 0.2;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.println();
pinMode(2, INPUT);
pinMode(3, INPUT);
attachInterrupt(digitalPinToInterrupt(2), startTimer, RISING);
attachInterrupt(digitalPinToInterrupt(3), stopTimer, RISING);
}
void loop()
{
if (dataReady && inprogress)
{
// MATH
float duration = ticks / ticksPerSecond; // seconds
float distance = sensorDistance; // meter
float speed = distance / duration; // meter / second
// OUTPUT
Serial.print("time: ");
Serial.print(ticks / 16.0, 2); // 2 decimals
Serial.print(" usec ");
Serial.print("\tspeed: ");
Serial.print(speed, 3); // 2 decimals
Serial.println(" m/s");
// PREPARE NEXT MEASUREMENT
inprogress = false;
}
}
ISR (TIMER1_OVF_vect)
{
overflows++;
}
// note startTimer code itself takes
void startTimer()
{
// prevent start interrupt when in progress
if (inprogress) return;
inprogress = true;
dataReady = false;
TCCR1A = 0; //
TCCR1B = 0; // stop
TCNT1 = 0; // reset counter, overflows at 65536
overflows = 0; // reset overflowCount
OCR1A = 1; // clock pulses per increment TCNT1
TIMSK1 = bit(TOIE1); // set overflow interrupt
TCCR1B = bit(CS10); // start
}
void stopTimer()
{
if (dataReady) return; // prevent additional stop interrupt
dataReady = true;
TCCR1A = 0;
TCCR1B = 0;
TIMSK1 = 0;
ticks = overflows * 65536UL + TCNT1; // calculate clockticks.
}
// ---END OF FILE ---