High speed photogate readings and hardware interrupts (Help Needed)

After doing a fair bit of research but still not finding anything useful i thought it might be time to ask the forums.
Anyways off to task at hand

I am essentially building a Nerf ammo counter. I have the coding for the clip reading all set up but need help with the dart trigger.
I have a photogate integrated into the barrel that will trigger the system when the beam is broken. Problem is the dart is traveling too fast for my code (that i have written right now) to trigger. I heard that hardware interrupts might be the way to go but i cant find anything that uses thresholds instead of basic state changes. How would one go about getting the code set up in such a way that it can detect changes that quick? Thanks.

For reference, here is a segment of the code…

 else if (digitalRead(clip18) == HIGH)
     {
      AmmoCount = 18;
      digitalWrite(digit0, LOW);
      segmentWrite(AmmoCount%10);
      delay(10);
      digitalWrite(digit0, HIGH);
      digitalWrite(digit1, LOW);
      segmentWrite(AmmoCount/10);
      delayMicroseconds(100);
      digitalWrite(digit1, HIGH);
   
    while(digitalRead(clip18) == HIGH){
      buttonState = analogRead(photosensor) <  700;
    if (buttonState != lastButtonState && buttonState == HIGH && AmmoCount>0) {
        AmmoCount -= 1;
      }
      lastButtonState = buttonState;
      digitalWrite(digit0, LOW);
      segmentWrite(AmmoCount%10);
      delay(10);
      digitalWrite(digit0, HIGH);
      digitalWrite(digit1, LOW);
      segmentWrite(AmmoCount/10);
      delay(10);
      digitalWrite(digit1, HIGH);
    }
    }

Will you please post all of your code so that what you are doing can be seen in context of the data types and sensor reading techniques being used ? Details, a link maybe, of the 'photogate' that you are using and how things are wired would be helpful too.

byte digit0 = 10;
byte digit1 = 11;
int clipdrum = 12;
int clip18 = 13;
int clip12 = A0;
int clip6 = A1;
int firepin = A2;
int photosensor = A4;
int triggersignal = A5;
int resetpin = 9;
int counter = 0;
int AmmoCount = 0;   
int buttonState = 0;         
int lastButtonState = 0;
 
byte sevenSegmentPins[] = {2,3,4,5,6,7,8};
byte sevenSegment[10][7] =
{
  //a b c d e f g
  { 0,0,0,0,0,0,1 },  // = 0
  { 1,0,0,1,1,1,1 },  // = 1
  { 0,0,1,0,0,1,0 },  // = 2
  { 0,0,0,0,1,1,0 },  // = 3
  { 1,0,0,1,1,0,0 },  // = 4
  { 0,1,0,0,1,0,0 },  // = 5
  { 0,1,0,0,0,0,0 },  // = 6
  { 0,0,0,1,1,1,1 },  // = 7
  { 0,0,0,0,0,0,0 },  // = 8
  { 0,0,0,1,1,0,0 }   // = 9
};
 
void setup()
{
   Serial.begin(9600); 
   pinMode(digit0, OUTPUT); //pin 10
   pinMode(digit1, OUTPUT); //pin 11
   pinMode(clipdrum, INPUT); //pin 12
   pinMode(clip18, INPUT); //pin 13
   pinMode(clip12, INPUT); //pin 14
   pinMode(clip6, INPUT); //pin 15
   pinMode(firepin, INPUT); //pin 16
   pinMode(resetpin, INPUT); //pin 9
   pinMode(photosensor, INPUT); // Light Trigger
   pinMode(triggersignal, OUTPUT);// Signal To Light Trigger
    
  for(int i=0; i<7; i++)
  {
    pinMode(sevenSegmentPins[i], OUTPUT);
  }
  digitalWrite(digit0, HIGH);
  digitalWrite(digit1, HIGH);
}
 
void segmentWrite(byte digit)
{
  byte pin = 2;
  for (byte i=0; i<7; ++i)
  {
    digitalWrite(pin, sevenSegment[digit][i]);
      ++pin;
  }
}
 
void loop()
{
    Serial.println(analogRead(photosensor));
    AmmoCount = 0;
    digitalWrite(digit0, HIGH); 
    segmentWrite(0);
   delay(10);
    digitalWrite(digit0, LOW);
    
    digitalWrite(digit1, HIGH);
    segmentWrite(0);
    delay(10);
    digitalWrite(digit1, LOW);

  if (digitalRead(clipdrum) == HIGH)
    {
      AmmoCount = 25;
      digitalWrite(digit0, LOW);
      segmentWrite(AmmoCount%10);
      delay(10);
      digitalWrite(digit0, HIGH);
      digitalWrite(digit1, LOW);
      segmentWrite(AmmoCount/10);
      delay(10);
      digitalWrite(digit1, HIGH);
   
    while(digitalRead(clipdrum) == HIGH){
      buttonState = analogRead(photosensor) <  700;
    if (buttonState != lastButtonState && buttonState == HIGH && AmmoCount>0) {
        AmmoCount -= 1;
      }
      lastButtonState = buttonState;
      digitalWrite(digit0, LOW);
      segmentWrite(AmmoCount%10);
      delay(10);
      digitalWrite(digit0, HIGH);
      digitalWrite(digit1, LOW);
      segmentWrite(AmmoCount/10);
      delay(10);
      digitalWrite(digit1, HIGH);
    }
    }

  else if (digitalRead(clip18) == HIGH)
     {
      AmmoCount = 18;
      digitalWrite(digit0, LOW);
      segmentWrite(AmmoCount%10);
      delay(10);
      digitalWrite(digit0, HIGH);
      digitalWrite(digit1, LOW);
      segmentWrite(AmmoCount/10);
      delayMicroseconds(100);
      digitalWrite(digit1, HIGH);
   
    while(digitalRead(clip18) == HIGH){
      buttonState = analogRead(photosensor) <  700;
    if (buttonState != lastButtonState && buttonState == HIGH && AmmoCount>0) {
        AmmoCount -= 1;
      }
      lastButtonState = buttonState;
      digitalWrite(digit0, LOW);
      segmentWrite(AmmoCount%10);
      delay(10);
      digitalWrite(digit0, HIGH);
      digitalWrite(digit1, LOW);
      segmentWrite(AmmoCount/10);
      delay(10);
      digitalWrite(digit1, HIGH);
    }
    }
  
  else if (digitalRead(clip12) == HIGH)
    {
      AmmoCount = 12;
      digitalWrite(digit0, LOW);
      segmentWrite(AmmoCount%10);
      delay(10);
      digitalWrite(digit0, HIGH);
      digitalWrite(digit1, LOW);
      segmentWrite(AmmoCount/10);
      delay(10);
      digitalWrite(digit1, HIGH);
    
    while(digitalRead(clip12) == HIGH){
      buttonState = analogRead(photosensor) <  700;
    if (buttonState != lastButtonState && buttonState == HIGH && AmmoCount>0) {
        AmmoCount -= 1;
      }
      lastButtonState = buttonState;
      digitalWrite(digit0, LOW);
      segmentWrite(AmmoCount%10);
      delay(10);
      digitalWrite(digit0, HIGH);
      digitalWrite(digit1, LOW);
      segmentWrite(AmmoCount/10);
      delay(10);
      digitalWrite(digit1, HIGH);
    }
    }
  
  else if (digitalRead(clip6)== HIGH)
    {
      AmmoCount = 6;
      digitalWrite(digit0, LOW);
      segmentWrite(AmmoCount%10);
      delay(10);
      digitalWrite(digit0, HIGH);
      digitalWrite(digit1, LOW);
      segmentWrite(AmmoCount/10);
      delay(10);
      digitalWrite(digit1, HIGH);
    
    while(digitalRead(clip6) == HIGH){
      buttonState = analogRead(photosensor) <  700;
    if (buttonState != lastButtonState && buttonState == HIGH && AmmoCount>0) {
        AmmoCount -= 1;
      }
      lastButtonState = buttonState;
      digitalWrite(digit0, LOW);
      segmentWrite(AmmoCount%10);
      delay(10);
      digitalWrite(digit0, HIGH);
      digitalWrite(digit1, LOW);
      segmentWrite(AmmoCount/10);
      delay(10);
      digitalWrite(digit1, HIGH);
    }
    }

  else
  {
    AmmoCount = 0;
  }
}

The “photosensor” is just a photoresistor from Radioshack :slight_smile:

Thanks for the code but I am afraid that I am none the wiser as to what you want to do. From your original description it would seem that you want to know how many shots have been fired or how many shots remain. If that is not it then can you please explain in more detail ?

I freely admit that you could write what I know about Nerf guns in very big letters on the head of a pin, so I apologise for the question if it is not relevant, but could you not just count the number of times the trigger is pressed ?

      buttonState = analogRead(photosensor) <  700;

I find code like this rather annoying. That the sensor reading is less than 700 has nothing to do with the state of a button.

Don’t use names that require thinking.

int buttonState = 0;         
    if (buttonState != lastButtonState && buttonState == HIGH && AmmoCount>0) {

buttonState is NOT a boolean. Don’t use it like one.

First of all, thanks for the replies. What i did is set up the code such that the photosensor acts as a button press (when under a certain level). The code works 100% except when high speed (ie the speed of the dart) is introduced. Basically I need help getting the photosensor to detect the dart when at high speeds.

And yes i could technically just detect whenever the trigger is pressed but that could lead to miscounts (ie if you pull the trigger without a dart loaded etc.) plus it is more of a coding/hardware challenge and those are always fun.

Basically I need help getting the photosensor to detect the dart when at high speeds.

If the hardware isn't working, so amount of coding can make up for that. You clearly need a faster detector.

The thing is I believe the photosensor can detect it, mostly because all it is is a CdS Photoresistor. The arduino is pulling the values at a certain rate and I believe that adjusting the code can make it such that the system can detect objects at that high of speeds.

A suggestion. Write a test program that does nothing else but read the photosensor and light an LED for a short period when it detects a dart passing. Does it work OK ?