Steve_Massikker
Published © CC BY-NC

Android app for control train

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

EasyShowcase (no instructions)2,352
Android app for control train

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
HC-06 Bluetooth module for Arduino
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Resistor 2.21k ohm
Resistor 2.21k ohm
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Android ArduinoTrainBtControl Lite

Story

Read more

Schematics

Control loco width Android tablet or phone

Begginner  diupsurb4d

Code

Command station

Arduino
// 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;
    }
  }
}

Credits

Steve_Massikker

Steve_Massikker

7 projects • 44 followers
Contact

Comments