In previous posts on Arduino DCC Decoders I have been using the word ‘function decoder’, which has led to some confusion. The decoders posted so far are what I should have called accessory decoders.
An accessory decoder listens to a DCC accessory address and switches an output or drives a servo. One Arduino DCC decoder can be configured to listen to max 16 DCC addresses.
A function decoder listens to one address (a locomotive address, or as the NMRA calls it, a multi-function decoder) and it switches locomotive functions, which are called F1, F2, etc. on command stations or handheld controllers.
And, if you like it small enough to fit into an HO wagon (with a diode bridge and a capacitor as a power supply), this code also works with the 22×18 mm ATtiny boards that can be had for under $1,50 here. For more info on how to get the ATtiny working, see this post.
Of course the DCC signal needs to be fed to the Arduino / ATtiny. For that I use this opto coupler circuit. If it needs to be smaller to fit inside a wagon, it is also possible to use a Zener diode and a resistor.
Configuration is really easy. In line 1 of the code state the loc address you want it to listen to. In the lines that follow, define the output pins of F0, F1, F2 … up to F12. Not all need to be defined, just the ones that you want to use.
On the Arduino outputs 3-19 are available (14-19 = A0-A5). On the Attiny P0, P1, P3, P4, P5.
On both the Arduino and the ATtiny, Pin 2 is the DCC input.
int decoderAddress = 1830; // This is the decoder address, change into the number you want.
#define F0_pin 13 // Define the output pin for every Function number in use. 13 has on board LED.
#define F1_pin 0 // Available pin numbers: 3-19
#define F2_pin 0
#define F12_pin 0
Unzip and place the content of the libraries folder in your Arduino Sketches/libraries folder. Place the other folders in your Arduino Sketches folder. The zip includes the function decoder as well as the accessory decoder software. Alternatively you can download the overall zip file from the Software page, it includes all software published so far.