Project showcase

Arduino Train For Kids © Apache-2.0

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

  • 5,306 views
  • 5 comments
  • 36 respects

Components and supplies

Apps and online services

About this project

This project is designed to support your children's interest in classical toys. Probably you already have a train toy set box, any one loco with electric motors drive is suitable. Rolling the train around in a circle is quite boring, so we'll add flashing lights when driving and controlling from any Android tablet or phone.

For group games, you can remake several locomotives, each of which can be controlled by a separate tablet or phone. If you look at the photo of the uncovered locomotive, you will also see the IR sensor. More on www.arduinorailwaycontrol.com

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 L9110S_AIA 3
#define L9110S_AIB 5
#define GND1 8
#define LED1 9
#define LED2 10
#define FLOODLIGHT 11
#define GND2 12


String inputString = "";  
boolean stringComplete = false; 
int locoSpeed;
bool tick, directionForward;
static unsigned long preMillis;
bool floodlight_ON = false, floodlight_IN = false, floodlight_OUT = false;
int counter = 0;
  
void setup() {
  // Initialize serial
  Serial.begin(9600);
  inputString.reserve(4); 
  Serial.println("Connected");  
  // Pins
  pinMode(GND1, OUTPUT);
  pinMode(GND2, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(FLOODLIGHT, OUTPUT);
  pinMode(L9110S_AIA, OUTPUT);
  pinMode(L9110S_AIB, OUTPUT);

  digitalWrite(GND1, LOW);
  digitalWrite(GND2, LOW);  
}

void loop() {

  if (stringComplete) {

    // TROTTLE
    if (inputString.charAt(0) =='t') { 
      if (inputString.charAt(1) =='0') locoSpeed = 0; // Stop
      if (inputString.charAt(1) =='1') locoSpeed = 50;
      if (inputString.charAt(1) =='2') locoSpeed = 80;
      if (inputString.charAt(1) =='3') locoSpeed = 100;
      if (inputString.charAt(1) =='4') locoSpeed = 150;
      if (inputString.charAt(1) =='5') locoSpeed = 200;
    }

    // MOVE DIRECTION
    if (inputString.charAt(0) =='d') {
      if (inputString.charAt(1) =='f') directionForward = true;
      if (inputString.charAt(1) =='r') directionForward = false;
      if (inputString.charAt(1) =='s') locoSpeed = 0;

      digitalWrite(L9110S_AIA, LOW);
      digitalWrite(L9110S_AIB, LOW);
      delay(400); 
    }

    // LIGHT
    if (inputString.charAt(0) =='l') { 
      if (inputString.charAt(1) =='a') floodlight_IN = true;
      if (inputString.charAt(1) =='b') floodlight_OUT = true;
    }

    inputString = "";
    stringComplete = false;
  }

  if (directionForward) {
    analogWrite(L9110S_AIA, locoSpeed);
    digitalWrite(L9110S_AIB, LOW); 
  }
  else {
    analogWrite(L9110S_AIB, locoSpeed);
    digitalWrite(L9110S_AIA, LOW);     
  }

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

  // LIGHT FADEIN START
  if (floodlight_IN && !floodlight_ON) {
    counter = 0;    
    floodlight_IN = false;
    floodlight_ON = true;
  }

  // LIGHT FADEOUT START  
  if (floodlight_OUT && floodlight_ON) {
    counter = 0;    
    floodlight_OUT = false;
    floodlight_ON = false;
  }

  // FLOODLIGHT ON
  if (floodlight_ON) {
    if (counter < 255) analogWrite(FLOODLIGHT, counter);
    else analogWrite(FLOODLIGHT, 255);
  }

  // FLOODLIGHT 0FF
  if (!floodlight_ON) {
    if (counter < 255) analogWrite(FLOODLIGHT, 255 - counter);
    else analogWrite(FLOODLIGHT, 0);
  }

  // TICK
  if (millis() > (preMillis + 10)) {
    counter++;
    preMillis = millis(); 
  }  
}

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

Schematics

box-scheme_1rPR2ucZ5K.jpg
Box scheme 1rpr2ucz5k

Comments

Similar projects you might like

uBUG

Project showcase by JHG

  • 1,988 views
  • 4 comments
  • 8 respects

Play Polyphonic Tones!

by nathan_ramanathan

  • 3,914 views
  • 5 comments
  • 10 respects

Arduino Tutorial: JARVIS v1 | How to make a Home Automation

Project tutorial by rahulkhanna

  • 6,430 views
  • 10 comments
  • 50 respects

Electric Kiln Controller

Project showcase by MrRoboto19

  • 1,391 views
  • 1 comment
  • 13 respects

Pedometer (Arduino 101)

Project tutorial by Frank Williams

  • 1,070 views
  • 0 comments
  • 6 respects

Retro 1980s LED Clock/Count Up Timer

Project in progress by randtekk

  • 1,503 views
  • 4 comments
  • 10 respects
Add projectSign up / Login