This project will use the NCE DCC Twin shown. It has provision for controlling two locos at address 3 and 4. It can also program the functions 1 through 8 for both locos - see buttons. However that is all it can do. It cannot control accessories such as points. This project will capture the function information and in a later project Function_Control_Points this function information will be used to control points.
The information on the tracks is captured via the opto coupler (The white chip) and the Nano interrupt pin (D2). A previous project DCC_Probe has decoded all the DCC messages - this project will extract the Loco Function information. These can be used to control accessories.
The board on the top right is not part of this project but is used in a later project to drive servos that control points.
Source code for program DCC_Loco_Fn_code
This program will use the information captured by the class/library DCC_Probe and pull out the status of the function buttons 1 through 8 for a specified locomotive.
Include file: DCC_Loco_Fn.h
Constructor: DCC_Loco_Fn no parameters
Public Methods: void Begin( )
int Fn_State(LOCO); where Loco is loc of interest
Method returns state 8..1 of functions
Possible Audience: Model train enthusiasts who have a knowledge of C++
Required Hardware: Project will use the PCB required for the DCC_Probe project. Basically, an opto coupler wired between the railway tracks an Arduino microprocessor.
Keywords, The project does not demonstrate new key words but is an example of Inheritance in C++.
Required Software: As illustrated the project builds on the DCC_Probe so will require the DCC_Probe library. The project will be developed using the Arduino IDE.
The project will require 3 files all with the same name but different extensions. In the Arduino IDE create a new file DCC_Loco_Fn. The editor will give it the extension *.ino. Also in the same window use the upside down triangle near the top right to create two new files DCC_Loco.cpp the implementation file and DCC_Loco_Fn.h the header file.
For starters populate the client or application file as shown. The client code almost gives the starting point in the propject design. That is a function/method of the form Fn_State(LOCO) that returns the current state of the function settings for a specified LOCO is required. Fn_State(LOCO) will be a member of a new class DCC_Loco_Fn to be developed. Possible starting code will be:
The end application has defined a starting point for what is required of class DCC_Loco_Fn. The class will read the results found in the class DCC_Probe and provide a public method Fn_State that returns selected results to the application or client code. In C++ terms the new class will inherit the DCC_Probe class. This will be part of the definition:
//DCC_Loco_fn.h Header File#ifndef DCC_LOCO_FN_H#define DCC_LOCO_FN_HAt this point the implementation file will be a shell of the final code consisting of the class constructor and the begin() and FnState() methods. That is
Since the new class uses the DCC_Probe class any initialisation requires the DCC_Probe::begin( ) method.
At this point the new class does not require any additional initializations.
The next stage requires some knowledge of the methods available with the DCC_Probe class (begin() has already been used).
The DCC_Probe class has collected the data from the railway tracks and placed these in an array. To read the array the method get_result( ) is provided. When there is new data this will be flagged with the method message_done( ) and when this data has been used the status may be cleared with the method clear_message_done( );
The code becomes:
At this point if there was no new data it was decided to return an invalid number. ie 256. This will require up grading the application code. that is
The implementation code then makes copies of the two elements of the data array and clears the message done.
The next part of the code requires combining the two bytes to obain the loco functions
-------------------------------------------------------------------------------------With command2 the results must be in the range 128 (0x7F) through 191 (0xBF). That is
The final step is to manipulate command2 to generate the required patterns. That is Function 1 in bit 0 of the result, Function 2 in bit 1 through Function 8 in bit 7. Note the status of the light function is not returned.
At this point the code will continuously output the status even if it does not change. To only output new data the application or client cod may be modified by including a variable saveActive which contains the last reading.