Nano second?

s = ut + at2/2

Or just make the distance from the second photogate tot the target an integer multiple of the distance between the gates.

all i need is to write free fall speed and time

void Bullet()
{
  GetOut = false;
  //Set the screen
  lcd.drawText(1, 123, "Stop ", 0, RED, txtBGColor);
  unsigned int g = 9800;//[mm/s2]
  unsigned int d = 50800;//[um]
  unsigned long Vout, ST, EIT, Counter;//Speed calculated; Start time; End/Impact time; 
  if (digitalRead(1) == LOW)  // If sensor1 detects projectile
  {
    ST = micros();
    EIT = ST;
    while(digitalRead(2) != LOW)  // Look for sensor2 to detect projectile
    {
      EIT = micros();
      if (EIT - ST > 1500000000)  // If we have waited 1.5 second and there has not been 
      {                       //a detection,then the projectile must have missed the second sensor
        lcd.drawText(8, 80, "Detection failed.", 0, RED, txtBGColor);
        delay(5000);
        lcd.drawText(8, 80, "Detection failed.", 0, txtBGColor, txtBGColor);
        EIT = 0;
        break;
      }
    }
    Counter = micros();//Reset counter to compensate Computing time
    if (EIT)
    {
      PressFS(0, 0, 1);//Focus 1 On
      ST = EIT - ST;
      //Let's get the impact time
      if(ROM_VAL[ROM_Gravity].Value == 0)//Constant speed
      {
        Vout = (d / ST) * 1000;//[um/us]
        EIT = ROM_VAL[ROM_Target_Dist].Value/Vout;//Impact time
      }
      else//Free falling
      {
        
      }
      Counter = micros() - Counter;
      EIT = EIT  - Counter;//compensate Computing time
      while (micros() < EIT) {;}  // Wait for impact
      //trigger flash
      PressFS(1, 0, 1);//Shutter 1 On
      PressFS(1, 1, 1);//Shutter 2 On - flash
      // Display how fast the projectile was moving
      lcd.drawText(8, 80, "Speed", 0, RED, txtBGColor);
      // Turn off flash
      PressFS(1, 1, 0);//Shutter 2 Off
      PressFS(1, 0, 0);//Shutter 1 Off    
    }
  }
  PressFS(0, 0, 0);//Focus 1 Off
}
 if (digitalRead(1) == LOW)

I think I've already asked,but just to be sure, what if it reads HIGH?

Yep, i just modified the code i found more errors, when i will finish it i will post it here (maybe tonight)

Ok, now is finished

void Bullet()
{
  GetOut = false;
  //Set the screen
  lcd.drawText(1, 123, "Stop ", 0, RED, txtBGColor);
  unsigned int g = 9800;//[mm/s2]
  unsigned int d = 50800;//[um]
  
  unsigned long Vout, ST, EIT, Counter;//Speed calculated; Start time; End/Impact time; 
  PressFS(0, 0, 1);//Focus 1 On
  Counter = micros();
  while(!GetOut)
  {
    if ((micros() - Counter) > 4000000) {ShowError(); GetOut = true; break;}
    if (digitalRead(1) == LOW)  // If sensor1 detects projectile
    {
      ST = micros();
      EIT = 0;
      while(digitalRead(2) != LOW)  // Look for sensor2 to detect projectile
      {
        EIT = micros();
        if (EIT - ST > 1500000)  //If we have waited 1.5 second and there has not been 
        {                           //a detection,then the projectile must have missed the second sensor
          ShowError();
          GetOut = true;
          EIT = 0;
          break;
        }
      }
      Counter = micros();//Reset counter to compensate Computing time
      if (EIT)
      {
        ST = EIT - ST;
        //Let's get the impact time
        Vout = (d / ST);//[um/us]
        //ROM_VAL[ROM_Target_Dist].Value is in [cm]
        if(ROM_VAL[ROM_Gravity].Value == 0)//Constant speed   
          EIT = (ROM_VAL[ROM_Target_Dist].Value * 10000) / Vout;//Impact time
        else//Free falling
        {
          EIT =  ((pow(0.5, (float)(Vout * Vout + 20 * g * ROM_VAL[ROM_Target_Dist].Value)) - Vout) / g) * 1000000;
        }
        Counter = micros() - Counter;
        EIT = EIT  + Counter;//compensate Computing time
        while (micros() < EIT) {;}  // Wait for impact
        PressFS(1, 0, 1);//Shutter 1 On
        PressFS(1, 1, 1);//Shutter 2 On - flash
        // Turn off 
        PressFS(1, 1, 0);//Shutter 2 Off- flash
        PressFS(1, 0, 0);//Shutter 1 Off
        // Display speed
        lcd.drawText(8, 80, itoa(Vout/1000, Buffer, 10), 0, RED, txtBGColor);//display in [m/s]
      }
    }
  }
  PressFS(0, 0, 0);//Focus 1 Off
}
/////////////////////////////////////////////////////////
//Description: Display on lcd error message if any of the gates is missed
//Parameters: None
//Returns: None
/////////////////////////////////////////////////////////
void ShowError()
{
  lcd.drawText(8, 80, "Detection failed.", 0, RED, txtBGColor);
  delay(5000);
  lcd.drawText(8, 80, "Detection failed.", 0, txtBGColor, txtBGColor);  
}

[quote author=Udo Klein link=topic=52776.msg376946#msg376946 date=1298130448]So he better also accounts for the length of his wires if he wants to measure in that range ;)[/quote]

Sure. Also one should learn LVPECL, LVDS etc. electronics.

if (digitalRead(1) == LOW)

?

is DDRB |= (1<<4); equivalent pinMode(12, OUTPUT);?

here is the final function…i think :astonished:
do you think will do the job?

void Bullet()
{
  GetOut = false;
  //Set the screen
  lcd.drawText(1, 123, "Stop ", 0, RED, txtBGColor);
  lcd.drawText(8, 80, "    ", 0, txtBGColor, txtBGColor);
  //unsigned int g = 9800;//[mm/s2]
  //unsigned int d = 50800;//[um]
  unsigned long Vout, ST, EIT, Counter;//Speed calculated; Start time; End/Impact time; 
  PressFS(0, 0, 1);//Focus 1 On
  Counter = micros();
  while(!GetOut)
  {
    if ((micros() - Counter) > 4000000) {ShowError(); GetOut = true; break;}//If we have waited 4[s] and S1 is HIGH then raise error
    if (!bitRead(PINC, 2))  // If S1 detects projectile
    {
      ST = micros();
      EIT = 0;
      while(!bitRead(PINC, 3))  // If S2 detects projectile
      {
        EIT = micros();
        if (EIT - ST > 1500000)  //If we have waited 1.5[s] and S2 is HIGH then raise error 
        {
          ShowError();
          GetOut = true;
          EIT = 0;
          break;
        }
      }
      Counter = micros();//Reset counter to compensate Computing time
      if (EIT)
      {
        ST = EIT - ST;
        //Let's get the impact time
        Vout = 50800 / ST;//[um/us]
        //ROM_VAL[ROM_Target_Dist].Value is in [cm]
        if(ROM_VAL[ROM_Gravity].Value == 0)//Constant speed   
          EIT = (ROM_VAL[ROM_Target_Dist].Value * 10000) / Vout;//Impact time
        else//Free falling
        {
          EIT =  ((pow(0.5, (float)(Vout * Vout + 196000 * ROM_VAL[ROM_Target_Dist].Value)) - Vout) / 9800) * 1000000;
        }
        Counter = micros() - Counter - 10;
        EIT = EIT  + Counter;//compensate Computing time
        while (micros() < EIT) {;}  // Wait for impact
        PORTB |=  (1<<1);//Shutter 1 On FAST
        PORTD |=  (1<<7);//Shutter 2 On FAST - flash
        // Turn off 
        PressFS(1, 1, 0);//Shutter 2 Off- flash
        PressFS(1, 0, 0);//Shutter 1 Off
        // Display speed
        lcd.drawText(8, 80, itoa(Vout/1000, Buffer, 10), 0, RED, txtBGColor);//display in [m/s]
      }
    }
  }
  PressFS(0, 0, 0);//Focus 1 Off
  lcd.drawText(1, 123, "Start", 0, mText, txtBGColor);
}
void ShowError()
{
  lcd.drawText(8, 80, "Detection failed.", 0, RED, txtBGColor);
  delay(5000);
  lcd.drawText(8, 80, "Detection failed.", 0, txtBGColor, txtBGColor);
  lcd.drawText(1, 123, "Start", 0, mText, txtBGColor);  
}
if (!bitRead(PINC, 2))

And if it doesn't?

i don't understand your question if it doesn't go low and more than 4 s will pass then i will get out from the function

Don't you want to wait until it detects a projectile, rather than just testing once at some arbitrary point in time?

not really the function is restarted at the press of a button in the worst case i can increase the waiting time

if you have another idea, please share it, maybe my approach is wrong

But don't you think that executing this:

if ((micros() - Counter) > 4000000)

every time through your "while" loop reduces the chances of seeing a fast-moving object? Your sampling rate is vastly reduced.

yes, it does But how else can i do it?

There are other options but all of them are tricky.

  1. Use an (external) hardware counter
  2. Use assembler. Go for cycle counting and interrupts.
  3. Use the build in hardware counters, loop them back to get one large counter of 32bit and trigger interrupts on overflow,
    also use interrupts to get the current value of the counter
  4. Use the build in hardware counters, but instead of looping them back synchronize two of them to get a 24 bit counter. Limit the third counter to 254 and use the chinese remainder theorem to compute an “almost 32bit” counter.

Depending on your skill set you might choose any of these options. I would go for #2. However it depends on your background. #4 is the most tricky one. Not recommended unless you understand the chinese remainder theorem AND are capable to throw in some nifty assembler code.

Udo

I’ll say my skills are between beginner to intermediate on programming ATMega chips
regarding teh options you provided

  1. the hardware is already built, so can’t add an external counter
  2. I have the feeling that this option doesn’t include arduino ide - so I’ll pass it
    3 & 4. I already use
void InitButons()// setup interrupt-driven keypad arrays
{
  //Setup timer2 -- Prescaler/256
  TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
  TCCR2B &= ~(1<<WGM22);
  TCCR2B = (1<<CS22)|(1<<CS21);
  ASSR |=(0<<AS2);
  //Use normal mode
  TCCR2A =0;
  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= (0<<OCIE2A);
  TCNT2=0x6;//counting starts from 6;
  TIMSK2 = (1<<TOIE2);
  SREG|=1<<SREG_I;
}
/////////////////////////////////////////////////////////
//Description: 
//Parameters: None
//Returns: None
/////////////////////////////////////////////////////////
// Timer2 interrupt routine - 1/(160000000/256/(256-6)) = 4ms interval
ISR(TIMER2_OVF_vect) 
{
  TCNT0= 6;
  GetKey();
}

to read the state of 6 keypad buttons and the other timers - i think , not so sure - will affect the spi and camera control pins

I’m no expert so i might(will) be wrong about what i said above.
PS: I can archive the code and upload it somewhere if anyone has the time and patience to show me how to use the interrupts
Thanks again

Finally Did u get how to measure ns???