Hi I am in the process of starting my annual winter Arduino project, something for the coming dark nights.
I have designed and 3d printed the hardware mk1, which is basically a 15cm tube with a 15mm opening at one end to slot the rifle barrel in, with O rings for a snug fit, further down the tube there are four housings to hold two sets of IR emitters and detectors.
My plan is a sketch in which the timer starts when the first beam is broken, and stops when the second beam is broken.
The maximum resolution at this time is around 250mps, I have googled Arduino Balistic, there are a few around, what I am looking to do is start with a basic stopwatch and improve from there.
The problem I am encountering, is all the stopwatch sketches use the same button for Stop/Start and I can not get my head round how to modify them to use two.
Can anyone suggest a basic two button stopwatch sketch I can use as my foundation?
If anyone would like the stl file of the hardware to make one themselves, please ask and I will upload it.
"resolution of 250 m/s" means you can measure 0, 250, 500, 750, etc. Not too bad a resolution if you're nearing light speed, pretty poor for measuring the speed of a bullet.
I guess the best approach to do this measurement is through interrupts (scanning ports is far too slow, you may even miss events that way). Two pins, two separate interrupts. Timer set to no prescaler (i.e. counting clock ticks). When the first is triggered, set the timer to zero. When the second is triggered, read the current timer value. This way you get a resolution of 62.5 ns on a 16 MHz Arduino, and it's a matter of simple maths to get to the speed.
Of course it takes time for the interrupt to actually kick in, a few clock cycles, but the timer starts late by as many cycles as it stops late (+/- 1-2 cycles) so no problem there.
Again I apologise, I should have said around the 250 mark
So far I have come up with the following, which will also report back muzzle energy, based on a 8 grain pellet, this at the moment is hard coded but as I progress I hope to be able to select weight once a lcd is added.
I decided to go down the line of one button start/stop detectors in series, to keep tasks down to a minimum.
/*
// INCLUDE CHRONO LIBRARY : http://github.com/thomasfredericks/Chrono
#include <Chrono.h>
#define BUTTON_PIN 3
Chrono myChrono;
int previousButtonState;
void setup() {
Serial.begin(57600);
pinMode(BUTTON_PIN, INPUT);
previousButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
int newButtonState = digitalRead(BUTTON_PIN);
if ( previousButtonState != newButtonState ) {
previousButtonState = newButtonState;
if ( newButtonState == LOW && myChrono.hasPassed(1) ) {
unsigned long FPS = myChrono.elapsed() * 13.33333 / 3.37;
unsigned long FtLbs = FPS * FPS * 8 / 450240; //450240 magic number ?????
Serial.print("ms ");
Serial.print( myChrono.elapsed() );
Serial.println();
Serial.print("MPS ");
Serial.print( myChrono.elapsed() * 13.33333 );// multiply time same ratio as length travels to == 1m
Serial.print("FPS ");
Serial.print( myChrono.elapsed() * 13.33333 / 3.37 );// convert mps to fps
Serial.print("Muzzle Energy Based on 8 Grain " (FtLbs ());
}
myChrono.restart();
}
}
JohnLincoln:
Have you considered that with 'O' rings and a snug fit on the barrel, that your 3d printed tube will be subjected to the barrel pressure?
The O rings simply stop it falling off The barrel is 16mm in diameter and fires 5.5mm (.22) pellets the housing is 16mm id down its length, leaving 5.25mm gap all round the projectile, thankfully no back pressure of the escaping C02.
I haven't referred to any libraries.
Timer 2 is a hardware resource, documented in the processor datasheet.
Have a look at Nick Gammon's tutorial on using timers.