Steve Massikker
Published © Apache-2.0

Automatic Train Control

Get the trains running on time. Make your schedule. Any Arduino boards and your model railway set.

BeginnerFull instructions provided12,103
Automatic Train Control

Things used in this project

Hardware components

Jumper wires (generic)
Jumper wires (generic)
×1
Arduino UNO
Arduino UNO
You can use ANY Arduino board (UNO, NANO, MEGA and etc)
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
I recommend using a regular L298 motor driver module, as in the picture of the circuit
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Connections to Rails

Circuit

Code

Swing

Arduino
// L298 
#define L298_ENA 5
#define L298_IN1 6
#define L298_IN2 7


// SCRIPTS VARIABLES
int counterScheduler;
unsigned long timerScheduler = 0;
unsigned long timerLocal = 0;
byte speedAuto = 0;


void setup() {

// Initializing pins
  pinMode(L298_ENA, OUTPUT);
  pinMode(L298_IN1, OUTPUT);
  pinMode(L298_IN2, OUTPUT);

// Set default direction to FORWARD
  digitalWrite(L298_IN1, HIGH);
  digitalWrite(L298_IN2, LOW); 

}

void loop() {

  	// Start Scheduler
    if (millis() > (timerScheduler + 1000)) {  // Tick every 1 sec
      counterScheduler++; 
      timerScheduler = millis();
    }  
    
    // ------------- SCRIPT SWING
    int brakingDelta = 5;
    int accelerateDelta = 6;

    // 1  | 0 > Time < 5 sec
    if (counterScheduler <= 5) {  
        // Start train
        if (millis() > (timerLocal + 100)) {
          if (speedAuto < 240) speedAuto = speedAuto + accelerateDelta;
          else speedAuto = 255;
          analogWrite(L298_ENA, speedAuto); 
          timerLocal = millis();
        }   
    }       
    
    // 2  | 10 sec > Time < 15 sec
    if ((counterScheduler >= 10) && (counterScheduler <= 15)) {  // Stop train after 10 sec
        // Stop train
        if (millis() > (timerLocal + 100)) {
          if (speedAuto > 30) speedAuto = speedAuto - brakingDelta;
          else speedAuto = 0;
          analogWrite(L298_ENA, speedAuto); 
          timerLocal = millis();
        } 
    }  
    
    // 3  | Change direction
    if (counterScheduler == 16) {  
        digitalWrite(L298_IN1, LOW);
        digitalWrite(L298_IN2, HIGH); 
    }   
    
    // 4  | 20 sec > Time < 30 sec
    if ((counterScheduler >= 20) && (counterScheduler <= 30)) {  
        // Start train
        if (millis() > (timerLocal + 100)) {
          if (speedAuto < 240) speedAuto = speedAuto + accelerateDelta;
          else speedAuto = 255;
          analogWrite(L298_ENA, speedAuto); 
          timerLocal = millis();
        } 
    }       
    
    // 5  | 31 sec > Time < 40 sec
    if ((counterScheduler >= 31) && (counterScheduler <= 40)) {  // Stop train
        // Stop train
        if (millis() > (timerLocal + 100)) {
          if (speedAuto > 30) speedAuto = speedAuto - brakingDelta;
          else speedAuto = 0;
          analogWrite(L298_ENA, speedAuto); 
          timerLocal = millis();
        } 
    }    
    
    // 6  | Return to Step 1
    if (counterScheduler > 40) {
        counterScheduler = 0;   
        digitalWrite(L298_IN1, HIGH);
        digitalWrite(L298_IN2, LOW); 
  	}
}

Credits

Steve Massikker

Steve Massikker

6 projects • 166 followers
I'm Steve Massikker, a graphic and visual designer and former engineer of civil aviation.

Comments