Dispatcher

by Charles R. Hentz

In August of 2022 Harry Black got an inspiration in the shower. Why not have an automatic dispatcher set all the turnouts on the O gauge layout? This would allow more action as it took a minute to set the toggle switches for the correct path for each train. Now when a throttle is removed from its holder near its parked train, a micro switch is opened.  This will be read by the Arduino Mega microprocessor. That throttle’s path is then set by the L298 Drivers using a multidimensional array in the code.  Only one throttle (one train) will run at a time on each line. After one trip around the layout, each train will be returned to its assigned siding and the throttle replaced into its holder closing its micro switch. The system will be in service full time. If for any reason manual operation is needed, all five toggle switches on the hidden control panel can easily be thrown to manual control.  This should allow train operation as fast as you can pick up a throttle and turn the speed dial.

Code

//Dispatcher:

//written by C. R HENTZ Aug. 24, 2022:

// L298N Breakout Motor Controller and Mega R3 BOARD 2560 are used:

int Position = 1 ;//0 = closed or 1 = thrown:

void SetTurnout();//function to set each turnout to correct position:

int ThrottleSearch();//function to search for throttle out:

int IsThrottleBack();//function to watch for throttle return:

int i = 1;

int k = 0;

int Throttle = 0 ;//throttle 1A, 2B, 3C, 4D or 0= none out:

int NewThrottle = 0;//to determine return of throttle:

int ThrottlePin[] = {20, 22, 24, 26, 28}; //

int ThrottleState = 1;//throttle out=HIGH or 1:

int PositionNeeded[5][6] = {

  {0, 0, 0, 0, 0, 0},

  {0, 1, 0, 0, 0, 0},

  {0, 0, 1, 1, 0, 0},

  {0, 0, 1, 1, 1, 0},

  {0, 0, 1, 1, 1, 1}

};//1st column ignored, 2nd=1st turnout 10/11:

//0=closed and 1=thrown:

//row 0 is all throttles in:

//row 1 is throttle 1 out:

void setup() {

  Serial.begin(9600);//inialize serial communication:

  pinMode(20, INPUT);//dummy Throttle A HIGH= out:

  pinMode(22, INPUT);//Throttle A HIGH= out:

  pinMode(24, INPUT);//Throttle B HIGH= out:

  pinMode(26, INPUT);//Throttle C HIGH= out:

  pinMode(28, INPUT);//Throttle D HIGH= out:

  pinMode(31, OUTPUT); //IN1 TO 10/11 LOW CLOSED:

  pinMode(32, OUTPUT); //IN2 TO 10/11 HIGH CLOSED:

  pinMode(33, OUTPUT); //IN3 TO 14 LOW CLOSED:

  pinMode(34, OUTPUT); //IN4 TO 14 HIGH CLOSED:

  pinMode(35, OUTPUT); //IN1 TO 17 LOW CLOSED:

  pinMode(36, OUTPUT); //IN2 TO 17 HIGH CLOSED:

  pinMode(37, OUTPUT); //IN3 TO 19/16 LOW CLOSED:

  pinMode(38, OUTPUT); //IN4 TO 19/16 HIGH CLOSED:

  pinMode(39, OUTPUT); //IN1 TO 30/31 LOW CLOSED:

  pinMode(40, OUTPUT); //IN2 TO 30/31 HIGH CLOSED:

  pinMode(43, OUTPUT);//LED AUTO ON GREEN:

  pinMode(45, OUTPUT);//LED AUTO OFF OR EXTRA THROTTLE RED:

  digitalWrite(45, LOW); //GREEN ON LED:

  digitalWrite(43, HIGH); //GREEN ON LED:

}

// the loop routine runs over and over again forever

void loop() {

  Serial.println(“Dispatcher220824”);

  Throttle = 0;

  SetTurnout();//fn to set each turnout to correct position:

  ThrottleSearch();//check for throttle out:

  while (Throttle == 0) {

    ThrottleSearch();//check for throttle out:

  }

  digitalWrite(45, HIGH);//status LED to red:

  digitalWrite(43, LOW);//status LED to red:

  SetTurnout();//fn to set each turnout to correct position:

  IsThrottleBack();//watch for throttle return:

  Throttle = NewThrottle;

  Serial.println(“End of Loop”);

}

//Function to set Turnout to correct position:

void SetTurnout() {

  k = 31;//first L298 driver 1N pin, last is 40:

  Serial.println(“SetTurnout Function”);

  Serial.print(“Throttle= “);

  Serial.println(Throttle);

  for (i = 1; i < 6; i++) {

    digitalWrite(k, PositionNeeded[Throttle][i]);

    digitalWrite(k + 1, !PositionNeeded[Throttle][i]);

    k = k + 2;//! gives the opposite value:

    delay(500);//reduce strain on power supply:

  }

}

//Function to check for throttle out:

int ThrottleSearch() {

  Throttle = 0;

  Serial.println(“ThrottleSearch Function”);

  for (i = 1; i < 5; i++) {

    ThrottleState = digitalRead(ThrottlePin[i]);

    Serial.println(ThrottleState);

    if (ThrottleState == 1) {

      Throttle = i;

    }

  }

  return Throttle;

}

int IsThrottleBack() { //watch for throttle return:

  Serial.println(“IsThrottleBack Function”);

  NewThrottle = Throttle;

  while (ThrottleState = HIGH) {

    ThrottleState = digitalRead(ThrottlePin[Throttle]);

    Serial.print(“ThrottleState = “);

    Serial.println(ThrottleState);

    if (ThrottleState == LOW) {

      NewThrottle = 0;

      Serial.println(“IsThrottleBack Function is finished”);

      digitalWrite(45, LOW); //GREEN ON LED://status LED to green:

      digitalWrite(43, HIGH); //GREEN ON LED://status LED to green:

      return NewThrottle;

    }

  }

}