Problem register time with Micros() to determine speed

Hey guys,

Sorry to bother you with another beginners question.

I’m working on a simple code that i want to use to determine the speed a which a laser beam is crossed.
My idea is to register time2a when the beam is just touched and time2b when the beam is almost totally blocked. Problem is that only time2a is registerd. Time2b follows standard 4 micros later, not infuenced by speed of crossing the laser beam. What i’m i doing wrong?
This is my code:

int sensePin2 = A3;
int Val2 = 0;
int time2c;
int d2 = 190;
unsigned long time2a, time2b;


void setup() {

 pinMode(sensePin2, INPUT); 
 Serial.begin(9600);
}


void loop() {


Val2 = analogRead (sensePin2);

if (Val2 < d2)
{ time2a = micros(); // moment at which beam is just touched

if (Val2 < d2 - 10) 
{ time2b = micros(); // moment at which beam is almost totally blocked

time2c = time2b - time2a; // timedifference i want to use later to determine speed of crossing the beam

Serial.println(time2a); 
Serial.println(time2b); 
Serial.println(time2c); 
delay(1000);

  }
}

}

analogRead takes about 100us to execute.
Does that help?

Please remember to use code tags when posting code.

I'm working on a simple code that i want to use to determine the speed a which a laser beam is crossed.
My idea is to register time2a when the beam is just touched and time2b when the beam is almost totally blocked.

Using what kind of sensor? I doubt there is anything that reacts fast enough to tell when a laser beam is partially blocked.

Please use code tags.

Read this before posting a programming question

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

This

if (Val2 < d2)

and this

if (Val2 < d2 - 10)

can both be true at the same time. Is that what you intend?

…R

I’m using a photodiode because thats whats used in a laserharp on youtube. unfortunately the code is way over my head.

if (Val2 < d2) and if (Val2 < d2 - 10) should NOT be true at the same time so i think you already found a major flaw.

how can i fix that?

(thanks already to everybody that posted!)

You don't want to test for the value being less than a threshold, you want to test for
it going below a threshold (ie it is now below it, the last sample wasn't) - you are
looking for a transition, not a steady state.

There are ways to speed up analogRead() by monkeying with the ADC clock prescalar,
at the expense of accuracy, perhaps this might be useful.

Go and understand the StateChangeDetection example and generalise it?

stefanx:
if (Val2 < d2) and if (Val2 < d2 - 10) should NOT be true at the same time so i think you already found a major flaw.

how can i fix that?

One way would be like this

if (Val2 < d2 - 10) {
   // do stuff
}
else if (Val2 < d2) {
  // do other stuff
}

…R

Ok! I'm going to study the posted suggestions.
Thanks guys!