Problem with millis() form analog input

Hello, that’s my first post on this forum.

I need to make a speed measurement system based on Arduino for my master thesis. I have no previous expierience with programming whatsoever, but I failed to say no for this…

So I’ve got 2 phototransistors connected to A0 and A1, there are lasers lighting them. If projectile passes between first set I want the clock to start a millis() (or micros), stop when it passes between second pair (values of light voltage for this are chosen well, I’ve tested diodes sensitivity). Can you advice me how can I make a proper choosing of loops for that case? I can’t manage to figure out a “waitng” for decrease of light.

Here’s my code:

int diode1 = A0;
int diode2 = A1;
long startTime;
long elapsedTime;
void setup() {
Serial.begin(9600);
Serial.println("Time measurement");

}

void loop() {
  // put your main code here, to run repeatedly:
int var1 = analogRead(diode1);
Serial.println(var1);
int var2 = analogRead(diode2);
Serial.println(var2);
if (var1 <= 900 && var2 >= 900)
{
   startTime = millis();
   Serial.println("Start ");
   
}
else  if (var2 <= 900)
    {
      elapsedTime = millis()- startTime;
      Serial.print("time in ms: ");
      Serial.println( (int)(elapsedTime));
     
    }
    else //don't know what should be here
    {
    }
}

What's wrong with what you got? What does it do or not do that doesn't meet with your expectations.

If you want to wait, don't use 'IF' as it requires another loop of testing. Try instead using 'WHILE' and set the condition such that the PR value keeps it in the loop until the threshold is breached (i.e. while(var2 > 900)).

Once that condition is reached, processing will flow to the next code segment which would then capture your second timestamp.

DKWatson:
If you want to wait, don't use 'IF' as it requires another loop of testing. Try instead using 'WHILE' and set the condition such that the PR value keeps it in the loop until the threshold is breached (i.e. while(var2 > 900)).

Once that condition is reached, processing will flow to the next code segment which would then capture your second timestamp.

Or better yet, make it a state machine and don't trap code in while loops waiting for stuff to happen. Let the loop function be your loop.

Would two analogReads and a bunch of printing in between be fast enough for a ?projectile?
Can't you use a (much faster) digital (interrupt) input.

Here's another recent post like that.
Leo..