Project showcase

Arduino Train for Kids 2 © Apache-2.0

Redesign of a cheap set of a kid's railway into an intelligent train with control from the Android device. Project updated. Added sensor.

  • 7,824 views
  • 6 comments
  • 39 respects

Components and supplies

Apps and online services

About this project

Own and managed the "adult" railway model is certainly good. But if you have children, their enthusiasm for your "toy" will cost you dearly! So I came up with an Application that unites you. It is enough to take any children's railway, the main thing is that the locomotive had a motor and a place for batteries. You can even buy a few simple sets with plastic rails, and manage several trains at the same times. Lego Trains, Big Thomas Coal Steam and so on is good example.

Despite the fact that the application interface is made for children, the application itself supports the version of Protocol 2 of the URB project. And this means that the same application can be controlled also the railway layout, though not all, but only trains under control driver A. Also the application supports the installation of Arduino sensors, which makes the train behave a bit like a robot.

Before running my application, pair your device and the Bluetooth module HC-06 or yours Bluetooth shield way through the Android settings. (see video).

Code

Sketch for LocoArduino
#define SENSOR_IR 2
#define L9110S_AIA 3
#define L9110S_AIB 5
#define GND1 8
#define LED1 9
#define LED2 10
#define FLOODLIGHT 11
#define GND2 12

static unsigned long preMillis;
String inputString = "";
boolean stringComplete = false;
int locoSpeed, counter = 0;
bool tick, directionForward, directionBackward;
bool sensorActive = false, alarm_allRed = false;


void setup() {
  // Initialize serial
  Serial.begin(9600);
  inputString.reserve(4);

  // Pins
  pinMode(GND1, OUTPUT);
  pinMode(GND2, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(FLOODLIGHT, OUTPUT);
  pinMode(L9110S_AIA, OUTPUT);
  pinMode(L9110S_AIB, OUTPUT);
  pinMode(SENSOR_IR, INPUT);

  digitalWrite(GND1, LOW);
  digitalWrite(GND2, LOW);
  digitalWrite(FLOODLIGHT, HIGH);
  delay(300);
  digitalWrite(FLOODLIGHT, LOW);
}

void loop() {

  if (stringComplete) {

    // TRAIN CONTROL
    if (inputString.charAt(0) =='a') {

      // TROTTLE
      if (inputString.charAt(1) =='0') {
        if (inputString.charAt(2) =='0') locoSpeed = 0; // Stop
        if (inputString.charAt(2) =='2') locoSpeed = 80;
        if (inputString.charAt(2) =='4') locoSpeed = 110;
        if (inputString.charAt(2) =='6') locoSpeed = 140;
        if (inputString.charAt(2) =='8') locoSpeed = 170;
      }
      if (inputString.charAt(1) =='1') locoSpeed = 200;

      // MOVE DIRECTION
      if (inputString.charAt(1) =='d') {
        if (inputString.charAt(2) =='f') {
          directionForward = true;
          directionBackward = false;
        }
        if (inputString.charAt(2) =='b') {
          directionForward = false;
          directionBackward = true;
        }
        if (inputString.charAt(2) =='s') {
          locoSpeed = 0;
          directionForward = false;
          directionBackward = false;
          // DC motor conflict prevention
          digitalWrite(L9110S_AIA, LOW);
          digitalWrite(L9110S_AIB, LOW);
          delay(400);
        }
      }        
    }

    // LIGHT
    if (inputString.charAt(0) =='l') {
    	if (inputString.charAt(2) =='0') digitalWrite(FLOODLIGHT, LOW);
      if (inputString.charAt(2) =='1') digitalWrite(FLOODLIGHT, HIGH);
    }

    // SENSOR ON/OFF
    if (inputString.charAt(0) =='s') {
      if (inputString.charAt(2) =='0') sensorActive = false;
      if (inputString.charAt(2) =='1') sensorActive = true;
    }

    inputString = "";
    stringComplete = false;
  }

  if (digitalRead(SENSOR_IR) == HIGH || !sensorActive) {

    if (directionBackward) {
      digitalWrite(L9110S_AIB, LOW);    	
      analogWrite(L9110S_AIA, locoSpeed);
    }
    if (directionForward) {
      digitalWrite(L9110S_AIA, LOW);
      analogWrite(L9110S_AIB, locoSpeed);
    }
    alarm_allRed = false;

  }
  else { // STOP at IR sensor

    digitalWrite(L9110S_AIA, LOW);
    digitalWrite(L9110S_AIB, LOW);
    Serial.println("alarmz");
    alarm_allRed = true;
  }

  if (locoSpeed > 0 && !alarm_allRed) {
    static unsigned long blinkMillis;
    if (millis() > (blinkMillis + 300)) {
      tick = !tick;
      blinkMillis = millis();
    }
    digitalWrite(LED1, tick);
    digitalWrite(LED2, !tick);
  }
  else if (alarm_allRed) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
  }
  else {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
  }
}

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

Schematics

Electric scheme
Diagramm 2foradnjvv

Comments

Similar projects you might like

PENXZYL: Arduino 3-Axis Brush Plotter

Project showcase by Guiye Perez Bongiovanni

  • 3,529 views
  • 2 comments
  • 15 respects

MyRiver

Project tutorial by Andrei Florian

  • 8,366 views
  • 2 comments
  • 33 respects

Detecting Heart Rate with a Photoresistor

Project tutorial by 3 developers

  • 6,610 views
  • 3 comments
  • 17 respects

Weather Forecast Clock Using Old Alarm and Arduino

Project showcase by LenkaDesign

  • 4,098 views
  • 10 comments
  • 22 respects

Photoresistor Characterization

by 3 developers

  • 2,156 views
  • 4 comments
  • 5 respects

u-blox LEA-6H 02 GPS Module with Arduino and Python

Project tutorial by Harshgosar

  • 1,327 views
  • 7 comments
  • 5 respects
Add projectSign up / Login