jenswilly.dk
Airsoft Rounds Counter follow-up
1. November 2011 22:07 by Jens Willy JohannsenOk. Everything has been assembled and mounted. I mounted the control unit inside the stock. Power comes from the gun battery. The wires from the photo interrupter to the control unit run inside the handguard and upper receiver. I have drilled through the aluminium endblocks of the handguard and I'm using three pin RC servo type plugs and sockets to connect the silencer with the IR photo transistor and LED to the gun (I need to unplug it in order to unscrew it) and also inside the stock so I can easily disassemble the gun.
And it works pretty darn good, actually!

Board assembled
6. January 2011 13:38 by Jens Willy JohannsenI have received the PCBs and assembled the first one. Everything expect connections to external components, that is.
Here's a picture. It looks pretty much like the rendering. And I know: some of the soldering joints could have been made better but bear in mind that this is my first SMD soldering attempt so I'm actually pretty pleased with the result.

Eagle3D layout
20. December 2010 21:38 by Jens Willy JohannsenHere is a 3D model of the board created with Eagle3D (here's a tutorial and here's some information about getting it to work on Mac OS X):

Firmware complete
16. December 2010 12:47 by Jens Willy JohannsenPretty much at least. I've added an EEPROM variable for storing the magazine capacity and I've added support for programming the magazine capacity and for resetting the rounds remaining counter without turning off the entire device.
The device can be in two modes: run and program. Run is the normal count-down-when-a-round-is-fired mode and the PRG/RST button will reset the counter (for when you put in a fresh magazine).
To enter program mode, press the PRG/RST switch while powering on the device. The display will show "PrG" for a second (as opposed to "run" in run mode) and then the counter will show 000. Pressing the PRG/RST button will increase the counter which is the magazine capacity. If the PRG/RST button is held down for a second, the counter will increase quickly. The capacity is saved in EEPROM every time is it increased so simply turn off and back on the device to use the new capacity.
Download the code here: firmware_rev2
Points of note:
- Both the photo interrupter and the reset switch work as external interrupts (INT0 and INT1, respectively)
- In programming mode, a 1 second timer compare match interrupt is used for hold-to-count-quickly
- Magazine capacity is stored in EEPROM

Final layout?
12. December 2010 12:19 by Jens Willy JohannsenSchematic and layout revised again. I added the 10 µF capacitor (that really ought to be there) and a LED with accompanying resistor for debugging purposes.
Here a print-it-out-and-see-if-it-fits test. And it does fit. Note that all the resistors are the same value (doesn't matter of course since all 1206 resistors are the same size) and that I've placed a 1206 resistor instead of a 1206 LED. Also, the B sized 10 µF capacitor isn't placed on the printout but it shouldn't pose any problems.

Schematic Revised
10. December 2010 22:05 by Jens Willy JohannsenAnd a new and revised schematic for the Airsoft Rounds Counter. The only difference is that the photo interrupter's collector wire is now connected to the ATTiny's INT0 pin.
Update: obviously I mean "to digit cathodes" and "to segment anodes" – not the other way around.

PCB Layout Revised
10. December 2010 21:55 by Jens Willy JohannsenOops. The photo interrupter's collector wire wasn't connected to INT0 on the ATTiny. That has been fixed now. Also, the board is now 1 mm wider so there is enough clearing from the segment A wirepad to the edge of the board.
And the VCC and GND traces are now 24 mils and all other traces are 12 mils wide. Refer to the first layout if you can't remember what the various wirepads are for.

Breadboard Prototype
10. December 2010 21:46 by Jens Willy JohannsenHere are a couple of pictures of the breadboard setup:
The first picture is of the breadboard itself. The components are marked and the green dots are where the wires from the photointerrupter go.
The second pictures includes the photo interrupter mounted to the barrel using duct tape (what else). Note that this was before the Max7219 and display was mounted:

SMD layout
10. December 2010 0:53 by Jens Willy JohannsenHere's the first draft of an SMD PCB layout for the Airsoft Rounds Counter project.
The wire pads are as follows:
- VIN
9V power input - AN, CA
Anode and cathode the the photo interrupter LED - CO, EM
Collector and emitter for the photo interrupter transistor - A-G, DP
Segment anodes on the display - 1-4
Digit cathodes on the display - RST/PRG SW
Reset and program tact switch
Actual size is 49.53 x 20.32 mm.

Firmware for Airsoft Rounds Counter
9. December 2010 12:05 by Jens Willy JohannsenHere is version 1 of the firmware for the Airsoft Rounds Counter project. The code uses avr-gcc but should be real easy to port if you use a different compiler.
This firmware is by no means the final version. Rather, it is the firmware for the breadboarded version (the schematic is here).
The ZIP archive contains three files:
- main.c
Contains all the code. Please note that the code assumes that a LED is connected to PB4 which is not shown in the schematic. This is purely for debugging purposes and you can add the LED or not. The code doesn't care. And neither do I. - Max7219.h
Header file with definitions for the various Max7219 commands. - Makefile
Standard makefile. Do a 'make all' to compile, 'make fuse' to set fuses (they are set to internal 8 MHz RC oscillator with slow startup, no BOD, SPI enabled), 'make flash' to flash the ATTiny and 'make clean' to delete the binaries.
Download the ZIP archive here: firmware.zip

Prototype
8. December 2010 11:06 by Jens Willy JohannsenI have the first prototype of the Airsoft Rounds Counter up and running on a breadboard.
Here is the schematic. A couple of notes:
- A 10 µF capacitor or so near the power supply certainly wouldn't hurt. Neither would a diode on the input side of the 7805.
- The "program and reset" switch doesn't do anything with the current code and can be omitted for this version.
- On the breadboard I also have a tact switch from the ATTiny's RESET pin to ground so I can reset the counter.
- I'm using a 9V battery for power.

SPI and USI
6. December 2010 13:36 by Jens Willy JohannsenThe Max 7219 chip communicates using SPI. The last time I used it I had apparently written my own bit-banging SPI routine in the Arduino IDE. Which works just fine (the SPI protocol being so simple) but it seems clunky. And since the ATTiny2313 has a built-in USI (Universal Serial Interface) capable of doing SPI I thought I might as well use that.
After a bit of Googling and reading of the datasheet, this is what I came up with:
#define PIN_LOAD PB4 // Utitlity method to shift 8 bits out the USI pins in 3-wire (SPI) mode static unsigned char spiByte(unsigned char val) { USIDR = val; // Set data byte to send USISR = (1<<USIOIF); // Clear Counter Overflow Interrupt Flag (by writing 1 to the USIOIF bit) do { USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC); // 3-wire mode; shift reg. clock source: external pos. edge; 4-bit counter source: USITC; Toggle USICK pin 0->1 or 1->0 (shift register will only clock on 0->1) } while ((USISR & (1<<USIOIF)) == 0); // Check for OIF set which will happen after 16 cycles. I.e. 8 bits since the clock toggles twice per bit. return USIDR; // Return data value } void sendToMax7219( char reg, char value ) { PORTB &= ~(1 << PIN_LOAD); // LOAD low spiByte( reg ); spiByte( value ); PORTB |= (1<<PIN_LOAD; // LOAD high }

Photo interrupters
6. December 2010 11:22 by Jens Willy JohannsenFor the Airsoft Round Counter project I need some way of detecting when a round is fired and (like most people suggest) a photo interrupter is ideal for the purpose.
Basically, a photo interrupter consists of an LED (usually an IR one) and a photo transistor. A photo transistor works like a normal transistor but instead of a current at the base, it requires light to turn on the collector-emitter current flow. In a photo interrupter the LED and the photo transistor are mounted in small posts and when nothing interrupts the beam, the transistor turns on, passing a current from the collector to the emitter. When the beam is interrupted, the transistor turns off.
A photo interrupter has four leads or connectors: anode and cathode for the LED and collector and emitter for the photo transistor.
To use it in as a digital input for a microprocessor, you can create a circuit like this:
R1 limits the current to the LED (using the normal LED calculations having obtained information about the LED forward voltage and desired current from the datasheet – typically something like 2.0V and 20 mA).
When the photo interrupter is not interrupted, the input reads 0. When an object blocks the beam, the input reads 1.
This document from Lite-On has some excellent information about using photo interrupters (after the list of their products).

Airsoft Rounds Counter
6. December 2010 10:02 by Jens Willy JohannsenBrowsing around on AVRfreaks' forum I stumbled upon this thread about an Airsoft Round Counter to display the number of remaining rounds when playing airsoft. This is obviously a cool idea and the thread inspired me to want to make one myself.
In the thread people discussed using everything from a simple switch attached to the trigger over photo interrupters and photo reflectors to accelerometers for detecting when a shot is fired. There is also a link to this page on the Design Decisions Wiki where a group of people have designed an "Airsoft Electronic Ammunition Counter" with detailed design considerations and requirement analysis and timing calculations. Excellent work and a great source of inspiration for this project.
My initial considerations for this project:
- Enclosure. Everything should be mounted to an airsoft gun and it shouldn't be the size of a shoe box!
- Component count. This ties in with number 1 above: if this is to be small enough to be portable I have to keep the component count down so the final PCB is of a manageable size. (Yeah yeah, "use SMD". Right, I'll do that in phase 2.)
- Ease of use. This will be used in skirmishes so it should be real easy to use. No re-flashing of the processor to change settings, easy to attach to a weapon and so on.
So far I have settled on the following basic components:
- ATTiny 2313-20PU. I could probably get by with a smaller ATTiny but since I already had a couple of these lying around I went with those...
- Four-digit 7 segment display
- Maxim 7219 CNG display driver.
- Wide-gap photo interrupter
Off to EAGLE for some schematic drawing and thence to the breadboard for figuring out how photo interrupters actually work...
