Project showcase
Model Railroad Random Selector

Model Railroad Random Selector © GPL3+

Randomly select a destination for one wagon on a Timesaver layout or randomly select order of wagons for an Inglenook layout.

  • 1,521 views
  • 0 comments
  • 6 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
I2C Port Expander Module for LCD
×1
20x4 LCD display
×1
1 pole 12 position rotary switch
×1
Momentary Push Button Switch
×1

About this project

I have several model railway layouts that involve either moving a wagon to a particular spot or assembling a train with wagons in a specific order. In the past I have used a system based on shuffling cards but it always seemed like a fiddly way to do things - it was easy to drop cards when shuffling and there always need to be somewhere to lay them out. This is what I created to solve these problems.

Basically, there are 4 different layouts that need to work with this and they are selected via the rotary switch. Once the layout has been selected it is just a press of the momentary button to show the random destination or order on the screen.

Code

Code for random selectorArduino
//random destination and car selector

// include the library code:
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>

LiquidCrystal_PCF8574 lcd(0x27);  // set the LCD address to 0x27 for a 16 chars and 2 line display



//set random number value to atart
int randnumber = 0;

void setup() {

  Wire.begin();
  Wire.beginTransmission(0x27);
  //start random generator
  randomSeed(analogRead(0));
  //set digital pins 2-5 to pullup input
  pinMode(2,INPUT_PULLUP);
  pinMode(3,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);
   pinMode(A1,INPUT_PULLUP);
  lcd.setBacklight(HIGH);
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  }

void loop() {
  //read switch state
  int selectA = digitalRead(2);
  int selectB = digitalRead(3);
  int selectC = digitalRead(4);
  int selectD = digitalRead(5);
  
  if (selectA==LOW){
  //run garland
    //print starting info on lcd
  
    lcd.setCursor(0,0);
    lcd.print("Garland             ");
    lcd.setCursor(0,1);
    lcd.print("                    ");
    lcd.setCursor(0,2);
    lcd.print("Move car to         ");
    lcd.setCursor(0, 3);
    
    
    //read button state
    int button = analogRead(A1);
    if(button>800){
    delay(100);
    }else{
    
    //find new random number and check that it is different to previous random number
    int newrandnumber = random(1,5);
    while(newrandnumber==randnumber){
    newrandnumber = random(1,5);
    }
    
    //replace old random number with new random number and print appropriate destination on lcd
    randnumber=newrandnumber;
    if(randnumber==1){
    lcd.print("workshop            ");
    }else if(randnumber==2){
    lcd.print("warehouse           ");
    }else if(randnumber==3){
    lcd.print("freighthouse        ");
    }else if(randnumber==4){
    lcd.print("team track          ");
    }
    delay(1000);
  }

  }else if(selectB==LOW){
  //run n scale timesaver
    //print starting info on lcd
 
    lcd.setCursor(0,0);
    lcd.print("N Scale Timesaver   ");
    lcd.setCursor(0,1);
    lcd.print("                    ");
    lcd.setCursor(0,2);
    lcd.print("Move car to         ");
    lcd.setCursor(0, 3);
    
    //read button state
    int button = analogRead(A1);
    if(button>800){
    delay(100);
    }else{
  
    //find new random number and check that it is different to previous random number
    int newrandnumber = random(1,6);
    while(newrandnumber==randnumber){
    newrandnumber = random(1,6);
    }

    //replace old random number with new random number and print appropriate destination on lcd
    randnumber=newrandnumber;
    if (randnumber==1){
    lcd.print("dockside            ");
    }else if (randnumber==2){
    lcd.print("depot               ");
    }else if (randnumber==3){
    lcd.print("jetty               ");
    }else if (randnumber==4){
    lcd.print("ramp                ");
    }else if (randnumber==5){
    lcd.print("factory             ");
    }
    delay(1000);
  }

  }else if(selectC==LOW){
  //run steen manufacturing
    //print starting info on lcd
    lcd.setCursor(0,0);
    lcd.print("Steen Manufacturing ");
 lcd.setCursor(0,1);
    lcd.print("                    ");
    lcd.setCursor(0,3);
    lcd.print("                    ");
    //read button state
   int button = analogRead(A1);
    if(button>800){
    delay(100);
    } else {
  
    //find random numbers and check that they are all different
    int numberOne = random(1,6);
    int numberTwo = random(1,6);
    while(numberTwo==numberOne){
    numberTwo = random(1,6);
    }
    int numberThree = random(1,6);
    while((numberThree==numberOne)||(numberThree==numberTwo)){
    numberThree = random(1,6);
    }

    //print first car selection on lcd
    lcd.setCursor(0, 2);
    lcd.print("                    ");
    lcd.setCursor(0, 2);
    if (numberOne==1){
      lcd.print("van, ");
    }else if (numberOne==2){
      lcd.print("tank, ");
    }else if (numberOne==3){
      lcd.print("hopper, ");
    }else if (numberOne==4){
      lcd.print("flat, ");
    }else if (numberOne==5){
      lcd.print("open, ");
    }
    //print second car selecion on lcd
    if (numberTwo==1){
      lcd.print("van, ");
    }else if (numberTwo==2){
      lcd.print("tank, ");
    }else if (numberTwo==3){
      lcd.print("hopper, ");
    }else if (numberTwo==4){
      lcd.print("flat, ");
    }else if (numberTwo==5){
      lcd.print("open, ");
    }
    //print third car selected on lcd
   
    if (numberThree==1){
      lcd.print("van");
    }else if (numberThree==2){
      lcd.print("tank");
    }else if (numberThree==3){
      lcd.print("hopper");
    }else if (numberThree==4){
      lcd.print("flat");
    }else if (numberThree==5){
      lcd.print("open");
    }
    delay(1000);
  }
 
  }else if(selectD==LOW){
  //run OO inglenook
    //print starting info on lcd
    lcd.setCursor(0,0);
    lcd.print("OO Inglenook       ");
 
    //read button state
    int button = analogRead(A1);
    if(button>800){
    delay(100);
    } else {
  
    //find random numbers and check that they are all different
    int numberOne = random(1,9);
    int numberTwo = random(1,9);
    while(numberTwo==numberOne){
    numberTwo = random(1,9);
    }
    int numberThree = random(1,9);
    while((numberThree==numberOne)||(numberThree==numberTwo)){
    numberThree = random(1,9);
    }
    int numberFour = random(1,9);
    while((numberFour==numberOne)||(numberFour==numberTwo)||(numberFour==numberThree)){
    numberFour = random(1,9);
    }
    int numberFive = random(1,9);
    while((numberFive==numberOne)||(numberFive==numberTwo)||(numberFive==numberThree)||(numberFive==numberFour)){
    numberFive = random(1,9);
    }

    //print first car selection on lcd
    lcd.setCursor(0, 1);
    if (numberOne==1){
      lcd.print("van      |");
    }else if (numberOne==2){
      lcd.print("tanker   |");
    }else if (numberOne==3){
      lcd.print("hopper   |");
    }else if (numberOne==4){
      lcd.print("flatcar  |");
    }else if (numberOne==5){
      lcd.print("5 plank  |");
    }else if (numberOne==6){
      lcd.print("chinaclay|");
    }else if (numberOne==7){
      lcd.print("salt van |");
    }else if (numberOne==8){
      lcd.print("livestock|");
    }
    //print second car selection on lcd
    if (numberTwo==1){
      lcd.print(" van      ");
    }else if (numberTwo==2){
      lcd.print(" tanker   ");
    }else if (numberTwo==3){
      lcd.print(" hopper   ");
    }else if (numberTwo==4){
      lcd.print(" flatcar  ");
    }else if (numberTwo==5){
      lcd.print(" 5 plank  ");
    }else if (numberTwo==6){
      lcd.print(" chinaclay");
    }else if (numberTwo==7){
      lcd.print(" salt van ");
    }else if (numberTwo==8){
      lcd.print(" livestock");
    }
    //print third car selection on lcd
    lcd.setCursor(0,2);
    if (numberThree==1){
      lcd.print("van      |");
    }else if (numberThree==2){
      lcd.print("tanker   |");
    }else if (numberThree==3){
      lcd.print("hopper   |");
    }else if (numberThree==4){
      lcd.print("flatcar  |");
    }else if (numberThree==5){
      lcd.print("5 plank  |");
    }else if (numberThree==6){
      lcd.print("chinaclay|");
    }else if (numberThree==7){
      lcd.print("salt van |");
    }else if (numberThree==8){
      lcd.print("livestock|");  
    }
    //print forth car selection on lcd
    if (numberFour==1){
      lcd.print(" van      ");
    }else if (numberFour==2){
      lcd.print(" tanker   ");
    }else if (numberFour==3){
      lcd.print(" hopper   ");
    }else if (numberFour==4){
      lcd.print(" flatcar  ");
    }else if (numberFour==5){
      lcd.print(" 5 plank  ");
    }else if (numberFour==6){
      lcd.print(" chinaclay");
    }else if (numberFour==7){
      lcd.print(" salt van ");
    }else if (numberFour==8){
      lcd.print(" livestock");
    }
    //print fifth car selection on lcd
    lcd.setCursor(0,3);
    if (numberFive==1){
      lcd.print("van                 ");
    }else if (numberFive==2){
      lcd.print("tanker              ");
    }else if (numberFive==3){
      lcd.print("hopper              ");
    }else if (numberFive==4){
      lcd.print("flatcar             ");
    }else if (numberFive==5){
      lcd.print("5 plank             ");
    }else if (numberFive==6){
      lcd.print("china clay          ");
    }else if (numberFive==7){
      lcd.print("salt van            ");
    }else if (numberFive==8){
      lcd.print("livestock           ");  
    }
    
    delay(1000);
  }
  }
  } 
   

Schematics

Fritzing Circuit
random_selector_fritzing_fqzn765urj.fzz

Comments

Similar projects you might like

Model Railroad Grade Crossing Controller

Project in progress by KevinMcArthur

  • 2,069 views
  • 0 comments
  • 15 respects

Simple Automated Point to Point Model Railroad

Project tutorial by Kushagra Keshari

  • 592 views
  • 0 comments
  • 4 respects

Arduino railroad

Project showcase by Sean Dutton

  • 7,513 views
  • 3 comments
  • 16 respects

Railroad Crossing Lights

Project showcase by eldo85

  • 3,109 views
  • 4 comments
  • 7 respects

Model Train Control

Project in progress by Masters

  • 7,334 views
  • 3 comments
  • 14 respects

Lottery Winner: Non-Repeating Random Numbers for Arduino

Project in progress by LAGSILVA

  • 5,709 views
  • 20 comments
  • 28 respects
Add projectSign up / Login