The following project presents an alternative DCC library for use with the turntable project, and can also be used for other DCC related Arduino projects.
Notes on Importing into the Arduino IDE:
You can check the current version numbers of the libraries using the menu option:
Sketch->Include Libraries->Manage Libraries…
After you download the library ZIP files using the links above, you can import them into the Arduino IDE using the menu item:
Sketch->Include Libraries->Add .ZIP Library…
It seemed to me that the code concerned with getting the data from the DCC system was unnecessarily complex so, just out of curiosity, I started to investigate it further. I downloaded the DCC library from mynabay.com and was disappointed to discover absolutely no documentation apart from a couple of example sketches – one of which has been incorporated in the turntable code. (If there is documentation elsewhere that I have overlooked, I apologize). And I didn’t make much headway trying to understand the internals of the library.
I then found another DCC decoder library at http://mrrwa.org which was easier to figure out, although also written in a style that may appeal to dyed-in-the-wool C++ programmers. And, like the mynabay code it included some additional functionality that, for me, obscured the basics.
I have now written a program that uses the pulse detection technique from the mrrwa library in a way that, I hope, will be easy to follow for anyone that is interested in what goes on inside the “black box”. The code simply shows the DCC packets in the Arduino Serial Monitor and it should be a simple matter to extend the code to make use of the packets to control stuff.
I have divided the code between 3 files as I think it makes it easier to follow. All the files should go in a directory called ReadDccPackets which is the name of the main Arduino sketch. The Arduino system loads the othe .ino files in alphabetical order which is why the file names begin with A_ and B_.
The overall approach of the program is as follows …
The file B_dccDetectBits.ino has the code that detects the data in the DCC signal and it stores the bits into a circular buffer that can be accessed by the code in A_dccGetPacket.ino.
The system for detecting bits is actually very simple. In the DCC system a 1 is represented by pulse with a half-wavelength of about 58 microseconds and a 0 is represented by a pulse with a half-wavelength of about 100 microseconds. The code uses an interrupt to identify the rising part of a pulse and then takes a reading of the pin 80 microseconds later. If the pin is still high the pulse is a 0, otherwise it was a 1.
The code in A_dccGetPacket.ino draws from the buffer containing the bits and examines them. Each packet comprises a preamble followed by an address byte, some data bytes and a checksum byte. The code saves the packet where it can be accessed by the code in ReadDccPackets.ino.
I have actually described the process backwards. In reality the process begins with by the code in ReadDccPackets.ino which starts the continuous process of detecting DCC bits (beginBitDetection()) and calls for a packet to be extracted (getPacket()) and then displayed (showValidPacket()) in the Serial Monitor. If you want to see all of the packets the function getPacket() must be called sufficiently often – but I haven’t tested how often. If data is not drawn from the buffer containing the bits it will fill up and then additional bits will be lost until the getPacket() function draws more bits from the buffer to make space for new ones.
The physical mechanism (using an opto-isolater) for connecting the DCC signals that is described in the Turntable should be used to connect to Arduino Uno pin 2. I didn’t have an opto-isolator so I made a crude breadboard circuit using a diode and a voltage divider. However I didn’t have any train motors running which might cause higher voltage pulses and the opto-isolator would be a safer way to isolate the DCC from the Arduino.