Source code for this project XFunction_Control_Points.
Summary: This project extends the Function Control Points Project to include controlling some rail side signals and an LCD display for the status of the points.
Required Hardware: In addition to the hardware required for the Function_Control_Points hardware (Nano Microcontroller with opto coupler interface to DCC tracks and PCA9685 Servo Interface Board) the project will require a 1602 LCD and multiple Green-Red and Green-Amber signals.
Required Software: The project will enhance the client code for the Function_Control_Points that inherits the DCC_Loco_Fn and Servo_for_Points libraries. The LCD code will inherit the LCD_1602 class.
This project enhances the Function_Control_Points project to include an LCD display that will display the status of the points. This part of the enhancement requires the LCD_1602_Class library that was developed earlier. This library includes some enhancements to the commercial 1602 Library.
The project will include code to generate signals that could be part of the layout to show approaching drivers the status of the points. The lights as shown above are use a prototype - it is anticipated that 3D printing will be used to develop the final signals. As some of the signals are on the side of the baseboard dwarf signals will used to reduce the potential of being knocked.
The photo shows a LDR that is used to detect the presence of trains.
For this project the starting points will be the Function_Control_Points code. (One file only). That is:
The additional code for the LCD is shown below.
Basically an object or instance of the LCD_1602 class is created. This new object is initialised and a welcome message displayed.
To update the display after each change in the active loco function the method Dsp_settings( ) will be called.
//Function_Control_Points -extended//Program Function_Control_Points is extended to include a LCD diplay.//Input from DCC_Loco_fn selects Point/Servo to control in Servo_for_Points Class
Initially the routine Dsp_setting( ) was placed near the top of the program loop but after testing it was moved to the end after all the servo action as this seemed more realistic the status being displayed after the point was switched rather than before.
As shown three signals may be located with each set of points.
One solution is to use the servo driver PCA9685 to drive a pair of LEDs. With the PWM timing set to "0" the output will be zero and the green LED activated. With the timing set to maximum (4095) the signal will be high and the red LED will be active.
For each point 3 LED pairs will be required with wirings as shown. When the points are set to straight the two right hand Green LEDs should be active. When the points are set to turn the left Green and the amber should be active.
** Note the centre diagram shows a common current limiting resistor. While in theory this is will work I have found that the GREEN Leds from my batch are much brighter than the RED and AMBER. It is probably better to have a current limiting resistor in all arms as per the right-hand diagram. Starting values might be 2.K for the Green and 1.5K for the Red and amber. In the right-hand diagram in theory one resistor in the signal line could be used. In practice all LEDs will be slightly different and one (LED) will "hog" all the current so induvial current limiting resistors should be included.
For my project I have used 8 servos so there are 8 additional servo outputs available to drive the Signals.
The Wire.h and Adafruit_PWMServoDriver.h will be part of the Servo_for_Points class so there is no need to include these.
If there were no servos the following must be included as part of the set-up method:
In this project each instance of the Servos_For_Points has executed this code so another repeat is not required.
Similar to the Dsp_setting( ) method the results for bit 0 must be used to drive the set of signals at positions 1 and 2.
The project monitored the DCC Function settings and controlled the servos, LCD and the signals.
Initially the servos were moved after the LCD and signals were changed but it was felt that it was more realistic to first move the points then change the LCD and signals.
Since the input signal was only active when the buttons on the controller were changed. The LCD display a "_" until a new reading is received. For the signals these should be initialised to the same as the actual points. (Change the argument in the following code that is included in the set up method.
Dsp_signals(0xff); //Initialize signals
To have the servos, LCD and signals in synchromism the Microcontroller and DCC Controller should be in the be in the same state. This will normally be the case when both are reset at the same time. During the code development phase this may not be the situation. For example if the Function 1 has been set at the loco and new code is then loaded and function 1 button pressed the state will be reset so the servos and signals will not change which will cause some concern. However, following the first request everything will be in synchronized.