I am trying to create a project that compares how long two lasers shine upon a LDR. At the moment I am trying to time how long long each laser activates the LDR then compare the two values together.
And I am struggling to time how long the sensors's activation period is.
Currently I have an LDR hooked up to analog read, reading the voltage of a potential divider circuit.
When the laser shines on the ldr the voltage being read drops.
How would I measure the time that the measured circuit drops below a set threshold.
I have tried to convert the sensors reading to a digital reading and use pulse in to time it. But to no avail.
Now i am trying to create a while loop that counts each time it runs through the loop. from that i should be able to find the time that thr sensor was below the threshold right?
However because i only want to know the length of time that the sensor is below the threshold and not the total time. I have set the count to return to zero when it raises above the set threshold.
How would I keep original count number to time it?
Harry_S:
I am trying to create a project that compares how long two lasers shine upon a LDR. At the moment I am trying to time how long long each laser activates the LDR then compare the two values together.
Your description is not as clear as you might have thought.
Do you mean that there is a separate LDR for each laser?
Do you want to record the time (the value of millis() or micros() ) when LaserA starts and again when it finishes? And do the same thing for LaserB
Another important factor is what level of precision you want - will an error in the measurements of (say) +/- 5 millisecs be acceptable? If not, what do you require.
A photodiode as mentioned by @holmes4 will react more quickly.
It will be much easier to give useful advice if you describe the project you are working on so that we can see your question in its proper context.
If you were doing this yourself what would you do?
You would write down the time at which the reading first dropped below the threshold, then you would note down the time the reading rose back above the threshhold. Next you would subtract the first time from the second to determine how long the reading was below the threshold.
Now look at the millis() and micros() functions in the reference section of this site. They let you read the time in either milliseconds or microseconds.
Thank you for your help guys. Sorry if I was not clear enough, I plan on using two LDR's and two differnt lasers. The project aims to check the alignment of two rotating disks. by seeing when the two disks interupt their respective laser's.
Both disks move relatively slowly so accuracy only has to be inbetween 0.1 and 0.25s and I would prefer to use photodiodes however i didnt have any of them to hand.
So the question is do you need to make sure everything is perfectly aligned so that each laser goes though their respective encoder ring at the exact same time or are you just trying to make sure both motors are spinning at the same rate so that the time difference between inputs remains the same..
A LDR will output like a Bell Curve so I would grab on the rise and fall and average for a cleaner output.
There are two concepts that are key to this sort of problem.
First is that Arduino has a free running internal clock that can be accessed via the "millis()" and "micros()" functions. To get elapsed time of an event one can record the start time of the event, wait for the event to complete, get the end time, and subtract end time from start time to get the elapsed time.
Second is the concept of program state, that is, the current behavior of the program depends upon one or more previous events. In the case of timing a recurring pulse there are two states, we are either waiting for the pulse to start or we are waiting for the pulse to complete. When the state of the pulse hasn't change since we last looked at it, there is nothing to be done on the task of measuring its duration. Only when the light changes from its prior state does the program take any action beyond polling the sensor. When it comes on we record the current time, when it goes off we use that to calculate the elapsed time.
In the following the variable "lightOn" describes the current state, "startTime" holds the time at start of pulse, and when the light goes from on to off is used to calculate the elapsed time.
#define LDR1pin A0
#define ldrThreshold 500
boolean lightOn = false ;
unsigned long startTime ;
unsigned long onTime = 0 ;
int ldr1Value ;
void setup() {
Serial.begin(9600) ;
}
void loop() {
ldr1Value = analogRead(LDR1pin) ; // Read value of LDR
if (lightOn) { // If light state was on, check to see if it went off
if (ldr1Value < ldrThreshold) { // Did light go off?
onTime = millis() - startTime ; // Get current time and subtract from start time
lightOn = false ; // Set state to light off
Serial.print(onTime) ; // Do something with measurement
}
} else { // If light state was off, see if it has come on
if (ldr1Value > ldrThreshold) { // Did light come on?
startTime = millis() ; // Get start time of light turning on
lightOn = true ; // Set state to light on
}
}
}