Model Railway Turntable 48 position

Dalenrose

Member
I have done some very basic work with Picaxe and have so far managed to operate a stepper motor to turn an ordinary DC model railway turntable. This very simple method uses one pin for each knob selection position and by using a Picaxe 40x2 allows the selection of 24 positions and while this works fine, it will not operate a 48 position turntable. I am sure this has already been figured out more than once before but I can't find any examples. Could somebody show me how to achieve the extra positions or tell me where I can find some information/code/circuits that might enlighten as to how it is done.
Thanks
Dale
 
You could use five 10-input BCD encoders like the CD40147. This would use 20 input pins on a PICAXE.

Alternatively, you could make a a diode matrix using around 100 diodes and 6 pullup resistors to make a 48 input to 6-pin encoder. This would use 6 input pins on the PICAXE - you might be able to use something as small as a PICAXE 14M2 for this one!
 
Possibly use some 74**165? These can be cascaded until you have enough to read all the inputs and then read by the PICAxe using only three pins. You can also "pseudo" multiplex the switches to the '165 in banks of eight using additional pins on the PICAxe, this saves on hardware, but at the expense of processor pins. I've built units using both methods and found the '165 (and the output equivalent '595) to be superb. If you spot a machine dotted with '165s and '595s out in the wild it could well be one of mine...
 
A 48 position turntable ?. That's some big railway !.

For selecting a single value from a big number, I'd be looking to use a keyboard.
There are plenty I2C keypads about.
 
Hi,

Welcome to the forum. I don't know if there is any formal definition or specification for an "Ordinary (48 position) Model Railway Turntable" and you have not given given any link for "This very simple method..", but I assume that your requirement is for a 48-way (pointer) Control Knob and/or a method to confirm/calibrate the direction of the turntable, should the PICaxe "forget" the direction (e.g. due to a power glitch), or some of the Motor Steps might get "lost". An ancillary question that could be relevant is; Does the design need, or account for the fact that the turntable can have "180 degree rotational symmetry" (i.e. the Engine can usually enter and leave in either direction along the turntable's track)?

As Pete says, the basic coding needs only 6 pins, by using Binary Code with a Diode Multiplex. Although this arrangement might need over 100 "General Purpose / Signal" diodes (e.g. 1N4148) they could cost less than a cent each and give you useful control over the "numbering scheme". A conventional "Rotary (wafer) Switch" will generally have a "Break Before Make" contact action, so the in-between positions will give a "zero" output. Thus the desired directions could be 1 to 48 (which can be coded in 6 bits, with a maximum of 63). These might be represented in the (now outdated) "Octal" counting scheme, but we could be a little more clever using a "base 7" sequence which conveniently gives 7 * 7 = 49 combinations, i.e. 48 directions + 1 "don't know" (zero). The advantage of this is that it needs a maximum of 4 diodes per position, i.e. the 7 digits would be binary coded as %000 , %001 , %010 , %011 , %100 , %101 and %110 (i.e. with %111 omitted, which would need 3 diodes).

I don't know whether you are (or would be) using Mechanical (e.g, rotary contact or micro-) switches, as there are other alternatives such as Optical or Magnetic (Reed or Hall Effect) sensors. If it is important that you don't have a "Don't Know" between switching positions, then you should look up "Gray Code" (which may need some modification for 48 directions) that is more relevant to these latter types of switch technology.

Perhaps one should also consider a "Minimalistic" design. In principle, a simple Potentiometer can indicate 48 (or many more) values in association with one input pin to the PICaxe's Analogue-Digital Converter. If you want physical mapping to full 360 degrees rotation, then Multi-Turn Pots do exist, and also 360 degree Continuous Rotation types, e.g. used with some Wind Direction Vanes on Weather Stations. Alternatively for the turntable, just one direction (confirmation) switch might be sufficient: If the direction gets Unknown or Lost, then the Program rotates the turntable until the (or a) switch closes, and then counts the Motor Steps to each subsequent position. That might just fit in my favourite 08M2. :)

Finally, if you want a solution much nearer to your original concept (i.e. 24 input pins), then the 24 Odd-Numbered target directions could be connected directly (or maybe via a diode) to the 24 input pins. Then the 24 Even-numbered target directions would connect to two adjacent pins via diodes (to prevent all the pins being short-circuited together). The Software could look for a "1" input and then check if an adjacent pin is also a "1". However a better design might use 16 inputs: Then each adjacent group of three target directions would connect diodes to three adjacent inputs in the form: A+B , B and A+C (then B+C , C and B+D , etc.). The advantage of 16 inputs is that they can map directly into a single Word (16-bits) which makes Lookup Tables, for example, much easier.

Cherers, Alan.





I have done some very basic work with Picaxe and have so far managed to operate a stepper motor to turn an ordinary DC model railway turntable. This very simple method uses one pin for each knob selection position and by using a Picaxe 40x2 allows the selection of 24 positions and while this works fine, it will not operate a 48 position turntable. I am sure this has already been figured out more than once before but I can't find any examples. Could somebody show me how to achieve the extra positions or tell me where I can find some information/code/circuits that might enlighten as to how it is done.
Thanks
Dale
 
It seems obvious I am out of my depth and thanks for the replies and thanks Alan for the very detailed response.
It’s not that I want a solution similar to the original because I thought I should be able to get away with a smaller Picaxe and hardware in general.
Is it very hard to get code to read the input from a keypad? If not could I get some tips on how to do that?
The simple method I used on 7 track table is attached.
 

Attachments

Hi,
Is it very hard to get code to read the input from a keypad? If not could I get some tips on how to do that?
Perhaps not very hard, but it's certainly not trivial. I'm sure there is some working code on the forum (tip: try "hippy" as an author) but all I found in a quick forum search were threads from members who were having "Problems" with their Keypad Code. However, none of those threads concluded with a working example, either from the OP or anybody else. Apart from the relative complexity of the Programming* (particularly if you want to enter a sequence of Digits), there are potential issues concerning the "User Interface". For example: Will the User know/remember which buttons to press, in perhaps a few years time? How does the "computer" confirm that it has understood (correctly) what is desired? How does the User correct a mistaken or accidental entry?
* Some PICaxe applications put resistors across the Keypad Matrix (so that it can be read on a single pin by the ADC), rather than get involved with some of the complexities of scanning the multiplex.

A very common problem with many PICaxe Forum "Questions" is that the OP starts with an assumed "Solution", rather than a REQUIREMENT. For example do you really want to control 48 "Lines" ? Would you prefer a Rotary (pointer) Knob , or a Row of switches, or a Numeric Keypad , or perhaps even a Keyboard? PICaxe actually supports the direct connection of a standard PC keyboard, although with a now rather outdated interface.

A pointer knob has potentially a very good User-Interface; easy to adjust with visual feedback of its setting, etc.. However, a Potentiometer might be more suitable than a Rotary Switch; the standard 1/4 inch shaft, just three connection pins and the ADC value can be interpreted by the PICaxe for almost any number of "Positions" from 2 up to 50 or more. Similarly, if you want a Keypad Interface, then perhaps an Off-the-Shelf (Universal) Infra-Red Remote Control would be easier? Again directly supported by PICaxe Basic, with a single-pin interface and it might even be just screwed down onto a baseboard.

Not a criticism, but it's usually best to attach .BAS (or I prefer .TXT) Program files directly from the PICaxe Program Editor, or even post it within [code ].. [/code] tags, so that we can all immediately see the main details. Also, if you're going to Symbolise variables such as "CurrentPos" , etc., then you really should do a Global "Search and Replace" to ensure that there aren't any of the original "w5"s left in the program. :)

Cheers, Alan.
 
Hi

I know it sounds and looks a bit ordinary that I’m not sure what system I really want to use.

I thought to use a digital keypad (not the PC keyboard) just to input 2 digits and pick that up (somehow) and use that info. At this stage I was not thinking about input errors. I just thought if the selection was not in range the code would just go back to the start and wait for “in range“ input, if the selection was 25 instead of 5 the table would go to position 25 and user would have to make a new selection to go back to 5.

The rotary switch was good, but the way I was using it required a Picaxe pin for each position and that had too many limitations both in pins and switches, and I didn’t really want a bank of switches if I could get out of it.

I tried a rotary encoder but haven’t had any luck there so far either, so your potentiometer (whether that’s the same or different) idea probably looks to be the solution. All I would need to know now is how to do it.

I didn’t really think that anybody would be looking at the code, as you can probably see. But I will take your advice and comments on board and mend my ways in the future.

Thanks for your advice and help. Cheers Dale
 
I would not use a potentiometer directly, too risky that it gets moved accidentally.
However, if you use a pot and a pushbutton to activate it, then it's a lot less risky.

This is probably the simplest control method.
 
Hi,

A Keypad is indeed a precise and familiar User-Interface, but that can be part of the problem, because people have expectations about how it should work. However, I wonder how many people don't realise that a Calculator/Computer numeric layout is "Upside Down" compared with a Telephone or Remote Control. Also your reference to "5" raises the issue, did you mean "05" ? Actually 5 (and 6 - 9) shouldn't ever be followed by another digit so the program could be arranged to operate accordingly. But 1 - 4 could require a second digit, and what about "0", or should it be "00" ? The "expectation" is that a single digit will time-out after a few seconds (and an accidental button press "disappear" eventually?), but that needs a timer which generally should not be "blocking" (i.e. NOT a PAUSE/WAIT). That perhaps leads into the realm of Interrupts, which is why I said the design is not trivial.

I'm not aware of an Off-the-Shelf, 48-way rotary switch; it could be fabricated with a "wiper" running over custom PCB tracks, but it's not a very reliable concept - do you ever need to clean your railway track? Rotary Switches normally have a "Detent" mechanism (such as a spring and ball-bearing) to ensure that it holds in one of the functional directions. This also could be applied to a Potentiometer, to avoid Busby's issue. In projects like this where the Program is basically a means to an end, we don't know what "tools" might be available, in this age of CAD and (3D) Printers, etc.. So the "manufacture" of a Scale (for a Pointer-Knob) or even a (3D printed) Detent might not be an issue. But if it is, then "gear-type" components from Lego, Meccano or Robot/Model-making suppliers could offer a solution. There is a large Cottage Industry of sellers around the world who supply second-hand (or new) Lego parts, sometimes at very reasonable prices.

There definitely is example code on the forum for a Rotary Encoder, because I wrote some of it. ;) However, disadvantages of a RE are that it generally has limited resolution and needs some form of "Display" to confirm its Position. But both of these might be solved with a Gear Train and a mechanical Pointer.

A potentiometer normally has a rotational range between about 240 and 270 degrees, which is why I asked if the Turntable has a full 360 degree rotation. The Potentiometer is normally wired across the PICaxe's supply rails, but not all of its range needs to be used. Also, a gear train could be employed, since the active range can be divided into almost any number of "Positions". Here is some example program code to show how simple it could be. I've used the READADC10 command, partly to highlight that it exists, but its output range from zero to just over 1000 can be more useful than the 0 - 255 from the normal READADC (byte) instruction. I've used a typical mathematical calculation, that might be used when marking the Scale from pre-tested pointer-positions. But if the Scale was already printed, then you could use a manual calibration method similar to the Turntable Rail alignments. For this, I've shown a LOOKUP method to indicate how the code could be simplified. A DATA / EEPROM / TABLE method can be used, but reading Word values from a byte table is a little untidy.
Code:
SYMBOL Potpin = b.1
SYMBOL Potposition = w2
SYMBOL RequestedPos = w6
SYMBOL Linenumber = b6
main:
  READADC10 Potpin , Potposition           ; Range from 0 to 1023
  for Potposition = 0 to 1023 step 10         ; For testing only ***
  Linenumber = Potposition + 13 / 21 MIN 1 MAX 48       ; Output Range 1 to 48
  LOOKUP Linenumber , (0 , 250 , 232 , 242, 256 , 270 , 282 , 296 ) , RequestedPos    ; Can be as long as needed
  GOTO SetSteps                     ; Or just "Fall Through"
SetSteps:
  sertxd (cr,lf, #Potposition ," ", #Linenumber," ", #RequestedPos)      ; For Testing Only ***
  next                               ; For testing ***
The "magic number" 21 is used to give just over 48 integer values after the division, with the other constants giving the two sectors at the ends 8 ADC steps larger than the others. Note that most instructions such as LOOKUP and ON ... GOTO (aka BRANCH) start counting from zero, so I've put in a filler 0 to be compatible with the calculated Range from 1 to 48.

Cheers, Alan.
 
Mind it I intersect with an inspiration. I just happen to be selecting a switch for a QRP rig I'm rebuilding to select pre-programmed CW messages.

Forget the rotary switch, forget the keyboard, "Why not use a thumb-wheel switch?". They can be pricey new, but surplus can be found.

They come in all sizes and outputs, like BCD (binary Coded Decimal), 4 bit Binary or (a bad choice) One-of-N (N is switch positions)

This switch is push-button controlled (BCD) and I can select 0 to 99 positions. I will be reading the switch with a single I/O expander PCF8574 using I2C interface. This IO expander can be daisy chained up to 8 ports, each individually addressable for reading or writing (up to 64 I/O ports).

Needs only 2 ports on the MCU. The code might be a little more complex. Even an o8M2 could handle the job.

thunbswitch_selector.jpg
 
Here's my tu'pennorth,

The controls are three pushbuttons, Up, Down and Set.
A two digit LED display shows steps. ( Needs code writing for this. )

Operation is as follows,
When either UP/DN button is pressed a timer is started.
The SET button, only if pressed while timer running, sets the selected step.
The selected step, 1 to 48, is then used to lookup the count for the stepper motor.
The actual motor drive also needs writing.

I've only run this in the simulator, so timings will change on a real chip. A few pauses may be needed.

Code was written for a 14M2, but should run on any PICAXE with enough pins.

Code:
' Turntable selector
' ------------------

' Use UP/DN buttons to change value. Either button sets timer running
' Use SET button to set selected value. Only works during timer.
' When timer expires, display will change to working value.

' Adjust timeoutval to be about 3 seconds

' Use a two digit LED to show selection. ( Write code to suit )



' Symbols

symbol UPbtn  = pinB.1        ' UP pushbutton
symbol DNbtn  = pinB.2        ' DN pushbutton
symbol SETbtn = pinB.3        ' SET pushbutton

symbol valtemp     = b4        ' Value changed by UP/DN buttons
symbol valwork    = b5        ' Value selected by SET button
symbol LEDval     = b6        ' Value to show on LEDs
symbol seltimer   = w4        ' Timer for SET to be pressed
symbol stepsval    = w5        ' Result of selection

symbol timeoutval   = 30    ' Duration of SET timer, adjust to suit.
symbol TTsize       = 48      ' Size of turntable


' Code starts here
' ----------------

do
    if UPbtn = 1 then        ' Count up
        inc valtemp
        if valtemp > TTsize then ' Rollover to bottom 
            valtemp = 1
        endif
        seltimer = timeoutval ' start timer
        ' pause 100    ' include a pause if count rate too fast
    endif

    if DNbtn = 1 then        ' Count down
        dec valtemp
        if valtemp = 0 or valtemp = 255 then ' Rollover to top
            valtemp = TTsize
        endif
        seltimer = timeoutval ' Start timer
        ' pause 100    ' include a pause if count rate too fast
    endif

    if seltimer > 0 then ' Timer is running
        
        dec seltimer ' Decrement timer
        
        LEDval = valtemp  ' Display input value on LEDs
    
        if SETbtn = 1 then ' SET button pressed
            valwork = valtemp ' Copy valtemp to working 
        endif
    
    else    ' Timer is expired
        
        LEDval = valwork    ' Display working value on LED
        
        ' Bumpless changeover
        if valwork > 0 then
            valtemp = valwork ' Set valtemp to current working value
        endif
    endif

    ' DriveLEDs 
    '  Put routine to drive LEDs here.
    '  LEDval is the digits. Add some fluff to show if timer running.
    '
    
    ' Convert valwork to step count.First entry is not used.
    ' Change values to whatever each position requires.
    '               posn 1  2  3 etc ...
    lookup valwork,(000,10,20,30,40,50,60,70,10,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300,310,320,330,340,350,360,370,380,390,400,410,420,430,440,450,460,470,480),stepsval 
    
    ' Motor drive
    if stepsval > 0 then ' only drive motor if a selection has been made
        '
        ' Put motor drive stuff here    
        ' sertxd ("motor ",#stepsval,cr,lf)
        '
    endif
    
    ' Diagnostics to serial 
    sertxd ("Temp : ", #valtemp, tab, "Work : ", #valwork,tab,"Time : ",#seltimer,tab,"LED :",#LEDval,tab,"Steps : ",#stepsval,cr,lf)

loop
 
Forget the rotary switch, forget the keyboard, "Why not use a thumb-wheel switch?".
That's a good idea !.

However, it might still need a timeout to allow the selection to settle. Otherwise the turntable could get confused with rapidly changing numbers.
 
Some PICaxe applications put resistors across the Keypad Matrix (so that it can be read on a single pin by the ADC), rather than get involved with some of the complexities of scanning the multiplex.
It is truly amazing what you can learn just reading threads! I love low tech simple answers for complex questions!
For example, detecting not only the keys on a Keypad Matrix but the position of a rotary switch or encoder or the state a dozen bumper switches with just one pin!
Gramps
 
Back
Top