Hi. I noted that in your source code you are using a while loop with a digitalRead.
While this will work, you can optimise your code, as the while and especially the digitalRead are quite slow.
For a faster more accurate reading I recommend setting up Pin Interrupts, known as an ISR (Interrupt Service Requests) these interrupts can be raised internally in the Atmel AVR processor independent of code execution. On the arduino you can access these using the attachInterrupt() function.
( https://www.arduino....ttachinterrupt/ )
I have written my own chrony code in the past.
My Chrono code is below. I've bolded the interrupt parts/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// If using software SPI (the default case):
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
// -------------------------------------------------------------------
// Interrupt Service Variables
const byte interruptPin1 = 3;
const byte interruptPin2 = 2;
volatile bool m_pin1_active = false;
volatile bool m_pin2_active = false;
volatile float m_time1 = 0;
volatile float m_time2 = 0;
// -------------------------------------------------------------------
// Variables
// distance between sensors = 170mm or 6.69291 inches
// distance between sensors 557742.5
unsigned long m_distance = 557742; // distance between sensors in inches
int m_numTimings = 0;
float m_fps[10];
float m_currentFPS = 0;
float m_avgFPS = 0;
float m_lowFPS = 99999.0;
float m_highFPS = 0;
float m_totalFPS = 0;
static String m_stringCurrent = "Current: ";
static String m_stringFPS = " FPS";
static String m_stringHi = "Hi:";
static String m_stringLow = ": Low:";
static String m_stringAvg = "Average: ";
// -------------------------------------------------------------------
// Interrupt Service Functions
void Pin1_ISR()
{
m_pin1_active = true;
m_time1 = micros();
}
void Pin2_ISR()
{
m_pin2_active = true;
m_time2 = micros();
}
// -------------------------------------------------------------------
void displayText( String string, int x, int y, int textSize = 1, int stride = 6)
{
int textCursor = x;
for ( int i = 0; i < string.length(); ++i)
{
display.drawChar(textCursor, y, string[i], WHITE, BLACK, textSize);
textCursor += stride;
}
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC);
pinMode(interruptPin1, INPUT_PULLUP);
pinMode(interruptPin2, INPUT_PULLUP);
attachInterrupt( digitalPinToInterrupt(interruptPin1), Pin1_ISR, RISING );
attachInterrupt( digitalPinToInterrupt(interruptPin2), Pin2_ISR, RISING );
display.clearDisplay();
pinMode(A0, INPUT);
}
void checkISRPins()
{
if( m_pin1_active && m_pin2_active )
{
if( m_time2 > m_time1)
{
float timeDiff;
timeDiff = m_time2 - m_time1;
float fps = m_distance / timeDiff;
m_currentFPS = fps;
m_numTimings++;
if( m_currentFPS < m_lowFPS )
{
m_lowFPS = m_currentFPS;
}
if( m_currentFPS > m_highFPS )
{
m_highFPS = m_currentFPS;
}
m_totalFPS += m_currentFPS;
m_avgFPS = m_totalFPS / m_numTimings;
}
// reset ISR variables
m_pin1_active = false;
m_pin2_active = false;
m_time1 = 0.0f;
m_time2 = 0.0f;
}
}
void displayResults()
{
display.clearDisplay();
String outString = "";
outString = m_stringCurrent + m_currentFPS + m_stringFPS;
displayText(outString, 1, 1 );
outString = m_stringAvg + m_avgFPS + m_stringFPS;
displayText(outString, 1, 11);
outString = m_stringHi + m_highFPS + m_stringLow + m_lowFPS;
displayText(outString, 1, 21);
display.display();
}
void loop()
{
checkISRPins();
displayResults();
delay(100);
}