Project showcase

Android app for control train © CC BY-NC

Control your train and railway layout with an Android app and Arduino.

  • 2,114 views
  • 7 comments
  • 11 respects

Components and supplies

Apps and online services

About this project

This project (see figure) is a typical version of robot control based on Arduino. Similar schemes you will easily find on the Internet. The only difference is that the output of the motor driver is connected to the rails, and not directly to the motor. Of course, you can use your combinations of bluetooth modules and motor-drivers, as well as ready shields. I recommend the L298N Dual H-Bridge Motor Controller and HC-06 (HC-05) bluetooth modules.

Sketch

This sketch is based on the example SerialEvent from Arduino IDE. The difference is that еhe processing of the control command is include to the function, and complete the command instead of the end of the stroke (/ n) uses the "z" character. Also Parsing part code convert command to AnalogWrite(speed) and direction moving the loco.

Command

External command consist three symbols. The last symbol is "z". You can come up with your own commands consisting of the first two letters, and sending them, for example, through the Arduino IDE serial port terminal using USB to move the train on the rails. Android application ArduinoTrainBtControl Lite sends commands exactly as well, but via bluetooth. Full description of ArduinoTrainBtControl Lite commands on my site in the section Protocol.

Step-by-step instructions here.

Update

There was a question about the scheme...

Code

Command stationArduino
// http://arduinorailwaycontrol.com/
// Beginner.ino
// V.4.2
// 15.10.2017
// Author: Steve Massikker


/////// COMMAND STATION ON BREADBOARD ///////

//// GPIO PINS ////

// L298
#define ENA_PIN 3
#define IN1_PIN 2
#define IN2_PIN 4

//// VARIABLES ////
boolean stringComplete = false;
String inputString = ""; 
int speedLoco;

void setup() {

// Initialize Serial
  Serial.begin(9600);
  inputString.reserve(16); 

// Initialize Motor Driver
  pinMode(ENA_PIN, OUTPUT); 
  pinMode(IN1_PIN, OUTPUT); 
  pinMode(IN2_PIN, OUTPUT); 

}

void loop() {

  if (stringComplete) {
    Serial.print("inputString = ");    // PRINT COMMAND //
    Serial.println(inputString);       // TO SERIAL      //

// ----------- START COMMAND PARSING ----------- // 

    //THROTTLE 
    if (inputString.charAt(0) =='t') {   
      if (inputString.charAt(1) =='0') {
        analogWrite(ENA_PIN, 0);
        speedLoco = 0;
      }
      if (inputString.charAt(1) =='1') { 
        analogWrite(ENA_PIN, 80); // change this value for acceleration your loco --- example: analogWrite(ENA_PIN, 120)
        speedLoco = 1;
      }
      if (inputString.charAt(1) =='2') {
        analogWrite(ENA_PIN, 100);  // START LOCO --- example: analogWrite(ENA_PIN, 160)
        speedLoco = 2;
      }
      if (inputString.charAt(1) =='3') { 
        analogWrite(ENA_PIN, 150);  // --- example: analogWrite(ENA_PIN, 180)
        speedLoco = 3;
      }
      if (inputString.charAt(1) =='4') {
        analogWrite(ENA_PIN, 200); // --- example: analogWrite(ENA_PIN, 220)
        speedLoco = 4;
      }    
      if (inputString.charAt(1) =='5') { 
        analogWrite(ENA_PIN, 255);
        speedLoco = 5;
      } 
    }

    // DIRECTION 
    if (inputString.charAt(0) =='d') {
      if (inputString.charAt(1) =='r') {
        digitalWrite(IN1_PIN, HIGH);
        digitalWrite(IN2_PIN, LOW);
      }
      if (inputString.charAt(1) =='f') {
        digitalWrite(IN1_PIN, LOW);
        digitalWrite(IN2_PIN, HIGH);
      }
      if (inputString.charAt(1) =='s') {
        digitalWrite(IN1_PIN, LOW);
        digitalWrite(IN2_PIN, LOW);
        analogWrite(ENA_PIN, 0);
        speedLoco = 0;
      } 
    }
      
    //JUNCTIONS  
    if (inputString.charAt(0) =='j') { 
      if (inputString.charAt(1) =='a') { 
        digitalWrite(LED_BUILTIN, HIGH);        
      }
      if (inputString.charAt(1) =='b') { 
        digitalWrite(LED_BUILTIN, LOW);      
      }
    }

// ----------- END COMMAND PARSING ----------- // 

    inputString = "";
    stringComplete = false;    
  }
}

// ----------- FUNCTIONS ----------- // 

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

Schematics

Control loco width Android tablet or phone
Begginner  diupsurb4d

Comments

Similar projects you might like

Smart Energy Saver for Your Home

Project tutorial by Dhairya Parikh

  • 3,074 views
  • 8 comments
  • 12 respects

Night Light Assistant

Project tutorial by Gabriele Fugazzi

  • 218 views
  • 0 comments
  • 1 respect

Twilight switch with levels

Project in progress by Rafa Salvador

  • 237 views
  • 0 comments
  • 1 respect

A Geiger Counter Simulator

Project tutorial by organtin

  • 81 views
  • 0 comments
  • 2 respects

Arduino MKR DIN Rail Mount

Project showcase by hwhardsoft

  • 931 views
  • 0 comments
  • 4 respects

Simple Water Quality Analysis

Project showcase by Wen-Liang Lin

  • 7,055 views
  • 5 comments
  • 10 respects
Add projectSign up / Login