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.

  • 7,367 views
  • 6 comments
  • 38 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

Binary Clock

Project showcase by pdio

  • 100 views
  • 4 comments
  • 1 respect

Central Heating Boiler Control Box

Project tutorial by Peter Groeneveld

  • 2,632 views
  • 6 comments
  • 17 respects

Arduino - Set Schedule via Web

Project tutorial by phpoc_man

  • 404 views
  • 2 comments
  • 13 respects

Protect Your Room with Fingerprints for Under $20

Project tutorial by Srivishnu Piratla

  • 3,137 views
  • 1 comment
  • 11 respects

Sump Pit Alarm

Project showcase by loupalladino

  • 110 views
  • 0 comments
  • 3 respects

Smart Talking Humanoid Robot Just with Arduino!

Project tutorial by ashraf_minhaj

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