Kushagra Keshari
Published © CC BY-NC-ND

Model Railway Layout with Automated Passing Siding (V2.0)

An Arduino-controlled model railroad layout with a passing siding where the locomotive picks up and drops off rolling stock autonomously.

IntermediateFull instructions provided2 hours3,543
Model Railway Layout with Automated Passing Siding (V2.0)

Things used in this project

Story

Read more

Code

Model_train_layout_with_automated_passing_siding_V2.0.ino

Arduino
/*
 * Arduino program to control a basic model train layout with a passing siding with the help of optical feedback mechanism.
 * 
 * Made by Tech Build: https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1
 * 
 * Feel free to tinker with the code and customize it for your own layout. :)
 */

int sensor1 = A0;//Pin connected to the first sensor, this sensor will be installed before 
                   //the passing siding, taking the direction of the train into account.

int sensor2 = A1;//Pin connected to the second sensore, this sensor will be installed after
                   //passing siding, taking the direction of the train into account.

int s = 0; //Integer to store the locomotive's speed at a scale from 0 to 255.

int motor_go(int i){//Custom function made to control the speed and direction of the locomotive based on the ineger value and sign(+ve or-ve).
 if(i>=1&&i<=255){
  digitalWrite(9,LOW);
  digitalWrite(8,HIGH);
  analogWrite(10,i);
 }
 if(i<=-1&&i>=-255){
  digitalWrite(8,LOW);
  digitalWrite(9,HIGH);
  analogWrite(10,-i);
 }
 if(i==0){
  digitalWrite(9,LOW);
  digitalWrite(8,LOW);
  analogWrite(10,i);
 }
}

void switch_to_pass(){//Custom fuction made to switch the turnouts to the passing siding.
  digitalWrite(11,LOW);
  digitalWrite(12,HIGH);  
  delay(200);
  digitalWrite(12,LOW);
}

void switch_to_main(){//Custom function made to switch the turnouts to the mainline.
  digitalWrite(12,LOW);
  digitalWrite(11,HIGH);    
  delay(200);
  digitalWrite(11,LOW);

}
void setup() {
  pinMode(sensor1,INPUT);
  pinMode(sensor2,INPUT);
  
  pinMode(8,OUTPUT);//Pins 8 and 9 are for direction control of the locomotive.
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);//Pin 10 is a PWM pin on the Arduino UNO and is connected to the 'ENB' pin of the motor driver.
  pinMode(11,OUTPUT);//Pns 11 and 12 are for turnout control.
  pinMode(12,OUTPUT);
  
}

void loop() {
     
     switch_to_main();//Switching the turnouts to the minline.

     for(s=0;s<=30;s++){//Starting the locomotive.
      motor_go(s);
      delay(60);
     }

     delay(2000);

     for(s=s;s<=90;s++){
      motor_go(s);
      delay(125);
     }
    
     while(digitalRead(A1)!=HIGH);//Wait till the locomotive reaches the second sensor.

     for(s=s;s!=30;s--){//Stop the locomotive.
      motor_go(s);
      delay(125);
     }

     delay(2000);

     for(s=s;s!=0;s--){
      motor_go(s);
      delay(60);
     }

     delay(1000);

     switch_to_pass();
      
     for(s=s;s!=-30;s--){
      motor_go(s);
      delay(60);
     }

     delay(2000);

     for(s=s;s!=-90;s--){
      motor_go(s);
      delay(125);
     }

     while(digitalRead(A0)!=HIGH);//Wait till the train reaches the first sensor.

     for(s=s;s!=-30;s++){
      motor_go(s);
      delay(60);
     }

     delay(2000);

     for(s=s;s!=0;s++){
      motor_go(s);
      delay(60);
     }

     switch_to_main();
     //Switching the turnouts to the mainline.

     for(s=s;s!=30;s++){
      motor_go(s);
      delay(60);
     }

     delay(2000);

     for(s=s;s!=90;s++){
      motor_go(s);
      delay(125);
     }

     while(digitalRead(A1)!=HIGH);

     for(s=s;s!=110;s++){
      motor_go(s);
      delay(250);
     }

     while(digitalRead(A1)!=HIGH);

     for(s=s;s!=180;s++){
      motor_go(s);
      delay(250);
     }

     while(digitalRead(A1)!=HIGH);//Two while statements for checking train's position mean
     while(digitalRead(A0)!=HIGH);//the train will cover one loop.

     while(digitalRead(A1)!=HIGH);//The above set of statements is repeated again to make the
     while(digitalRead(A0)!=HIGH);//train cover another loop.

     for(s=s;s!=110;s--){
      motor_go(s);
      delay(250);
     }     

     while(digitalRead(A0)!=HIGH);

     switch_to_pass();


     for(s=s;s!=90;s--){
      motor_go(s);
      delay(250);
     }

     delay(5000);

     for(s=s;s!=70;s--){
      motor_go(s);
      delay(250);
     }

     while(digitalRead(A1)!=HIGH);


     for(s=s;s!=110;s++){
      motor_go(s);
      delay(250);
     }

     while(digitalRead(A0)!=HIGH);

     switch_to_main();

     delay(2000);

     for(s=s;s!=80;s--){
      motor_go(s);
      delay(250);
     }

     delay(6000);
     
     for(s=s;s!=30;s--){
      motor_go(s);
      delay(250);
     }

     delay(2000);

     for(s=s;s!=0;s--){
      motor_go(s);
      delay(60);
     }

     delay(5000);
          
}

Credits

Kushagra Keshari
19 projects • 70 followers
A casual electronics and a model railway hobbyist.
Contact

Comments

Please log in or sign up to comment.