Include File <Coal_Basin_LED2.h>
Constructor: Coal_Basin_LED2 route_dsp(int clk, int out);
Public Methods: void begin( );
void control_signals(unsigned int leds); //leds route wish to display
This project drives the signals (LEDs) in the model train layout shown below. There are LEDs at the end of each track/route that will indicate if that route has been selected.
This project is part of a larger project illustrated below.
In the overall project buttons are used to select the desired route and this project will display the chosen route on a control panel. The active LEDs are not 1:1 with the pressed buttons so some conversion is necessary. There are two control panels in parallel and either could select a route (and control the LEDs). There are also some combinations of routes that are allowable in parallel. These require some logic between the buttons and the LEDs.
A 16 bit value is shifted one bit at a time to the Serial Out of the micro-controller. The micro-controller then generates a clock pulse to the serial to parallel converter. The data is rippled through the serial to parallel converter. The output pins (ie the parallel part) drive 16 LEDs turning the LEDs on and off as the data is rippled across. When the transfer is complete the serial to parallel outputs will be stable and the required LEDs will be activated.
To eliminate any clutter in the Coal_Basin_LED2 header file separate files are used to include the status definitions and the LED wirings:
Coal_Basin_Status.h. This file lists all the numeric values for the possible coal basin status. ie #define SILOS 1, #define WHARF1 2 etc. This file is included in all the files associated with this project. Unless the track layout is changed there should be no reason to modify this file
Coal_Basin_LED2_Wiring.h This file translates the state value into the pattern to activate the selected LED(s). This file is necessary since to avoid cross overs in the PCB design the wiring was not 1:1.
Wiring file
To eliminate some issues when generating the Library file these additional files should be given names which on a alphabetic listing fall after the main program files in this case Coal_Basin_LED2
The header file will
Include the previously defined Coal_Basin_LED2_Wiring file (that has in turn included Coal_Basin_Status)
Created a class Coad_Basin_LED2 where the constructor has two arguments indicating the pins for the clock and data out are defined.
The class includes two public methods begin( ) which will configure the clock and data pins and control_signals(unsigned int leds) where the parameter requests what LED is to be activated.
The main fetures of the implementation file are:
The begin( ) method where the pins to be used for the clock and data to the serial to parallel convertor are set as ouyputs
The _convert(unsigned int leds) method where the project state is converted to the desired pattern to drive the LEDs. Since more than one LED can be active at the one time the the result is built up bit by bit. For example if the input argument leds includes the value for SIDING1 then the pattern for the O_SIDING will be added to to a variable lights. That is if (leds&SIDING1) lights |= O_SIDING1;
Control_signals that takes the desired lights to be activated and generates the 16 clock pulses to ripple these into the serial to parallel convertor.
The *.ino file is simply a demonstration file that creates one instance/object of Coal_Basin_LED named route attached to pins A2 and A3
Turns each LED on and off in turn.
The main objective here is to modify the file Coal_Basin_LED2_Wiring.h to match the LEDs to the desired state. For this the given while(1) loop in the demonstration file can be replaced with route_dsp.control_signals(test_value); where test_value can be 1, 4, 8, 0x10 through to 0x4000 or alternatively the labels SILOS, WHARF1 .. could be used.
Some tests using input combination should also be used For example SILOS+EASTSIDE1, WHARF1+BASIN.
One fragment of the *.ino code was to turn each LED on and off in turn:
for (unsigned int count = SILOS; count <= EASTSIDE3; count *= 2) { }
This code worked well when there were only 15 Leds. That is EASTSIDE3 was 0x4000. However the final board had provision for 16 Leds so adding an extra state made EASTSIDE3 0x8000 the design was stuck in the for( ) loop. On the "last pass" count also will be 0x8000. Multiplying by 2 gives 0x10000 but since count is an unsigned 16 bit integer the stored value is actually 0x0000. Hence on the next pass when the loop should exit the test will be is 0x0000 less than 0x8000 which it certainly is so the program will continue in the for( ) loop. Then multiplying 0x0 by 2 will count as 0x0 forever .
To use with other projects it will be necessary to generate a library. The best sequence is
Using File Explorer for example make a *.zip file of all the files. They will be in the folder Coal_Basin_LED2 that is under your Arduino work directory. The new file will have label Coal_Basin_LED2.zip (**)
In the Arduino IDE select <Sketch> <Include Library> <add *.zip files> and select Coal_Basin_LED2.zip. This will create the library Coal_Basin_LED2 that will under libraries in your Arduino work directory.
To use the library include the following in the new program #include <Coal_Basin_LED2.h>. With the "<..>" the compiler will look in the libraries folder.
(**) When creating the *.zip file it will be given the name of the first file. The desired name will be Coal_Basin_LED2 so if necessary the name of the *.zip should be changed to Coal_Basin_LED2. This will not be necessary if any additional header files have a name that in alphabetical order fall after Coal_Basin_KED2. This was recommended in the "Design Include Files" section
This project will be used as the input to the Coal_Basin2 projects. The main elements are:
Coal_Basin_Buttons2 that reads the buttons on the control panel
Coal_Basin_LED2 (This file) that controls the LEDs that display the chosen track.
Coal_Basin_Control2 that uses the button inputs to drive the LEDs and the required servos that control the points. ie Coal_Basin_Servo2.