Nano second?

Hello,
I’m trying to make a function that will return a time in nano seconds. I need this in order to calculate the speed of a fast moving object.
Here is what i got so far

//see http://www.arduino.cc/playground/Main/TimerPWMCheatsheet
void NanoTimer(byte OnOff)
{
  if(OnOff)
    TCCR0B = TCCR0B & 0b11111000 | 0x02;//delay(8000) or 8000 millis() ~ 1 second
  else
    TCCR0B = TCCR0B & 0b11111000 | 0x03;
}
extern volatile unsigned long T0COF;//timer 0 counter overflow
unsigned long GetNano()
{
  return (((T0COF << 8) + TCNT0)*500);
}

and where i use the fast timer ....

 lcd.drawText(1, 123, "Stop ", 0, RED, txtBGColor);
  unsigned int g = 9800;//[mm/s2]
  unsigned int d = 508;//[mm*10-1]
  unsigned long Vout, ST, EIT;//Speed calculated; Start time; End/Impact time
  NanoTimer(1);
  if (digitalRead(1) == LOW)  // If sensor1 detects projectile
  {
    ST = GetNano();
    EIT = ST;
    while(digitalRead(2) != LOW)  // Look for sensor2 to detect projectile
    {
      EIT = GetNano();
      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
        NanoTimer(0);//Stop fast timer
        lcd.drawText(8, 80, "Detection failed.", 0, RED, txtBGColor);
        delay(5000);
        lcd.drawText(8, 80, "Detection failed.", 0, txtBGColor, txtBGColor);
        EIT = 0;
        break;
      }
    }
    if (EIT)
    {
      ST = EIT - ST;
      //Let's get the impact time
      if(ROM_VAL[ROM_Gravity].Value == 0)//Constant speed
      {
       // Vout = (d / ST) *
        EIT = ROM_VAL[ROM_Target_Dist].Value/Vout;
      }
      else//Free falling
      {
        
      }
}
NanoTimer(0);//Stop fast timer

Do you think that these functions will do the job? any advice is wellcomed

//According to wikipedia a hyper velocity bullet can reach 450[m/s] //This means 0.45[mm/us] The distance between the two sensors is 50.8[mm], so a projectile like this one will travel that distance in 113[us] A distance of 0.5[m] 50[cm] 500[mm] is traveled in 1111[us]

in Earth's atmosphere all results below will be quite inaccurate after only 5 seconds of fall (at which time an object's velocity will be a little less than the vacuum value of 49 m/s (9.8 m/s² × 5 s), due to air resistance). For a body encountering a thick atmosphere like the Earth's near sea level, terminal velocity is reached exponentially between 8 and 15 seconds, after which a steady velocity of very approximately 100 m/sec is maintained for compact objects with densities between those of water and common metals. The distance between the two sensors is 50.8[mm], so a free falling object that moves with 100[m/s] will travel that distance in 508[us] A distance of 0.5[m] 50[cm] 500[mm] is traveled in 5000[us]

How long does a digitalRead take to execute?

A digital read takes 1 instruction cycle. I think the ATMEGA chip has a 1 to 1 mapping from clock speed to instruction cycle

Yes, an instruction takes 62.5ns to execute (mostly). But digitalRead isn't one instruction.

i was wrong http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208715493/11

That link is to a topic on analogRead.

 if (digitalRead(1) == LOW)  // If sensor1 detects projectile

Assuming you can read your sensor faster, what if it doesn't detect the projectile? Should this be "while (projectile_NOT_detected)" ?

What kind of fast travelling object do you want to measure and what kind of sensors do you have? If you are going to the nano second range rise times of the sensors will become a significant issue. For example standard opto couplers will always have latencies >1µs. And OCs are designed for signal transmission. So the first question would be what kind of sensors do you have. Second question: why not go for a simple frequency counter?

Udo

1ms: 1.0/1e-3 = 1e3 Hz = 1 kHz
1us: 1.0/1e-6 = 1e6 Hz = 1 MHz
1ns: 1.0/1e-9 = 1e9 Hz = 1 GHz

Arduino clock is 16 MHz. 1.0/16e6 = 6.25e-8 = 62.5 ns.

I'm using Honeywell SD5600-001 and I'm trying to detect an air gun projectile but also falling water drops from Vanyamba's post i now understand that nano sec is not possible so i will just eneble the faster timer, maybe 0x01

void NanoTimer(byte OnOff) { if(OnOff) TCCR0B = TCCR0B & 0b11111000 | 0x02;//delay(8000) or 8000 millis() ~ 1 second else TCCR0B = TCCR0B & 0b11111000 | 0x03; }

Now the problem i didn't realize till now is digital read. Is there a faster alternative to digital read? How can i make frequency counter?

gvi70000: Is there a faster alternative to digital read?

No. If you need 1 ns digitalRead, you need 1 GHz MCU.

gvi70000: How can i make frequency counter?

External timers are rising/falling clock pulses aligned.

I have no clue how fast an air gun projectile might be. But most probably <500m/s. Hence it travels less than 0.5µm per nano second. Or less than 0.5mm per micro second. Waterdrops are much slower. I doubt that you need nano second resolution at all.

Also most electronic flashes have a latency in the microsecond range. Sometimes >>10µs. And while we are at it: most flashes will emit light for >20µs. Everything below 20µs gets tricky and/or expensive.

Udo

The speed of light = 299792458 m/s. During 1 ns the light goes through 30 cm.

So he better also accounts for the length of his wires if he wants to measure in that range ;)

Please, don't make fun of me I will use micros and digitalread without changing the timer0 settings

Thanks all for the help

The speed of light = 299792458 m/s

Well, that's about as OT as it is possible to get.

An air pellet is around 120 - 150m/s, but is only about 4mm long. Let's go for the faster end:

150 000 mm /4.5 mm = 30us. So, your slug goes past a single point in 30 microseconds. Enough time to execute up to 480 AVR instructions. In answer to your question, yes there is an alternative to digitalRead - look up direct port manipulation.

But if he wants to get a picture of it he will most probably have a image resolution of <1mm. Thus he would need a flash of <3µs.

This all reminds me of Glacial Wanderer and The Camera Axe.
http://www.glacialwanderer.com/hobbyrobotics/?p=490.

Udo

Tell us more about your sensors! I am just guessing photo gates. I don't think they're fast enough for your bullets but water drops have terminal velocity in the air, feel the rain drops, not very fast. Maybe in the range of 10m/s. Photo gates can do that.

The discussions you had reminded me of BNC cables and nano second delays they have, written on them, proportional to their length.

The sensor is identical with the one from camera axe

I don't want the picture of the bullet, i need the picture of the object that is hit by the bullet I know the distance from the sensor to the target so i need to calculate the speed of the bullet and the time to impact.

the sensor is as follow

Gun----------------Photogate1---------Photogate2-------------------------Target get the speed of the bullet Get the impact time