Maintenance: Project Hub will be unavailable on Wednesday 18 (9AM to 6PM CET) while we deploy critical improvements
Automatic Train Control
Get the trains running on time. Make your schedule. Any Arduino boards and your model railway set.
Components and supplies
1
Arduino Uno Rev3
1
Dual H-Bridge motor drivers L298
1
Jumper wires (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Swing
arduino
1// L298 2#define L298_ENA 5 3#define L298_IN1 6 4#define L298_IN2 7 5 6 7// SCRIPTS VARIABLES 8int counterScheduler; 9unsigned long timerScheduler = 0; 10unsigned long timerLocal = 0; 11byte speedAuto = 0; 12 13 14void setup() { 15 16// Initializing pins 17 pinMode(L298_ENA, OUTPUT); 18 pinMode(L298_IN1, OUTPUT); 19 pinMode(L298_IN2, OUTPUT); 20 21// Set default direction to FORWARD 22 digitalWrite(L298_IN1, HIGH); 23 digitalWrite(L298_IN2, LOW); 24 25} 26 27void loop() { 28 29 // Start Scheduler 30 if (millis() > (timerScheduler + 1000)) { // Tick every 1 sec 31 counterScheduler++; 32 timerScheduler = millis(); 33 } 34 35 // ------------- SCRIPT SWING 36 int brakingDelta = 5; 37 int accelerateDelta = 6; 38 39 // 1 | 0 > Time < 5 sec 40 if (counterScheduler <= 5) { 41 // Start train 42 if (millis() > (timerLocal + 100)) { 43 if (speedAuto < 240) speedAuto = speedAuto + accelerateDelta; 44 else speedAuto = 255; 45 analogWrite(L298_ENA, speedAuto); 46 timerLocal = millis(); 47 } 48 } 49 50 // 2 | 10 sec > Time < 15 sec 51 if ((counterScheduler >= 10) && (counterScheduler <= 15)) { // Stop train after 10 sec 52 // Stop train 53 if (millis() > (timerLocal + 100)) { 54 if (speedAuto > 30) speedAuto = speedAuto - brakingDelta; 55 else speedAuto = 0; 56 analogWrite(L298_ENA, speedAuto); 57 timerLocal = millis(); 58 } 59 } 60 61 // 3 | Change direction 62 if (counterScheduler == 16) { 63 digitalWrite(L298_IN1, LOW); 64 digitalWrite(L298_IN2, HIGH); 65 } 66 67 // 4 | 20 sec > Time < 30 sec 68 if ((counterScheduler >= 20) && (counterScheduler <= 30)) { 69 // Start train 70 if (millis() > (timerLocal + 100)) { 71 if (speedAuto < 240) speedAuto = speedAuto + accelerateDelta; 72 else speedAuto = 255; 73 analogWrite(L298_ENA, speedAuto); 74 timerLocal = millis(); 75 } 76 } 77 78 // 5 | 31 sec > Time < 40 sec 79 if ((counterScheduler >= 31) && (counterScheduler <= 40)) { // Stop train 80 // Stop train 81 if (millis() > (timerLocal + 100)) { 82 if (speedAuto > 30) speedAuto = speedAuto - brakingDelta; 83 else speedAuto = 0; 84 analogWrite(L298_ENA, speedAuto); 85 timerLocal = millis(); 86 } 87 } 88 89 // 6 | Return to Step 1 90 if (counterScheduler > 40) { 91 counterScheduler = 0; 92 digitalWrite(L298_IN1, HIGH); 93 digitalWrite(L298_IN2, LOW); 94 } 95} 96
Swing
arduino
1// L298 2#define L298_ENA 5 3#define L298_IN1 6 4#define L298_IN2 7 5 6 7// SCRIPTS VARIABLES 8int counterScheduler; 9unsigned long timerScheduler = 0; 10unsigned long timerLocal = 0; 11byte speedAuto = 0; 12 13 14void setup() { 15 16// Initializing pins 17 pinMode(L298_ENA, OUTPUT); 18 pinMode(L298_IN1, OUTPUT); 19 pinMode(L298_IN2, OUTPUT); 20 21// Set default direction to FORWARD 22 digitalWrite(L298_IN1, HIGH); 23 digitalWrite(L298_IN2, LOW); 24 25} 26 27void loop() { 28 29 // Start Scheduler 30 if (millis() > (timerScheduler + 1000)) { // Tick every 1 sec 31 counterScheduler++; 32 timerScheduler = millis(); 33 } 34 35 // ------------- SCRIPT SWING 36 int brakingDelta = 5; 37 int accelerateDelta = 6; 38 39 // 1 | 0 > Time < 5 sec 40 if (counterScheduler <= 5) { 41 // Start train 42 if (millis() > (timerLocal + 100)) { 43 if (speedAuto < 240) speedAuto = speedAuto + accelerateDelta; 44 else speedAuto = 255; 45 analogWrite(L298_ENA, speedAuto); 46 timerLocal = millis(); 47 } 48 } 49 50 // 2 | 10 sec > Time < 15 sec 51 if ((counterScheduler >= 10) && (counterScheduler <= 15)) { // Stop train after 10 sec 52 // Stop train 53 if (millis() > (timerLocal + 100)) { 54 if (speedAuto > 30) speedAuto = speedAuto - brakingDelta; 55 else speedAuto = 0; 56 analogWrite(L298_ENA, speedAuto); 57 timerLocal = millis(); 58 } 59 } 60 61 // 3 | Change direction 62 if (counterScheduler == 16) { 63 digitalWrite(L298_IN1, LOW); 64 digitalWrite(L298_IN2, HIGH); 65 } 66 67 // 4 | 20 sec > Time < 30 sec 68 if ((counterScheduler >= 20) && (counterScheduler <= 30)) { 69 // Start train 70 if (millis() > (timerLocal + 100)) { 71 if (speedAuto < 240) speedAuto = speedAuto + accelerateDelta; 72 else speedAuto = 255; 73 analogWrite(L298_ENA, speedAuto); 74 timerLocal = millis(); 75 } 76 } 77 78 // 5 | 31 sec > Time < 40 sec 79 if ((counterScheduler >= 31) && (counterScheduler <= 40)) { // Stop train 80 // Stop train 81 if (millis() > (timerLocal + 100)) { 82 if (speedAuto > 30) speedAuto = speedAuto - brakingDelta; 83 else speedAuto = 0; 84 analogWrite(L298_ENA, speedAuto); 85 timerLocal = millis(); 86 } 87 } 88 89 // 6 | Return to Step 1 90 if (counterScheduler > 40) { 91 counterScheduler = 0; 92 digitalWrite(L298_IN1, HIGH); 93 digitalWrite(L298_IN2, LOW); 94 } 95} 96
Downloadable files
Circuit
Circuit

Connections to Rails
Connections to Rails

Circuit
Circuit

Comments
Only logged in users can leave comments