Kushagra Keshari
Published © CC BY-NC-ND

Simple Automated Point to Point Model Railroad Running Tw...

An Arduino-based point-to-point model railroad automation project.

IntermediateFull instructions provided2 hours1,912
Simple Automated Point to Point Model Railroad Running Tw...

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Adafruit Motor Shield V2
×1
IR proximity sensor
×1
Adafruit 12V 5A switching power supply
×1
Jumper wires (generic)
Jumper wires (generic)
×4
Male/Female Jumper Wires
Male/Female Jumper Wires
×9

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Automated_point_to_point_model_railroad_runjing_two_trains.ino

Arduino
No preview (download only).

Automated_point_to_point_model_railroad_runjing_two_trains.ino

Arduino
/*
 * Arduino code for automated multi point model railorad for running two trains in an 
 * automated sequence.
 * 
 * Made by Tech Build: https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1
 * 
 * Find more such projects here: https://www.instructables.com/member/KushagraK7/
 * 
 * Feel free to tinker around with the code, add some features or change the existing ones
 * to suit your requirements.
 * 
 * Happy railroading!
 */
#include<Wire.h>
#include<Adafruit_MotorShield.h>//Make sure this library is installed in your IDE.

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *loco = AFMS.getMotor(1);//Track power connected to the terminal 'M1'.
Adafruit_DCMotor *turnoutA = AFMS.getMotor(3);//Turnout at station 'A' connected to the terminal 'M3'.
Adafruit_DCMotor *turnoutB = AFMS.getMotor(4);//Turnout at mainline branching two tracks to stations 'B' and 'C'.

int s;              //int variable for storing the speed and direction of the train, speed ranges
                    //from -255 to 255.
int sensorA = A0;   //The sensor installed in the track approaching station A.
int sensorB = A1;   //The sensor installed in the track approaching station B.
int sensorC = A2;   //The sensor installed in the track approaching station C.
int MinSpeedA = 20; //The low speed for train A, you might need to try with different values 
                    //before settling for the optimum one.
int MinSpeedB = 55; //The low speed for train B.
int MidSpeed = 55;  //The medium speed for both the trains is kept same.
int MaxSpeedA = 80; //The maximum speed for train A when running on the mainline.
int MaxSpeedB = 100;//The maximum speed for train B when running on the mainline.

/*
 * Custom function made to adjust the speed and direction of the train based on the value and
 * sign (+/-) of variable i, between -255 and 255.
 */
void loco_go(int i){
 if(i>=1&&i<=255){
  loco->setSpeed(i);
  loco->run(FORWARD);
 }
 if(i<=-1&&i>=-255){
  loco->setSpeed(-i);
  loco->run(BACKWARD);
 }
 if(i==0){
  loco->setSpeed(i);
  loco->run(RELEASE);
 }
}

//Custom functions made for turnout switch control.

void turnoutA_side(){
turnoutA->setSpeed(255);
turnoutA->run(FORWARD);
delay(100);
turnoutA->setSpeed(0);
turnoutA->run(RELEASE);
}

void turnoutA_straight(){
turnoutA->setSpeed(255);
turnoutA->run(BACKWARD);
delay(100);
turnoutA->setSpeed(0);
turnoutA->run(RELEASE);
}

void turnoutB_side(){
turnoutB->setSpeed(255);
turnoutB->run(FORWARD);
delay(100);
turnoutB->setSpeed(0);
turnoutB->run(RELEASE);
}

void turnoutB_straight(){
turnoutB->setSpeed(255);
turnoutB->run(BACKWARD);
delay(100);
turnoutB->setSpeed(0);
turnoutB->run(RELEASE);
}
void setup() {
  // put your setup code here, to run once:

  AFMS.begin(200);//Starting the motor shield and defining the frequency of the PWM signals
                  //to be sent to the motors, you can try with different values.
                  //Here, for example, it has been set to 200Hz.

  //Declaring the pins connected to the sensors to be digital input pins.
  pinMode(sensorA, INPUT);
  pinMode(sensorB, INPUT);
  pinMode(sensorC, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:


  turnoutA_side();

  for(s=0; s!=MinSpeedA; s++){
   loco_go(s);
   delay(250);
  }

  while(digitalRead(sensorA)!=HIGH);
  
  turnoutB_straight();

  for(s=s; s!=MidSpeed; s++){
    loco_go(s);
    delay(250);
  }

  delay(2000);

  for(s=s; s!=MaxSpeedA; s++){
    loco_go(s);
    delay(250);
  }

  while(digitalRead(sensorC)!=HIGH);

  for(s=s; s!=MidSpeed; s--){
    loco_go(s);
    delay(125);
  }

  for(s=s; s!=MinSpeedA; s--){
    loco_go(s);
    delay(125);
  }

  delay(4000);

  for(s=s; s!=0; s--){
    loco_go(s);
    delay(250);
  }

  turnoutB_side();
  turnoutA_straight();

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

  for(s=s; s!=MinSpeedB; s++){
    loco_go(s);
    delay(125);
  }

  while(digitalRead(sensorA)!=HIGH);

  for(s=s; s!=MidSpeed; s++){
    loco_go(s);
    delay(125);
  }

  delay(2000);
 
  for(s=s; s!=MaxSpeedB; s++){
    loco_go(s);
    delay(250);
  }

  while(digitalRead(sensorB)!=HIGH);

  for(s=s; s!=MidSpeed; s--){
    loco_go(s);
    delay(125);
  }

  for(s=s; s!=MinSpeedB; s--){
    loco_go(s);
    delay(250);
  }

  delay(4000);

  for(s=s; s!=30; s--){
    loco_go(s);
    delay(125);
  }

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

  turnoutB_straight();

  for(s=s; s!=-MinSpeedA; s--){
    loco_go(s);
    delay(250);
  }

  while(digitalRead(sensorC)!=HIGH);
  
  turnoutA_side();

  for(s=s; s!=-MidSpeed; s--){
    loco_go(s);
    delay(250);
  }

  delay(2000);

  for(s=s; s!=-MaxSpeedA; s--){
    loco_go(s);
    delay(250);
  }

  while(digitalRead(sensorA)!=HIGH);

   for(s=s; s!=-MidSpeed; s++){
    loco_go(s);
    delay(125);
   }

   delay(2000);

   for(s=s; s!=-MinSpeedA; s++){
    loco_go(s);
    delay(250);
   }

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

  turnoutB_side();
  turnoutA_straight();

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

    for(s=s; s!=-MinSpeedB; s--){
    loco_go(s);
    delay(125);
  }

  while(digitalRead(sensorB)!=HIGH);

  for(s=s; s!=-MidSpeed; s--){
    loco_go(s);
    delay(250);
  }

  delay(2000);

  for(s=s; s!=-MaxSpeedB; s--){
    loco_go(s);
    delay(250);
  }

  while(digitalRead(sensorA)!=HIGH);

   for(s=s; s!=-MidSpeed; s++){
    loco_go(s);
    delay(250);
   }

   delay(1000);

   for(s=s; s!=-MinSpeedB; s++){
    loco_go(s);
    delay(250);
   }

   delay(4000);
   
   for(s=s; s!=0; s++){
      loco_go(s);
      delay(125);
    }  

}

Credits

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

Comments

Please log in or sign up to comment.