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.
Now that this is cleared up ... here it is ... an Arduino and ATtiny DCC Function Decoder.
Of course DCC 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 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 for 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 (14-19 are A0-A5) are available. On the Attiny P0, P1, P3, P4, P5.
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
i want to test your Code on a ATTINY44 because i have a lot of them here. I think it would be no problem to do so, but can you tell me which clock i have to use when i write it to the attiny? I can use the 1 Mhz or the 8 Mhz internal clock, or do i need an 20 Mhz external clock ?
New versions of Arduino and ATtiny DCC Function- and Accessory Decoders are available.
DCC Function Decoder The possibility to use the direction of travel of the loc (forward or reverse) to switch two outputs has been added. This makes it possible to switch head and tail lights depending on the loc direction.
DCC Accessory Decoder A mode has been added to switch signals using two pins. The outputs are inverse, if pin1 is HIGH, then pin 2 is LOW. Four modes are available now: mode 1 - Continuous output. OFF at startup. mode 2 - One shot, with a timer. OFF at startup. mode 3 - Flasher with 2 outputs that alternate based on 2 timers in ms. OFF at startup. mode 4 - Signal with 2 outputs that always are inverse. At startup one of them is ON.
IMPORTANT: The output logic has been changed with respect to the previous versions. In the new versions, the Arduino output pin is LOW when accessory or function is switched ON, and vice versa.
If you need it the other way around ... it is not difficult to edit this in the code, just do a 'find' for all 'digitalWrite' instructions and change them.
The download link is available on the software page. The download contains all Aduino decoder versions, S88-, sniffer- and sound software.