Using IR Break Beam for Velocity

Hello All, I am currently trying to write an arduino program that calculates velocity by recording the time it takes to break two IR break beam sensors and dividing by distance. Unfortunately, I have limited experience with Arduino and can't quite figure it out. I have included my code below which I know is incorrect. Any help would be appreciated. I've read that using interrupts can help me with my code but do not quite understand them. Can I somehow include interrupts so that the timer starts when the first sensor changes state and then stops when the second sensor changes state? Thanks

unsigned long timeone; unsigned long timetwo; int sensoronepin= 2 ; int sensoronevalue = HIGH ; int sensortwopin= 3; int sensortwovalue = HIGH; float separation= 10; float duration; float velocity;

void setup() { pinMode(sensoronepin, INPUT); pinMode(sensortwopin, INPUT);

}

void loop() {

sensoronevalue = digitalRead(sensoronepin); if(sensoronevalue = LOW) { timeone= micros(); }

sensortwovalue = digitalRead(sensortwopin); if(sensortwovalue = LOW) { timetwo = micros(); } duration = (timetwo- timeone); velocity = (separation)/(duration);

}

= is not ==

if (x == y)
{
}

How fast do you expect your subject(s) to be moving and what is the physical separation (distance) between the two sensors? Bear in mind that the resolution of micros() 'as is' is only 4us.

You have made a good start but it would be better if you detected when the beams [u]become[/u] broken rather than when they [u]are[/u] broken.

See the StateChangeDetection in the IDE