You will need pin change detection; detect when the pin goes high, not when it is high. As it stands currently, you keep setting the start variable while the pin is high.
You might also suffer from button bounce. When you press or release a button, it goes a few times high-low-high... or vice versa.
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
uint32_t start,elapseTime,endtime;
void setup()
{
pinMode(buttonPin, INPUT); // initialize the button pin as a input
pinMode(ledPin, OUTPUT);// initialize the LED as an output:
Serial.begin(9600);// initialize serial communication:
}
void loop()
{
buttonState = digitalRead(buttonPin);// read the pushbutton input pin:
if (buttonState != lastButtonState) // compare the buttonState to its previous state
{
if (buttonState == HIGH) // if the state has changed, increment the counter
{
start = micros(); // if the current state is HIGH then the button went from off to on:
}
else
{
endtime = micros();// if the current state is LOW then the button went from on to off:
}
}
lastButtonState = buttonState; // save the current state as the last state, for next time through
elapseTime = endtime - start ;
Serial.println(elapseTime);
delay(500);
}
If you want accurate timing, try the Input Capture feature of Timer1. It will record the time of a pulse edge with a resolution of 1 clock tick (1/16th of a microsecond). Read the time of the falling edge and subtract the time of the rising edge to get the pulse width. Because it is a hardware function you have to use the ICP1 (Input Capture Pin, Timer1) pin which, on the Arduino UNO, is Pin 8. Because the time is saved in the Input Capture Register before the interrupt is signaled, there is no problem with interrupt latency.
// Measures the width of positive pulses on Arduino UNO Pin 8 (ICP1)
// Should work for up to 65535 clock cycles (4095 microseconds)
void setup()
{
Serial.begin(115200);
noInterrupts (); // protected code
// reset Timer 1
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TIFR1 |= (1 << ICF1); // clear Input Capture Flag so we don't get a bogus interrupt
// start Timer 1, no prescaler
TCCR1B |= (1 << CS10); // plus Input Capture Edge Select (rising on D8)
TCCR1B |= (1 << ICES1); // Input Capture Edge Select (1=Rising, 0=Falling)
TIMSK1 |= (1 << ICIE1); // Enable Timer 1 Input Capture Interrupt Enable
interrupts ();
}
volatile uint16_t PulseTime = 0;
ISR(TIMER1_CAPT_vect)
{
static uint16_t RisingEdgeTime = 0;
static uint16_t FallingEdgeTime = 0;
if (PulseTime == 0)
{
if (TCCR1B & (1 << ICES1))
{
// Rising Edge
RisingEdgeTime = ICR1;
TCCR1B &= ~(1 << ICES1); // Switch to Falling Edge
}
else
{
// Falling Edge
FallingEdgeTime = ICR1;
TCCR1B |= (1 << ICES1); // Switch to Rising Edge
PulseTime = FallingEdgeTime - RisingEdgeTime;
}
}
}
void loop()
{
uint16_t pulseTime = 0;
noInterrupts();
pulseTime = PulseTime;
interrupts();
// If a sample has been measured
if (pulseTime)
{
// Display the pulse length in microseconds
Serial.println(pulseTime / 16.0, 2);
delay(1000); // Slow down output
// Request another sample
noInterrupts();
PulseTime = 0;
interrupts();
}
}