Leadie69
Published © GPL3+

Model Railroad Random Selector

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

BeginnerShowcase (no instructions)3,530
Model Railroad Random Selector

Things used in this project

Hardware components

Arduino UNO
Arduino 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

Story

Read more

Schematics

Fritzing Circuit

Code

Code for random selector

Arduino
//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);
  }
  }
  } 
   

Credits

Leadie69
1 project • 3 followers
Contact

Comments

Please log in or sign up to comment.