Arduino Train Racing

Introduction: Arduino Train Racing

About: Hello. Main name is Steve. I'm designer and former engineer from Moscow.

Owners of model railways can prepare a fascinating surprise for their family and friends. You can use any railway with metal rails. A lot of trains, a lot of fun, so this system is designed to control two players, you need collect two circles and have at least two locomotives. A feature of the system is the ability to intervene in the control the other player.

You can also use this control system for racing car games like Electric Loop Road Racing.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Stuff Required

Requires UNO or NANO Arduino board, two Bluetooth module HC-06 (05) and motor-driver L298 (or shields) to operate. You can use the power supply from your railway kit or any 9-12 V power supply. Conveniently assemble the circuit using Female-Female wires for NANO (Male-Female for UNO) and the power plug-screw terminals.

Step 2: Upload Sketch to Your Arduino

#include <SoftwareSerial.h>

// SOFTWARE SERIAL
SoftwareSerial Bluetooth(12, 13); // RX, TX

// L298 
#define L298_ENA 5
#define L298_IN1 6
#define L298_IN2 7
#define L298_IN3 8
#define L298_IN4 9 
#define L298_ENB 10

// SERIAL EVENT
String inputString = "";

// FLAGS
bool stringComplete = false;

// CONTROL TRAINS
byte speedArrayA [] = {20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,255};
byte speedArrayB [] = {20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,255};
byte speedTrainA = 0, speedTrainB = 0;


void setup() {

// Initializing COMM
  Serial.begin(9600);
  Bluetooth.begin(9600);
  inputString.reserve(4);

// Initializing Motor-Drivers
  pinMode(L298_ENA, OUTPUT);
  pinMode(L298_IN1, OUTPUT);
  pinMode(L298_IN2, OUTPUT);
  pinMode(L298_IN3, OUTPUT);
  pinMode(L298_IN4, OUTPUT);
  pinMode(L298_ENB, OUTPUT);

// Set default direction to FORWARD
  // Driver A
  digitalWrite(L298_IN1, HIGH);
  digitalWrite(L298_IN2, LOW); 
  // Driver B
  digitalWrite(L298_IN3, HIGH);
  digitalWrite(L298_IN4, LOW); 

}

void loop() {

// ---- START PARSING AND RESEND INCOMING APP COMMANDS
  if (stringComplete) {

    // Player A
    if (inputString.charAt(0) =='a') controlPlayerA();
    // Player B
    if (inputString.charAt(0) =='b') controlPlayerB();    

    inputString = "";
    stringComplete = false;
  }

  bluetoothEvent();
}


//// FUNCTIONS ////

void bluetoothEvent() {
  if (Bluetooth.available()) {
    char inChar = (char)Bluetooth.read();
    inputString += inChar;
    if (inChar == 'z') {
      stringComplete = true;
    }
  }
}

void serialEvent() {
  if (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == 'z') {
    stringComplete = true;
    }
  }
}

void controlPlayerA() {
  // Speed 
  if (inputString.charAt(1) =='0') {
    if (inputString.charAt(2) =='0') speedTrainA = 0;
    if (inputString.charAt(2) =='1') speedTrainA = speedArrayA[0];
    if (inputString.charAt(2) =='2') speedTrainA = speedArrayA[1];
    if (inputString.charAt(2) =='3') speedTrainA = speedArrayA[2];
    if (inputString.charAt(2) =='4') speedTrainA = speedArrayA[3];
    if (inputString.charAt(2) =='5') speedTrainA = speedArrayA[4];
    if (inputString.charAt(2) =='6') speedTrainA = speedArrayA[5];
    if (inputString.charAt(2) =='7') speedTrainA = speedArrayA[6];
    if (inputString.charAt(2) =='8') speedTrainA = speedArrayA[7]; 
    if (inputString.charAt(2) =='9') speedTrainA = speedArrayA[8];
  } 
  if (inputString.charAt(1) =='1') {
    if (inputString.charAt(2) =='0') speedTrainA = speedArrayA[9];
    if (inputString.charAt(2) =='1') speedTrainA = speedArrayA[10];
    if (inputString.charAt(2) =='2') speedTrainA = speedArrayA[11];
    if (inputString.charAt(2) =='3') speedTrainA = speedArrayA[12];
    if (inputString.charAt(2) =='4') speedTrainA = speedArrayA[13];
    if (inputString.charAt(2) =='5') speedTrainA = speedArrayA[14];
    if (inputString.charAt(2) =='6') speedTrainA = speedArrayA[15];
    if (inputString.charAt(2) =='7') speedTrainA = speedArrayA[16]; 
    if (inputString.charAt(2) =='8') speedTrainA = speedArrayA[17];
    if (inputString.charAt(2) =='9') speedTrainA = speedArrayA[18];   
  }
  if (inputString.charAt(1) =='2') {
    if (inputString.charAt(2) =='0') speedTrainA = speedArrayA[19];
    if (inputString.charAt(2) =='1') speedTrainA = speedArrayA[20];
    if (inputString.charAt(2) =='2') speedTrainA = speedArrayA[21];
    if (inputString.charAt(2) =='3') speedTrainA = speedArrayA[22]; 
    if (inputString.charAt(2) =='4') speedTrainA = speedArrayA[23];         
  }
  // Direction and Stop
  if (inputString.charAt(1) =='d') {
    if (inputString.charAt(2) =='f') { // (f) Forward
      digitalWrite(L298_IN1, HIGH);
      digitalWrite(L298_IN2, LOW); 
    }
    if (inputString.charAt(2) =='b') { // (b) Backward
      digitalWrite(L298_IN1, LOW);
      digitalWrite(L298_IN2, HIGH); 
    }
    if (inputString.charAt(2) =='s') { // (s) Stop button
      speedTrainA = 0;
    } 
  }
  analogWrite(L298_ENA, speedTrainA); // Throttle
}  

void controlPlayerB() {
  // Speed 
  if (inputString.charAt(1) =='0') {
    if (inputString.charAt(2) =='0') speedTrainB = 0;
    if (inputString.charAt(2) =='1') speedTrainB = speedArrayB[0];
    if (inputString.charAt(2) =='2') speedTrainB = speedArrayB[1];
    if (inputString.charAt(2) =='3') speedTrainB = speedArrayB[2];
    if (inputString.charAt(2) =='4') speedTrainB = speedArrayB[3];
    if (inputString.charAt(2) =='5') speedTrainB = speedArrayB[4];
    if (inputString.charAt(2) =='6') speedTrainB = speedArrayB[5];
    if (inputString.charAt(2) =='7') speedTrainB = speedArrayB[6];
    if (inputString.charAt(2) =='8') speedTrainB = speedArrayB[7]; 
    if (inputString.charAt(2) =='9') speedTrainB = speedArrayB[8];
  } 
  if (inputString.charAt(1) =='1') {
    if (inputString.charAt(2) =='0') speedTrainB = speedArrayB[9];
    if (inputString.charAt(2) =='1') speedTrainB = speedArrayB[10];
    if (inputString.charAt(2) =='2') speedTrainB = speedArrayB[11];
    if (inputString.charAt(2) =='3') speedTrainB = speedArrayB[12];
    if (inputString.charAt(2) =='4') speedTrainB = speedArrayB[13];
    if (inputString.charAt(2) =='5') speedTrainB = speedArrayB[14];
    if (inputString.charAt(2) =='6') speedTrainB = speedArrayB[15];
    if (inputString.charAt(2) =='7') speedTrainB = speedArrayB[16];
    if (inputString.charAt(2) =='8') speedTrainB = speedArrayB[17]; 
    if (inputString.charAt(2) =='9') speedTrainB = speedArrayB[18];  
  }
  if (inputString.charAt(1) =='2') {
    if (inputString.charAt(2) =='0') speedTrainB = speedArrayB[19];
    if (inputString.charAt(2) =='1') speedTrainB = speedArrayB[20];
    if (inputString.charAt(2) =='2') speedTrainB = speedArrayB[21];
    if (inputString.charAt(2) =='3') speedTrainB = speedArrayB[22]; 
    if (inputString.charAt(2) =='4') speedTrainB = speedArrayB[23];         
  }
  // Direction and Stop
  if (inputString.charAt(1) =='d') {
    if (inputString.charAt(2) =='f') { // (f) Forward
      digitalWrite(L298_IN3, HIGH);
      digitalWrite(L298_IN4, LOW); 
    }
    if (inputString.charAt(2) =='b') { // (b) Backward
      digitalWrite(L298_IN3, LOW);
      digitalWrite(L298_IN4, HIGH); 
    }
    if (inputString.charAt(2) =='s') { // (s) Stop button
      speedTrainB = 0;
    } 
  }
  analogWrite(L298_ENB, speedTrainB); // Throttle
}

Step 3: Connecting All Electronics

Assemble the circuits according to the figure and connect the power supply.

Step 4: Install Arduino Train Junior PRO or DUO

Install from Google Play any of the listed applications:
Arduino Train Junior PRO
Arduino Train DUO

My Apps is participate in the Designed for Families program on Google Play. You can share this app from Google Play with up to 5 family members using Google Play Family Library.

Step 5: Make Bluetooth Connections and Play

Pair the HC-06(05) modules with your Android devices. Run apps on two Android devices and connect to it Arduino control system. Now are you ready to train race!

Be the First to Share

    Recommendations

    • Sculpting Challenge

      Sculpting Challenge
    • 3D Printed Contest

      3D Printed Contest
    • Motor Vehicle Contest

      Motor Vehicle Contest

    Discussions