LDR Timer Trigger

Hi

My first post, I'm fairly new to programming and I need help resolving an issue I'm getting with my project.

I'm using an LDR (digital signal) to trigger a timer, when the value reads a 0 from the LDR I want it to start a timer and when the value returns to 1, stop the timer and print the time in milliseconds to the serial monitor.

This is a proof of concept test for a larger project for sorting lengths of beams (Lego) by monitoring the time each beam takes to fall through a shaft with an LDR and LED facing one another. The results will be compared to an array where the max value will be stored for each length of beam and eventually activate a servo which will direct the beam into the correct

The below sketch is what I've come up with so far and it semi-works as I'd like. I believe it's operating the timer sequence but always displays a 0 to the serial monitor - When the LDR reads a 0 from pin 8 the timer begins and starts the sub routine to monitor the LDR again for a change back to a 1. The time should then be displayed to the serial monitor but as mentioned, I always receive a 0, as if the millis() is resetting within the subroutine.

#define LDR 8

int sensorValue=0;

unsigned long startTime; // start time for timer

unsigned long elapsedTime; // elapsed time for timer

void setup() {

Serial.begin(9600);

int timerDis;
}

void loop() {

sensorValue = digitalRead(LDR); // read the LDR

if (sensorValue == 0){ // check for a high to low transition

startTime = millis(); // store current millis

timerDis(); // begin subroutine

}}

void timerDis(){

sensorValue = digitalRead(LDR); // read the LDR again

if (sensorValue == 1){ // check for a low to high transition

elapsedTime = millis() - startTime; // store current millis and subtract startime

Serial.println(elapsedTime); // Display the time the LDR was at a state of 0.

}

}

I've previously written code that will produce the results I'd like but lost it when a drive corrupted. It was a couple of years ago so I'm trying to recreate it but please offer suggestions to achieve this, I'm not convinced I'm tackling this with the correct method so any help is much appreciated.

Mark.

Warning: LDR's are notoriously slow so they might not be suitable for detecting the length of objects falling down a tube. Some kind of phototransistor or photodiode sensor might be a better choice.

Perhaps the pulseIn() function would be of use. It measures pulses with 4 microsecond precision.

If you want REALLY precise timing you can use the Input Capture pin on Timer1 of the Arduino UNO. This grabs the timer value on a falling or rising edge. If you are timing a LOW pulse you capture a falling edge and then the subsequent rising edge. If you run the timer at 16 MHz you can measure intervals up to about 4 milliseconds with a precision of 1/16th microsecond. You can also use the timer prescale to slow down the timer by 8 or 64 to get 32 milliseconds at 1/2 microsecond precision or 262 milliseconds at 4 microsecond precision.