Project tutorial
Android App for Garden Train

Android App for Garden Train © Apache-2.0

Control the trains of your railway from your phone. Switching between trains by simply changing Bluetooth connections.

  • 10 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project

The owner of the garden railway suggested that I write a special application for its hobby. Prior to that, he tested the DEMO2 application on his locomotive, and suggested several improvements. As a result, I wrote a new application with extended functions that controls the same Arduino hardware as the rest of my projects. Since in this project Arduino is installed directly on the train, unlike the URB project, everything becomes simpler.

You can install the system presented here on several locomotives and control them by choosing the MAC address of a specific BT- module installed on the locomotive.

Connect App to different locomotives

Commands to control train

The application sends a simple list of four-character commands via Bluetooth to Arduino, witches you can use yours functions into the sketch.

This example does not use all commands. By adding additional devices to the circuit (for example, a servo drive and a mini MP3 player for Arduino), you can use the functions of sounds and decoupling. There is also a switch CUSTOM which you can use at your discretion.

The application also uses feedback from the locomotive and shows the battery level of the locomotive.

Full description of the API, see on my website.

Code

Sketch for Garden TrainArduino
Simple example
//--------------------------------------//
//   # WWW.ARDUINORAILWAYCONTROL.COM    //
//                                      //
//   ARDUINO GARDEN APP | SECOND SKETCH //
//                                      //
//   V.1.2019  Steve Massikker          //
//--------------------------------------//

void(* resetFunc) (void) = 0; // RESET FUNCTION
#include <SoftwareSerial.h>

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

// L298
#define L298_ENA 9
#define L298_IN1 10
#define L298_IN2 11

// LEDs
#define FF_LED 3 // Forward Floodlight white LED
#define BF_LED 4 // Backward Floodlight white LED
#define FRR_LED 5 // Forward Rear Right red signal LED
#define FRL_LED 6 // Forward Rear Left red signal LED
#define BRR_LED 7 // Backward Rear Right red signal LED
#define BRL_LED 8 // Backward Rear Left red signal LED

// VARIABLES //
bool stringComplete = false;
String inputString = ""; 
//                     1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
byte speedArray [] = {40, 50, 60, 70, 80, 90,100,110,120,130,140,150,160,170,180,190,200,220,240,255};
byte speedTrain = 0;
bool forwardMovingTrain;

void setup() {

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

// Initializing Motor-Driver
  pinMode(L298_ENA, OUTPUT); 
  pinMode(L298_IN1, OUTPUT); 
  pinMode(L298_IN2, OUTPUT);

// Initializing LEDs  
  pinMode(FF_LED, OUTPUT); 
  pinMode(BF_LED, OUTPUT); 
  pinMode(FRR_LED, OUTPUT);
  pinMode(FRL_LED, OUTPUT); 
  pinMode(BRR_LED, OUTPUT); 
  pinMode(BRL_LED, OUTPUT);  

// Blinking all LEDs
  digitalWrite(FF_LED, HIGH); 
  digitalWrite(BF_LED, HIGH);  
  delay(100);
  digitalWrite(FRR_LED, HIGH); 
  digitalWrite(FRL_LED, HIGH); 
  digitalWrite(BRR_LED, HIGH); 
  digitalWrite(BRL_LED, HIGH); 
  delay(150);
  digitalWrite(FF_LED, LOW); 
  digitalWrite(BF_LED, LOW); 
  digitalWrite(FRR_LED, LOW); 
  digitalWrite(FRL_LED, LOW); 
  digitalWrite(BRR_LED, LOW); 
  digitalWrite(BRL_LED, LOW); 

}

void loop() {

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

    if (inputString =="999") resetFunc(); // RESET AFTER BT CONNECTION 

    // Train control
    if (inputString.charAt(0) =='a') {

      // Speed 
      if (inputString.charAt(1) =='0') {
        if (inputString.charAt(2) =='0') speedTrain = 0;
        if (inputString.charAt(2) =='1') speedTrain = speedArray[0];
        if (inputString.charAt(2) =='2') speedTrain = speedArray[1];
        if (inputString.charAt(2) =='3') speedTrain = speedArray[2];
        if (inputString.charAt(2) =='4') speedTrain = speedArray[3];
        if (inputString.charAt(2) =='5') speedTrain = speedArray[4];
        if (inputString.charAt(2) =='6') speedTrain = speedArray[5];
        if (inputString.charAt(2) =='7') speedTrain = speedArray[6];
        if (inputString.charAt(2) =='8') speedTrain = speedArray[7];
        if (inputString.charAt(2) =='9') speedTrain = speedArray[8]; 
      } 
      if (inputString.charAt(1) =='1') {
        if (inputString.charAt(2) =='0') speedTrain = speedArray[9];
        if (inputString.charAt(2) =='1') speedTrain = speedArray[10];
        if (inputString.charAt(2) =='2') speedTrain = speedArray[11];
        if (inputString.charAt(2) =='3') speedTrain = speedArray[12];
        if (inputString.charAt(2) =='4') speedTrain = speedArray[13];
        if (inputString.charAt(2) =='5') speedTrain = speedArray[14];
        if (inputString.charAt(2) =='6') speedTrain = speedArray[15];
        if (inputString.charAt(2) =='7') speedTrain = speedArray[16];
        if (inputString.charAt(2) =='8') speedTrain = speedArray[17];
        if (inputString.charAt(2) =='9') speedTrain = speedArray[18]; 
      }
      if (inputString.charAt(1) =='2') {      
        if (inputString.charAt(2) =='0') speedTrain = speedArray[19];
      }  

      // Direction and Stop
      if (inputString.charAt(1) =='d') {
        if (inputString.charAt(2) =='f') { // (f) Forward
          digitalWrite(L298_IN1, HIGH);
          digitalWrite(L298_IN2, LOW);
          forwardMovingTrain = true;
        }
        if (inputString.charAt(2) =='b') { // (b) Backward
          digitalWrite(L298_IN1, LOW);
          digitalWrite(L298_IN2, HIGH);
          forwardMovingTrain = false;
        }
        if (inputString.charAt(2) =='s') { // (s) Stop button
          digitalWrite(L298_IN1, LOW);
          digitalWrite(L298_IN2, LOW);
          speedTrain = 0;
        } 
      }
      analogWrite(L298_ENA, speedTrain); // Throttle
    }

    // LEDs control
    if (inputString.charAt(0) =='l') { 
      // Forward Floodlight white LED
      if (inputString.charAt(1) =='f') { 
        if (inputString.charAt(2) =='0') digitalWrite(FF_LED, LOW);
        if (inputString.charAt(2) =='1') digitalWrite(FF_LED, HIGH);          
      }  
      // Backward Floodlight white LED
      if (inputString.charAt(1) =='b') { 
        if (inputString.charAt(2) =='0') digitalWrite(BF_LED, LOW);
        if (inputString.charAt(2) =='1') digitalWrite(BF_LED, HIGH);          
      }
      // Rear red LEDs
      if (inputString.charAt(1) =='r') { 
        if (inputString.charAt(2) =='0') {
          digitalWrite(FRR_LED, LOW); 
          digitalWrite(FRL_LED, LOW); 
          digitalWrite(BRR_LED, LOW); 
          digitalWrite(BRL_LED, LOW);             
        }
        if (inputString.charAt(2) =='1') {
          if (forwardMovingTrain) {
            digitalWrite(FRR_LED, LOW); 
            digitalWrite(FRL_LED, LOW); 
            digitalWrite(BRR_LED, HIGH); 
            digitalWrite(BRL_LED, HIGH);               
          }
          else {
            digitalWrite(FRR_LED, HIGH); 
            digitalWrite(FRL_LED, HIGH); 
            digitalWrite(BRR_LED, LOW); 
            digitalWrite(BRL_LED, LOW);                
          }
        }  
      }  

      if (inputString.charAt(1) =='0') { 
        digitalWrite(FF_LED, LOW);
        digitalWrite(BF_LED, LOW); 
        digitalWrite(FRR_LED, LOW); 
        digitalWrite(FRL_LED, LOW); 
        digitalWrite(BRR_LED, LOW); 
        digitalWrite(BRL_LED, LOW); 
      }       
    }  
    
    Serial.println(inputString); 
    inputString = "";
    stringComplete = false;
  }

  bluetoothEvent();
}


//// FUNCTIONS ////

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

Schematics

Simple circuit for Garden Train
Circuit oghxrjzocc

Comments

Similar projects you might like

Create a People Counter Controlled by an Android App

Project tutorial by Kutluhan Aktar

  • 5,224 views
  • 4 comments
  • 7 respects

DIY Speedometer Using Arduino and Processing Android App

Project tutorial by user334153146

  • 15,355 views
  • 0 comments
  • 19 respects

Make an Enhanced R2-D2 to Be Controlled by an Android App

Project tutorial by Kutluhan Aktar

  • 2,041 views
  • 0 comments
  • 2 respects

Line Follower Robot - PID Control - Android Setup

Project tutorial by MJRoBot

  • 33,174 views
  • 10 comments
  • 43 respects

Control Arduino Robot Arm with Android App

Project tutorial by Slant Concepts

  • 14,819 views
  • 9 comments
  • 35 respects

Arduino Train for Kids 2

Project showcase by Steve Massikker

  • 12,016 views
  • 7 comments
  • 46 respects
Add projectSign up / Login