Detecting a small and fast object

dlloyd:
When you're ready to test using interrupts, its best to keep an interrupt function as simple as possible and do everything else in the main loop. Here's some pseudo code to get started:

const interruptPin = 2;

volatile bool bbCountFlag = false;
volatile unsigned int bbCount = 0;
unsigned int bbCountCopy = 0;

void setup() {
 pinMode(interruptPin, INPUT);
 attachInterrupt(digitalPinToInterrupt(interruptPin), nextBB, FALLING); // or RISING
}

void loop() {

if (bbCountFlag) {
   noInterrupts();
   bbCountCopy = bbCount;
   interrupts();
   bbCountFlag = false;
 }
 // do something using bbCountCopy
}

void nextBB() {
 bbCount++;
 bbCountFlag = true;
 EIFR = bit (INTF0);  // clears flag for interrupt 0 (adds minimal debounce)
}

I tried this code but the arduino would get stuck blocking all serial communications. I have to print the result on a screen so it's not viable. I played with the code a bit getting this:

//Pin Names
const int photoGatePin = 2; //the IR Led and IR phototransistor are connected here

//Variables
int totalShotsFired = 0;
volatile bool photoGateFlag = 0;

void setup() {
  Serial.begin(9600);
  attachInterrupt(0, photoGateTrigger, RISING );
}

void photoGateTrigger() {
  photoGateFlag = 1;
}

void loop() {
  if (photoGateFlag) {
        photoGateFlag = 0;
        totalShotsFired++;
        refreshScreenBBShots(); //another function not present here, it refreshes the screen printing the result
      }
  }

First try, it was working properly, detecting every shot. Swapped battery and wasn't detecting a single shot. Reset the arduino and retry, some shots were detected, other weren't . In other words it isn't reliable or consistent, can it be it should have to be faster?

But ... are you using a single sensor ? ... do you want only to see if they pass, or also measure their speed ? ...

Etemenanki:
But ... are you using a single sensor ? ... do you want only to see if they pass, or also measure their speed ? ...

I only need to see if the BBs passed to count them, I don't need their speed

Hello,
Glad you took the interrupt path, for this kind of challenge it is the right way.
You might consider increasing your counter in the interrupt handler and do the rest outside:
If your main routine takes too long, you will miss counts (I do not know how fast your bullets come).
But if you increase the counter over there, you will count in the correct way.
You also might want to comment out your display routine: if it is blocking interrupts, then you might also loose some interrupts but that depends on the way it is built.
Maybe you need to focus on the the hardware part, are you able to visualize the signal using an oscilloscope? That way you immediately know if your signal is long and stable enough to trigger an interrupt. (and I repeat myself: to catch an interrupt properly you need a signal that is longer than 1 clock cycle of your CPU according to the datasheet.)

Best regards,
Johi.

JOHI:
You might consider increasing your counter in the interrupt handler and do the rest outside:
If your main routine takes too long, you will miss counts (I do not know how fast your bullets come).
But if you increase the counter over there, you will count in the correct way.

The BBs move at about 100m/s but the rate of fire is between 15 and 30, so in 33 ms I have plenty of time to increase the counter and send the information to the screen, what I don't want is the interrupt to block other signals that may occur during the routine so I thought this was the best way to code it.

JOHI:
Maybe you need to focus on the the hardware part, are you able to visualize the signal using an oscilloscope? That way you immediately know if your signal is long and stable enough to trigger an interrupt.

Sorry but I don't have an oscilloscope

ariesbreath:
The BBs move at about 100m/s ...

100m/S is 1 mm each 10uS, the BB are large 6mm, let's say 4mm for consider some tolerances and not perfect block of the light in the first and last part of the sphere, still the light must become interrupted for around 40uS ... i think are more than enough, at least if the phototransistor is not a slow type ... cheap ones can easily have a response time around 20 or 30 uS, opposite to photodiodes ...

Hi,
Have you googled

arduino chronograph

and looked at how they detect the object passing a sensor?

Tom... :slight_smile:

In mine, photodiodes :wink: ... SFH229FA, 3mm photodiodes, with an op-amp as input stage ... they have 10nS rise and fall time, and are more than enough for detect 4.5mm 0.62 grams lead pellets shooted from a Gamo air rifle at around 140m/S :wink:

But some commercial unit uses image sensors, still made from photodiode arrays, and a reflective strip ... the sensor detect the shadow of the object from the reflective strip, illuminated with IR from near the sensor itself ... can be used for made ballistic chronographs also for arrows and big bullets (like these ones as example), but have way too much cost for an hobbystic realization, imho ...

TomGeorge:
Hi,
Have you googled
arduino chronograph
and looked at how they detect the object passing a sensor?

Yes, there are many systems depending on some factors (material and dimensions of the bullet) but the logic is the same: obscuring a signal of some kind, like I'm trying to do.

Etemenanki:
In mine, photodiodes :wink: ... SFH229FA, 3mm photodiodes, with an op-amp as input stage ... they have 10nS rise and fall time, and are more than enough for detect 4.5mm 0.62 grams lead pellets shooted from a Gamo air rifle at around 140m/S :wink:

The phototransistor I'm using is a TEFT4300, this one https://www.vishay.com/docs/81549/teft4300.pdf
From the sheet I read this:

Turn-on time     VS = 5 V, IC = 5 mA, RL = 100 Ω ton      2 μs
Turn-off time    VS = 5 V, IC = 5 mA, RL = 100 Ω toff     2.3 μs

So, while the light is obscured for about 0.04 ms = 40 μs, I need only 2 of them to turn on the phototransistor, am I right?
If I'm right, the phototransistor is fine, but I need the arduino to pick up this signal

Well, is at this point where usually come helpful an oscilloscope ...

I mean, is difficult to say if the signal is good or bad, if you cannot see it ... do you know anyone in your zone that can help you with an oscilloscope, if you don't have one ?

Etemenanki:
Well, is at this point where usually come helpful an oscilloscope ...

I mean, is difficult to say if the signal is good or bad, if you cannot see it ... do you know anyone in your zone that can help you with an oscilloscope, if you don't have one ?

Unfortunately not.

I was thinking about other workarounds, like placing two mirrors that the light can bounce upon to trip the photogate multiple times, or placing the led and phototransistors' slots diagonally to increase the time the signal is blocked

The phototransistor I'm using is a TEFT4300

Yes that should work for the bb's if the speed of the bb's is as proposed earlier in this thread. Have you tested your transmitter/receiver pair with a card or something which blocks the ir to confirm that you are wired properly and have the right transition identified for blocked.

This post (in "Sensors") describes a similar (school?) project...

The photoelectric sensor should detect a 6mm diameter sphere moving about 90m/s...

...and got a TRCT5000 to work.

https://forum.arduino.cc/index.php?topic=734887.0

Solved it already thanks to the German Community on this board. I got it to work with an interrupt reading the digital output of the IR-module.

https://www.youtube.com/watch?v=MCTqC2-AN7o
Read again post #33 and convert microseconds to milliseconds. Fiber Optic LED can handle hundreds of MHz, use photo diode.

google - laser video sender

Update on the project.
I still don't know how to read the sensor faster in a reliable and easy way, so I thought that the best way to proceed was to increase the time in which the sensor is triggered. I printed a slanted slot instead of a vertical one at about 55° so that the time needed for the BB to pass the light was like doubled. I tried a hundred shots and it works perfectly.

MarkT:
Phototransistors may be too slow, but will be fastest with a low value load resistance on the
collector, such as 1k or less, which means fairly intense illumination.

A note: I tried to use a 6.8 kohm resistor instead of a 10k one because MarkT said that a lesser resistor gives a faster response, so I'm not sure which thing is the one that let the sensor to be read correctly, maybe both.

A special thanks to dlloyd that kept up with my posts and pointed me to the right route