Simple timed runs will work but inevitably will have "drift" with the train position over time. The problem comes from friction, motor and drive train non-linearities, start and stopping friction not being repeatable, and foward vs. reverse motion not being equivalent. In other words, if you drive a loco/train forward at a specific speed for XX seconds, stop it, reverse it and drive it for XX seconds in reverse, you are likely not to arrive exactly at the same point at which you started!! ...and the difference between original start and subsequent stopping point will grow. There are a number of factors you can try to adjust, but virtually every mechanical design we can attempt will likely, eventually, yield the same observable behavior.
So.... it would be much better to place end detectors to catch the end travel of the shuttle train and use that to reverse the direction. The following sketch does a better job than the original sketch I posted with additional mods along the lines you were looking for: broadcast 0 address, 2 end sensors, optional stopping sensor(s) along the way, estop capability, switch (DPDT relay) for alternate DCC control, ability to set F0-F4, direction LEDs. and speed control knob. You can place the loco on the track in any direction as long as it does not cover an end sensor at the start of the run. I have not tried this sketch out. It does compile and download. Let me now how it goes.
I think this will make for a much more robust control mechanism for any display layout.
/********************
DCC_FTN_Gen_Univ_Shuttle.ino Univeral Shuttle GBunza 2019
This version is used to operate a trolley or shuttle on its own independent track with stops and reversals
Each of the following inputs are typically connected to a position sensor or short block sensor which goes low
When detecting the presence of the loco/trolley/shuttle (like an optical sensor):
The end_sensor1 and end_sensor2 detect end of runs
stop_sensor1 intermediate short stop switch / sensor
If you don�t need the intermediate stops, then don�t connect stop_sensor1 to anything
You can also connect multiple sensors with "open collector outputs" or "opne drain outputs
together (in parallel) for multiple stops all to the stop_sensor1
A switch or pushbutton is connected to D17 to act as an emergency stop ESTOP and may also be omitted
locoAdr= 0 which is a broadcast address to all locos on the track !!
pin 17 can have a switch for relay control to release auto shuttle 0 == NO auto shuttle
An emergency stop switch can be attached to pin15
* The DCC waveform is output on Pin 9, and 10 (inverted)
********************/
// Pro Mini pin map:
// 2 DCC_EN DCC hardware enable to H Bridge
// 3 fwdLEDout LED LOW = ON
// 4 bkwdLEDout LED LOW = ON
// 5 master_DPDT_relay DPDT relay to switch between auto shittle and manual DCC control
// 6 end_sensor1 // sense motion, stop, & reverse
// 7 end_sensor2 // sense motion, stop, & reverse
// 8 p0 / F0
// 9 DCC Library driver
// 10 DCC Library driver inverted
// 11 p1 / F1
// 12 p2 / F2
// 13 p3 / F3
// 14 - stop_sensor1 // intermediate short stop switch / sensor
// 15 - estop_pin 15 // ESTOP Pushbutton
// 16 - A2 analog speed_pin
// 17 - special relay control SWITCH to release auto shuttle 0 == NO auto shuttle
#include < DCCPacket.h>
#include < DCCPacketQueue.h>
#include < DCCPacketScheduler.h>
// for 128 speed steps uncomment the following line; for 28 speed steps comment the following line with "//"
#define speedsteps128
#define DCC_EN 2
#define end_sensor1 6 // sense motion, stop, & reverse
#define end_sensor2 7 // sense motion, stop, & reverse
#define stop_sensor1 14 // intermediate short stop switch / sensor
#define estop_pin 15 // ESTOP Pushbutton
#define speed_pin A2 // Analog pin 2 == D16
DCCPacketScheduler dps;
uint8_t functs0to4 = 0x00;
uint8_t functs5to8 = 0x00;
uint8_t functs9to12 = 0x00;
int8_t last_locoSpeed=0;
uint16_t locoAdr= 0 ; //PK - Addr set to "0" (Zero) for broadcast
unsigned long shuttletime= 150000 ; //PK - default shuttle time 2min30sec in each direction
#define DCC_EN 2
#define fwdLEDout 3
#define bkwdLEDout 4
#define master_DPDT_relay 5
#define p0 8 // F0
#define p1 11 // F1
#define p2 12 // F2
#define p3 13 // F3
#define p7 17 // special relay control SWITCH to release auto shuttle 0 == NO auto shuttle
unsigned long timing_start,timing_end;
int8_t dcc_speed = 1; // loco speed
byte t_cnt;
byte loco_dir = 1; // loco direction - start forward =1
void setup() {
pinMode(end_sensor1, INPUT_PULLUP); // sense forward motion, stop, & go in reverse
pinMode(end_sensor2, INPUT_PULLUP); // sense reverse motion, stop, & go forward
pinMode(stop_sensor1, INPUT_PULLUP); // intermediate stop switch / sensor
pinMode(estop_pin, INPUT_PULLUP); // Optional Pushbutton for ESTOP
pinMode(fwdLEDout, OUTPUT);
pinMode(bkwdLEDout, OUTPUT);
pinMode(master_DPDT_relay, OUTPUT);
pinMode(p0, INPUT_PULLUP);
pinMode(p1, INPUT_PULLUP);
pinMode(p2, INPUT_PULLUP);
pinMode(p3, INPUT_PULLUP);
pinMode(p7, INPUT_PULLUP); //special relay control SWITCH
// to release auto shuttle 0 == NO auto shuttle
loco_dir = 1; // loco direction - start forward =1
dcc_speed = 1; // Speed 0 forward
pinMode(DCC_EN,OUTPUT); // will be kept high, DCC_EN pin
digitalWrite(DCC_EN, 1);
delay(3000);
dps.setup();
delay (5);
#ifdef speedsteps128
dps.setSpeed128(locoAdr,DCC_SHORT_ADDRESS,dcc_speed); // Speed 0 forward
#endif
#ifndef speedsteps128
dps.setSpeed28(locoAdr,DCC_SHORT_ADDRESS,dcc_speed); // Speed 0 forward
#endif
dcc_cmd_rpt (2000 );
digitalWrite(fwdLEDout, 1);
digitalWrite(bkwdLEDout, 1);
}
void loop() {
// Start by checking if we can go
while (digitalRead(estop_pin)== LOW) {
dps.eStop(); // ESTOP Power Cut Off
// Global Broadcast
dcc_cmd_rpt(4000); // wait for 4 seconds
}
while ((digitalRead (p7) & 0x01) == 0) { //special relay control to release auto shuttle
digitalWrite (master_DPDT_relay, 0); //turn on relay to release control
digitalWrite(fwdLEDout, 0); // turn on BOTH direction leds for indication
digitalWrite(bkwdLEDout, 0);
}
// OK to proceed
set_function (0, digitalRead (p0) & 0x01) ;
set_function (1, digitalRead (p1) & 0x01) ;
set_function (2, digitalRead (p2) & 0x01) ;
set_function (3, digitalRead (p3) & 0x01) ;
dcc_speed = (analogRead (speed_pin)/8); // Speed range from 0-127
if (dcc_speed < 127) dcc_speed++; // Positive speed is forward
if (loco_dir == 0) dcc_speed = - dcc_speed;
digitalWrite(fwdLEDout, loco_dir^1);
digitalWrite(bkwdLEDout, loco_dir);
if (((digitalRead(end_sensor1) & 0x01)==0 )
||((digitalRead(end_sensor2) & 0x01)==0 )) { // check if we came to the end, & go in reverse
dcc_speed = 1; // Stop
dcc_cmd_rpt (5000 ); // wait 5 seconds before reversing
loco_dir = loco_dir ^1; // reverse the direction XOR 1
dcc_speed = ( (analogRead (speed_pin)/8)+1 );
if (loco_dir == 0) dcc_speed = - dcc_speed;
dcc_cmd_rpt (500 );
}
if ((digitalRead(stop_sensor1) & 0x01)==0 ) { // intermediate short stop switch / sensor
last_locoSpeed = dcc_speed;
dcc_speed = 1; // stop
dcc_cmd_rpt (6000 ); // wait 6 seconds
dcc_speed = last_locoSpeed ; // resume speed
dcc_cmd_rpt (5000 ); // for at least 8 seconds
}
dcc_cmd_rpt ( 10 );
}
void set_function (byte f_number, byte f_value) {
switch ( f_number ) {
case 0: // Function 0
functs0to4 = (functs0to4 & 0b00011110)
| f_value;
break;
case 1: // Function 1
functs0to4 = (functs0to4 & 0b00011101) | (f_value< < 1);
break;
case 2: // Function 2
functs0to4 = (functs0to4 & 0b00011011) | (f_value< < 2);
break;
case 3: // Function 3
functs0to4 = (functs0to4 & 0b00010111) | (f_value< < 3);
break;
case 4: // Function 4
functs0to4 = (functs0to4 & 0b00001111) | (f_value< < 4);
break;
case 5: // Function 5
functs5to8 = (functs5to8 & 0b00011110) | (f_value);
break;
case 6: // Function 6
functs5to8 = (functs5to8 & 0b00011101) | (f_value< < 1);
break;
case 7: // Function 7
functs5to8 = (functs5to8 & 0b00011011) | (f_value< < 2);
break;
case 8: // Function 8
functs5to8 = (functs5to8 & 0b00010111) | (f_value< < 3);
break;
case 9: // Function 9
functs9to12 = (functs9to12 & 0b00011110) | (f_value);
break;
case 10: // Function 10
functs9to12 = (functs9to12 & 0b00011101) | (f_value< < 1);
break;
case 11: // Function 11
functs9to12 = (functs9to12 & 0b00011011) | (f_value< < 2);
break;
case 12: // Function 12
functs9to12 = (functs9to12 & 0b00010111) | (f_value< < 3);
break;
}
}
void dcc_cmd_rpt (unsigned long loop_delay) {
#ifdef speedsteps128
dps.setSpeed128(locoAdr,DCC_SHORT_ADDRESS,dcc_speed); // Set Speed
#endif
#ifndef speedsteps128
dps.setSpeed28(locoAdr,DCC_SHORT_ADDRESS,dcc_speed); // Set Speed
#endif
dps.setFunctions(locoAdr, DCC_SHORT_ADDRESS, functs0to4,functs5to8, functs9to12);
wait_dcc(loop_delay);
}
void wait_dcc (unsigned long wait_time) {
unsigned long tempul;
tempul = wait_time;
timing_start = millis();
timing_end = timing_start+ tempul;
while (timing_start< timing_end) {
dps.update();
timing_start = millis();
}
}