Jump to content


Google Ads are only seen by non-members of RMweb - Create an RMweb account and you'll only receive modelling ads.

Photo
* * * * * 5 votes

DCC Controlled (PECO) Turntable Project.

Arduino stepper dcc turntable peco




  • Please log in to reply
497 replies to this topic

#1 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 12 November 2013 - 14:52

There have been many threads/posts relating to motorising/controlling turntables, utilising DC motors, Stepper Motors, gears, gearboxes, toothed belts, meccano etc. all with varying degrees of complexity to get the correct indexing of the bridge deck. None of these (that I could find) also incorporated DCC control.

This series of posts will describe the construction of a simple DCC controlled turntable utilising a stepper motor directly connected to the turntable shaft (no gearboxes, gears, belts) and an ARDUINO microcontroller programmed to do all the hard stuff.

This project is ongoing (working prototype complete), comes in good faith, but no guarantees.

(The software could be modified to operate from a rotary switch if DCC operation not required)

Part 1: The Bits

Components required for this project. (Plus approx. cost)

ARDUINO UNO microcontroller                   (eBay) £16
ADAFRUIT Motor Shield V2                        (eBay) £16
Stepper Motor (SM-42BYG011-25)            (eBay) £16
Hall Effect Sensor (RS370-6896)                           £5
Magnet (RS189-5512)                                           £6
Bellows Coupling (RS693-2467)                            £17
Opto Isolator 6N137 (RS671-1359)                       £2
2 off 10k resistors
1 off 1K resistor
1 off IN4148 diode
4 off Rubber mountings (Farnell 1466996)           £3

                                            Sub Total (Approx.)  £81

Plus a Turntable of choice. (PECO OO LK55)     £39

                                                                   Total £120

Shopping around could save a few pounds.

 

Part 2 to follow - Turntable Hardware (stepper motor/sensor mounting) Construction.

 

YouTube video of working prototype.

 

(any erratic behaviour is due to the YouTube link)

 

A PC and SPROG II Interface were used to generate the DCC commands but any DCC controller would suffice.

Ray.


Edited by tender, 13 February 2014 - 13:47 .

  • Craftsmanship/Clever x 5
  • Informative/Useful x 2

Google Ads are only seen by non-members of RMweb - Create an RMweb account and you'll only receive modelling ads.

#2 alangdance

alangdance

    Member


  • Members
  • PipPip
  • 186 posts

Posted 12 November 2013 - 15:40

can't wait for the next installment



#3 corax67

corax67

    Member


  • Members
  • PipPip
  • 3,235 posts
  • LocationSunny Kent

Posted 12 November 2013 - 16:36

Top man Ray - looking forward to this thread developing.

#4 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 13 November 2013 - 15:38

Part 2: Turntable/Motor mount  Construction

 

This project uses the PECO LK55 turntable kit which is relatively straightforward to build. If you’re using another make, you’re on your own, but the following for the PECO T/T will be a guide.

 

Build the turntable as described in the supplied instructions but don’t fix the bridge deck to the well using the retaining collar. Paint and weather as desired.

 

Drill a hole in one end of the Bridge deck cross member for the sensor magnet and mount so it just misses the well when rotated. Use some packing washers for this.

 

turntable5.JPG

 

turntable5a.JPG

 

The track pick-up for the Bridge deck is via a split ring arrangement, if you want to use sound locos you may want to modify this as there is a dead spot in the rotation. There are other threads discussing this.

Find the dead spot (using a meter and rotating the deck) and mount the Hall Sensor between the rail positions on the outside of the well up under the lip of the well (superglue). The embossed writing on the sensor should be against the well wall. This will be the reference point for the turntable controller and avoided as one of the exit roads.

 

turntable6.JPG

 

The stepper motor is mounted directly under the Bridge deck pivot so the base of the well needs strengthening as it’s a bit flimsy. Cut a disk of 6-9mm ply the same diameter as the well. Drill a 20mm hole in the centre. Glue this to the base of the well.

I can’t remember if the T/T kit comes with a drive spindle, but this is made from 4mm diameter steel rod. It should be a tight fit into the deck spindle, but to make sure it doesn’t slip clean and glue in place with superglue.

Now insert the deck, mark and cut to length so that about 20mm of the 4mm shaft protrudes below the ply base.

 

This is where the description will vary from the photos as I had already made a mount for a DC motor which I have modified for the stepper motor. You may think of another way to do this but it is essential to get the spindle of the motor and bridge deck shaft in as close alignment as possible.

 

Cut a piece of 6mm ply about 70mm square and mark out a hole in the centre and four mounting holes 31mm apart to form a square around the centre hole.

Drill the centre hole out to 8mm and the four mounting holes to 3mm.

Mount the Stepper motor to this using the rubber mounts and 4 off M3 screws.

 

Cut 2 pieces of 6mm ply 70*40mm.

Obtain a short (20mm) length of brass tube 4mm ID, 5mm OD. (Eileens Emporium) and a 5mm straight coupling (http://www.technobot...5mm-to-5mm.html).

Or if you can find a 4-5mm straight coupling use this instead of the brass tube.

 

Insert the bridge into the well and place the brass tube over the shaft. Attached the 5mm coupling, tightening the grub screw so it holds the brass tube in place.

I found it best to invert the turntable for the next bit, supporting the bridge deck on a block of wood.

Add the stepper motor to the other end of the coupling such that the distance from the 6mm ply mount to the underside of the well is about 34mm. Make sure the deck wheels are in contact with the base of the well. The motor and mount are now supported by the Bridge deck shaft above the base of the well. This ensures that the motor shaft and T/T shaft are in alignment before glueing in place.

Now glue the two side pieces of 70*40mm ply to the well base and two opposing sides of the motor mount.

When dry, remove the straight coupling and replace with the bellows coupling. Adjust the height of the deck so the wheels just contact the well rails. Check that the bridge deck rotates freely. (If your careful you may be able to do this using the bellows coupling instead of the straight coupling)

 

This is how my mount looks, but as I said earlier I modified mine from a previous DC motor mount, but you should get the idea.

 

turntable3.JPG

 

Next Instalment – The Electrics


Edited by tender, 13 November 2013 - 16:32 .

  • Craftsmanship/Clever x 4

#5 alangdance

alangdance

    Member


  • Members
  • PipPip
  • 186 posts

Posted 13 November 2013 - 16:12

This is looking like a very good article.

Do you think it would be possible to have the motor above the baseboard and driven from a timing belt.

 

Alan



#6 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 13 November 2013 - 16:39

This is looking like a very good article.

Do you think it would be possible to have the motor above the baseboard and driven from a timing belt.

 

Alan

Hi Alan, It should be possible but this will put a sideways pressure on the T/T shaft. The PECO bearing is not very good, just a plastic plain bearing surface which is why I chose to mount the motor directly under the turntable effectively using the motor bearings to support the T/T deck.



#7 alangdance

alangdance

    Member


  • Members
  • PipPip
  • 186 posts

Posted 13 November 2013 - 17:53

Thanks fot the comments. I like your idea but will have to look into the mounting of the motor as I have no room under the baseboard

 

Alan



#8 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 14 November 2013 - 16:10

Part 3 The Electrics.

 

One thing that’s puts a lot of people off projects like this is the complexity of the electrics involved, me included. However I’m pleased to say that the amount of wiring involved with this project is minimal, with all the complex stuff being done by the Arduino/Adafruit modules.

All you need to do is solder a few connectors to the Adafruit module and build the DCC interface circuit on a piece of strip(vero)board.

 

DCCIF.png

 

Above is a tidied up version of the DCC interface circuit with connectors for all connections, but you can just solder wire to the board as I have done in the prototype pictured below with the Arduino and Adafruit boards. (The Arduino and Adafruit boards are plugged together)

 

Component Values

R1/2 10k

R3 1K

D1 1N4148

U2 6N137

 

 

turntable2.JPG

 

(If you really have trouble with this send me a PM or post on here and I’ll try and do a VEROBOARD layout of the various components and where to cut the tracks.)

 

The stepper motor is connected to the Adafruit board connections M3 and M4.

Blue/Yellow to M3, Green/Red to M4.

 

The connections to the Adafruit board on the DCC I/F circuit above are as printed on the board.

 

adafruitsmall.jpg

 

The other thing you will need is a 9-12V DC supply (approx. 0.5amp) for the stepper motor supply to the Adafruit board.

 

This is a photo of the complete prototype wiring. 

 

turntable4.JPG

 

Of course you will also need a DCC signal from your layout for testing (or as I have done, connect to a SPROG II Decoder Programmer connected to a PC).

 

To install the software on the ARDUINO you will also need a PC.

 

Next – Installing the Arduino integrated development environment (IDE) on a PC/Initial Testing


Edited by tender, 14 November 2013 - 16:14 .

  • Informative/Useful x 2
  • Like x 1

#9 alangdance

alangdance

    Member


  • Members
  • PipPip
  • 186 posts

Posted 14 November 2013 - 16:38

Thanks for the next stage. Can't wait for the next stage.

 

Alan



#10 Robin2

Robin2

    Member


  • Members
  • PipPip
  • 696 posts
  • LocationNear Manchester

Posted 15 November 2013 - 16:56

Thanks fot the comments. I like your idea but will have to look into the mounting of the motor as I have no room under the baseboard

 

Alan

I've seen turntable projects where the table is turned by a DC motor driving a worm gear with a large gear fixed to the table shaft. That puts the motor to the side of the table rather than directly under it. You could use a similar arrangement with a stepper motor. If you had a long shaft connecting it to the worm gear it could be well out pf the way.

 

...R



#11 alangdance

alangdance

    Member


  • Members
  • PipPip
  • 186 posts

Posted 15 November 2013 - 17:45

Yes I have thought of this using the  worm gear and large gear you get with the Peco turntable kits but still will need the motor above the baseboard.

 

Alan



#12 cambriancoaster

cambriancoaster

    Member


  • Members
  • PipPip
  • 138 posts
  • LocationStaffordshire

Posted 17 November 2013 - 19:35

Light at the end of the tunnel? - very likely!  

 

I am absolutely delighted to see this thread since I have been puzzling for ages as to how I might motorise and control my Mercian models turntable deck under DCC for my Pwllheli layout (see my thread Pwllheli next stop and photo below).   This could very well be the answer to my prayers so I look forward to seeing Part 4 with keen anticipation. 

 

100_4416S.jpg

 

Best wishes

 

CC



#13 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 17 November 2013 - 21:25

Sorry for the delay with Part 4, forgot to bring the Word file home this weekend with the latest update, will post tomorrow.
Ray.

#14 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 18 November 2013 - 16:14

Part 4 Installing the Arduino integrated development environment (IDE) on a PC/Initial Testing

 

Firstly you will need to install the Arduino IDE on a PC/Laptop so you can upload the code to the Arduino Uno module.

 

This link http://arduino.cc/en...de/Windows#toc1 will give full instructions on how to install the IDE, connect the Arduino Uno to your PC/Laptop and run a simple program called ‘Blink’ to check that the Arduino is working ok (I’d leave the Adafruit and DCC I/F disconnected at this stage until you’re sure the basic Arduino working ok).

 

Next you will need to download the Libraries for the Adafruit Stepper Motor Module and the DCC Decoder control.

 

For the Adafruit Library go to http://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/install-software and click on the ‘Download latest Adafruit Motor Shield V2 Library’ button.

 

This will download a zip file ‘Adafruit_Motor_Shield_V2_Library-master.zip’ to a location on your PC.

Browse to this file double click on it and click ‘Extract all files’ giving a new folder  Adafruit_Motor_Shield_V2_Library-master.

Change the ‘-‘ to an ‘_’ as Libraries can’t have ‘-‘ signs

Now copy (or move) this folder and its contents to  c:\……..\My Documents\Arduino\libraries\

(………  will probably be your user name)

 

Next we want the DCC Libraries.

Download them from here: http://www.mynabay.com/arduino/2-uncategorised/14-arduino-dcc-monitor

 

‘DCC-Decoder Library Version 4’

 

Again extract the files from the zip file and change the ‘.’ In the folder name (dcc_decoder.v4) to a ‘_’ and move the folder to C:\.........My Documents \Arduino\libraries\

 

You should now have two new folders in c:\……..\My Documents\Arduino\

 

Adafruit_Motor_Shield_V2_Library_master

dcc_decoder_v4

 

Now plug the Adafruit onto the Arduino module and connect up the Stepper Motor and Sensor (disconnect Arduino from the power/USB first). You will now need to supply 9-12DC to the Power terminal of the Adafruit module for the Motor supply and reconnect the USB lead from the Arduino to the PC.

 

Start the Arduino IDE (the same way as for the ‘Blink’ test).

 

Now browse to the ‘Stepper Test’ sketch from the ‘File’ menu.

 

steppertest.png

 

And upload the sketch.

This sketch will spin the stepper motor back and forth through 4 different modes of operation.

Single Stepping, Double stepping, Interleaved Stepping and Micro Stepping.

Don’t worry how violent the movement is for this test, just don’t leave a loco on the bridge when you do it. The motion will be a lot smoother with the turntable control sketch.

 

Next we want to test the DCC interface.

 

Browse to the DCC_Monitor sketch from the ‘File’ Menu

 

dcctest.png

 

And upload the sketch.

 

At this point you need to have your DCC track connected to the DCC interface board and be able to generate some DCC commands, ideally Turnout commands for Accessory Decoder 200. This will depend on your system but generally push buttons to:

 

Select Accessory

200 enter

Normal (or Reverse)

 

(NCE PowerCab)

 

From the Arduino IDE Window, open up the Serial Monitor by clicking on the small Icon near the top righthand side of the window. This will open up a new window and if all is well you should see some data scrolling up the screen.

 

dccmonitor1.png

 

Don’t worry if they’re not the same as these, if you’re getting anything it’s probably working.

 

Now send a few Dcc Accessory commands from your DCC controller.

You should see an extra line of data appear each time.

 

dccmonitor2.png

 

Again don’t worry if the numbers are not the same as these.

 

OK, so hopefully we now have the Stepper and DCC Interface working, next test is for the Sensor for the deck reference point.

 

That will be in Part 5 as I’ve not written any test code for that yet, (it just worked so I didn’t bother) along with a test sketch for setting the speed/acceleration and deceleration and a 180deg turn.

 

Ray


Edited by tender, 18 November 2013 - 16:22 .

  • Craftsmanship/Clever x 3

#15 alangdance

alangdance

    Member


  • Members
  • PipPip
  • 186 posts

Posted 18 November 2013 - 19:28

This just get better. Thanks again.



#16 cornishmick

cornishmick

    Member


  • Members
  • PipPip
  • 52 posts
  • LocationFalmouth Cornwall

Posted 19 November 2013 - 01:01

Fantastic :no:



#17 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 19 November 2013 - 15:14

Part 5 - Testing the Reference Sensor and setting the Speed and Acceleration

Firstly mark a point on the end of the turntable deck containing the magnet so you can check the alignment and repeatability of the test.

For this test we need to create a new sketch for the code.

Start the Arduino IDE and from the 'File' drop down menu select 'New'

Now copy/paste the following code into the new sketch.
 


// Turntable test routine
//
// Requires the Adafruit_Motorshield v2 library 
//   https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
// And AccelStepper with AFMotor support 
//   https://github.com/adafruit/AccelStepper

// This sketch is for Adafruit Motorshield v2 only!
// Will not work with v1 shields

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

// Connect stepper with 200 steps per revolution (1.8 degree)
// to the M3, M4 terminals (blue,yellow,green,red)

Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);

// you can change these to SINGLE, DOUBLE, INTERLEAVE or MICROSTEP!

// wrapper for the motor! (3200 Microsteps/revolution)
void forwardstep2() {  
  myStepper2->onestep(FORWARD, MICROSTEP);
}
void backwardstep2() {  
  myStepper2->onestep(BACKWARD, MICROSTEP);
}

// Now we'll wrap the stepper in an AccelStepper object

AccelStepper stepper2(forwardstep2, backwardstep2);


void setup()
{  
  
  AFMStop.begin(); // Start the shield
  
  //configure pin3 as an input and enable the internal pull-up resistor
  pinMode(3, INPUT_PULLUP);
  //read the sensor (open collector type) value into a variable
  int sensorVal = digitalRead(3);
   


// step forward to sensor index point
  while (sensorVal == HIGH) {
    sensorVal = digitalRead(3);
    forwardstep2();
      delay(50);
  }
  delay(5000);
  
// set stepper speed, acceleration and position 
  stepper2.setMaxSpeed(50.0);
  stepper2.setAcceleration(10.0);
  stepper2.moveTo(800);  
  
}

void loop()
{
    // Change direction at the limits

    if (stepper2.distanceToGo() == 0)
       { delay(5000);
	stepper2.moveTo(-stepper2.currentPosition());
       }

    stepper2.run();
}

 

Before uploading this sketch save it (File/Save As) giving it a new name, TurntableTest.

(TurntableTest will now appear in the 'File/Sketchbook' menu for future loading).

Now upload the sketch.

The turntable should now rotate slowly until the mark reaches a point very near the sensor on the well.
It will pause here for 5 seconds during which time mark a point on the well side exactly by the mark on the deck.
The deck will now continue to rotate another 90 degrees and pause for 5 seconds, again, mark this position on the well side.
Now the deck will rotate back through 180 degrees and pause for 5 seconds. You guessed it, mark this position as well.

The deck will continue to turn back and forth between these two positions until you turn the power off.
Leave it running for an hour or so occasionally checking that the deck marker and end points (marked on the well) align at the end of each sweep.
If it looses alignment (misses steps) it would suggest you have some unwanted friction/shaft misalignment in the system somewhere which will need sorting out.

Notice at the start and end of each sweep the deck accelerates from rest and decelerates to stop, you might also notice some jitter in the motion.
(if you have a high rate of jitter it may suggest undue friction still in the system)
The rate of acceleration/deceleration and the turntable speed are easily adjusted and will effect any perceived jitter.
This is going to be very subjective so its up to you to play with the speed and acceleration parameters to suit your needs.

Look for this section of the code in the sketch:
 

// set stepper speed, acceleration and position 
  stepper2.setMaxSpeed(50.0);
  stepper2.setAcceleration(10.0);
  stepper2.moveTo(800);  

The setMaxSpeed parameter (50.0) adjusts the speed of rotation in steps per second. A value of 100 would take about 32 seconds for a complete rotation so I wouldn't go any faster than this. If you slow it down too much you will start to introduce jitter.

The setAcceleration parameter (10.0) adjusts the rate of acceleration/deceleration in steps per second per second. 10 is probably to slow but anything above 100 probably wouldn't be noticeable at the maximum speeds we are using.

So have a play around with these values and let us know what values you decide on.


.Next instalment - Adding DCC control

Ray.


Edited by tender, 20 November 2013 - 14:55 .

  • Thanks x 2
  • Craftsmanship/Clever x 2

#18 cambriancoaster

cambriancoaster

    Member


  • Members
  • PipPip
  • 138 posts
  • LocationStaffordshire

Posted 19 November 2013 - 16:57

Ray

 

This topic gets more and more interesting.   Inspired by it I have spent several happy hours viewing the Arduino/Adafruit websites and associated U tube tutorials and am quite amazed at the possibilities their hardware and software offer for DCC model railway control applications.   I cannot wait to buy the items and have a play with them (initially to try and motorise my turntable). 

 

One question though - do you think it would be possible for the Hall Effect Sensor to be replaced by an infra red one (since this might be easier to fit in my application)?

 

Many, many thanks for introducing me to the world of Arduino and Adafruit.

 

CC 



#19 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 19 November 2013 - 18:31

Hi CC,
If you look at the video you will see a hole in the base of the well, this was originally for an opto device but I never got around to installing it before I found the Hall effect one. No reason why it shouldn't work with the existing circuit as long as it has an open collector output which pulls down to 0v when the deck passes it. I might connect up one and see how the accuracy compares to the Hall device.
Ray.
  • Informative/Useful x 1

#20 cambriancoaster

cambriancoaster

    Member


  • Members
  • PipPip
  • 138 posts
  • LocationStaffordshire

Posted 19 November 2013 - 21:31

Hi CC,
If you look at the video you will see a hole in the base of the well, this was originally for an opto device but I never got around to installing it before I found the Hall effect one. No reason why it shouldn't work with the existing circuit as long as it has an open collector output which pulls down to 0v when the deck passes it. I might connect up one and see how the accuracy compares to the Hall device.
Ray.

 

 

Thanks again Ray.   Your response is most encouraging.   If you do try out an opto device I would be very interested in learning whether it works (and, of course, details of the device you use).   In the meantime I'll search the web and see if I can find a suitable sensor.

 

Best wishes

CC



#21 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 20 November 2013 - 10:33

Hi CC

You could try one of these: OPB716Z, it would be a direct replacement electrically for the Hall device.

 

Datasheet : http://www.farnell.c...heets/67415.pdf

 

I've found one in my odd bits box so will give it a try when I get a few minutes spare.

 

Ray.


  • Informative/Useful x 1

#22 cambriancoaster

cambriancoaster

    Member


  • Members
  • PipPip
  • 138 posts
  • LocationStaffordshire

Posted 20 November 2013 - 21:23

Hi CC

You could try one of these: OPB716Z, it would be a direct replacement electrically for the Hall device.

 

Datasheet : http://www.farnell.c...heets/67415.pdf

 

I've found one in my odd bits box so will give it a try when I get a few minutes spare.

 

Ray.

 

That saves me a lot of search time and is greatly appreciated.

 

I look forward to hearing your conclusions when you have tested your turntable set-up with the opto sensor.

 

Best wishes

CC



#23 PhilNE

PhilNE

    Member


  • Members
  • PipPip
  • 100 posts
  • LocationPreston

Posted 27 November 2013 - 12:51

I don't know whether I missed a step but you also need to download Accelstepper from here...... 

 

https://github.com/a...it/AccelStepper

 

for Part 5 - Testing the Reference Sensor and setting the Speed and Acceleration

 

 



#24 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 27 November 2013 - 22:10

I don't know whether I missed a step but you also need to download Accelstepper from here...... 
 
https://github.com/a...it/AccelStepper
 
for Part 5 - Testing the Reference Sensor and setting the Speed and Acceleration


Thanks Phil for pointing out my omission. Always a problem when you write up something a week or so later.
Sorry for the lack of updates, been otherwise occupied. Will try and get Part 6 posted by the end of the week which will add DCC control into the mix.

Ray.
  • Craftsmanship/Clever x 1

#25 tender

tender

    Member


  • Members
  • PipPip
  • 2,376 posts
  • LocationNorth Wales

Posted 29 November 2013 - 14:27

Part 6 Adding DCC Control

 

In this part we will add DCC control to the turntable setting one position about 45 degrees from the reference and another rotated 180 degrees from this.

This is controlled by an Accessory Command from your DCC hand set.

 

i.e Select Accessory Address 200, select Thrown or Normal (or whatever nonemclature your handset uses)

 

Switching between Thrown and Normal will rotate the deck by 180 degrees.

 

 

As before, start the Arduino IDE and from the 'File' drop down menu select 'New' and copy/paste the following code into the new sketch and save it as 'DCC Test'

////////////////////////////////////////////////////////////////////////////////
//
// DCC Turntable Control Test Routines (Accessory Address 200)

#include <DCC_Decoder.h>
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Defines and structures
//
#define kDCC_INTERRUPT            0

typedef struct
{
    int               address;                // Address to respond to

} 

DCCAccessoryAddress;

DCCAccessoryAddress gAddresses[1];

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Adafruit Setup

Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

// Connect stepper with 200 steps per revolution (1.8 degree)
// to the M3, M4 terminals (blue,yellow,green,red)

Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);

// you can change these to SINGLE, DOUBLE, INTERLEAVE or MICROSTEP!

// wrapper for the motor! (3200 Microsteps/revolution)
void forwardstep2() {  
  myStepper2->onestep(FORWARD, MICROSTEP);
}
void backwardstep2() {  
  myStepper2->onestep(BACKWARD, MICROSTEP);
}

// Now we'll wrap the stepper in an AccelStepper object

AccelStepper stepper2(forwardstep2, backwardstep2);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Decoder Init 
//
void ConfigureDecoder()
 {
    gAddresses[0].address = 200;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Basic accessory packet handler 
//
void BasicAccDecoderPacket_Handler(int address, boolean activate, byte data)
{
        // Convert NMRA packet address format to human address
    address -= 1;
    address *= 4;
    address += 1;
    address += (data & 0x06) >> 1;
    
    boolean enable = (data & 0x01) ? 1 : 0;
    
        if( address == 200 )
        {
            Serial.print("Basic addr: ");
            Serial.print(address,DEC);
            Serial.print("   activate: ");
            Serial.println(enable,DEC);
            
            if( enable )
              {
                stepper2.moveTo(400);
              }
           else
              {
                stepper2.moveTo(2000);
              }
       }
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Setup
//
void setup() 
{ 
   Serial.begin(9600);
   
   AFMStop.begin(); // Start the shield
  
  //configure pin3 as an input and enable the internal pull-up resistor
  pinMode(3, INPUT_PULLUP);
  //read the sensor (open collector type) value into a variable
  int sensorVal = digitalRead(3);
   
  //set stepper motor speed and acceleration 
  stepper2.setMaxSpeed(30.0);
  stepper2.setAcceleration(20.0);
//  stepper2.moveTo(800);

// if near reference point move away
  sensorVal = digitalRead(3);
  while (sensorVal == LOW) {
    sensorVal = digitalRead(3);
    forwardstep2();
      delay(50);
  }
  
// step forward to sensor index point
  while (sensorVal == HIGH) {
    sensorVal = digitalRead(3);
    forwardstep2();
      delay(50);
  }
   
   DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
   ConfigureDecoder();
   DCC.SetupDecoder( 0x00, 0x00, kDCC_INTERRUPT );
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main loop
//
void loop()
{
    static int addr = 0;
    
        ////////////////////////////////////////////////////////////////
        // Loop DCC library
    DCC.loop();
    
        ////////////////////////////////////////////////////////////////
        // Loop Stepper
    
    stepper2.run();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Assuming all is working ok, after uploading to the Arduino board the turntable deck will rotate to the reference point and stop. While it is doing this you can open the serial monitor as before by clicking the icon in the top right corner of the Arduino IDE.

 

When the turntable stops select Accessory address 200 from your handset and set Normal.

 

The turntable should rotate to a position about 45 degrees from the reference.

Again when the T/T stops select Accessory address 200 and set Thrown.

 

The T/T should now rotate 180 degrees.

 

If you've opened the Serial monitor you should see something like this

 

TTtest.PNG

 

The activate value changing between 0 and 1 as you select between Normal and Thrown positions.

 

So, that's about it as far as testing goes all that needs doing now is to develop the software to suit your individual needs.

 

In the Part 7 next week I'll describe what I have done to get a Seven Road turntable (with software), each position being set by specific values in the code.

 

Those of you with Programming skills may want to take this further and make the positions programmable from the handset. I intend to do this at some point but it wont be this side of Christmas.

 

Ray.

 

 


  • Craftsmanship/Clever x 4








Also tagged with one or more of these keywords: Arduino, stepper, dcc, turntable, peco

Google Ads are only seen by non-members of RMweb - Create an RMweb account and you'll only receive modelling ads.