Steve_Massikker
Published © Apache-2.0

Arduino Train for Kids

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

IntermediateShowcase (no instructions)3 hours7,245

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
L9110s motor-driver
×1
Bluetooth HC-06
×1

Software apps and online services

Arduino Train for Kids
Arduino IDE
Arduino IDE

Story

Read more

Schematics

box-scheme_1rPR2ucZ5K.jpg

Box scheme 1rpr2ucz5k

Code

Sketch for Loco

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

Credits

Steve_Massikker

Steve_Massikker

7 projects • 44 followers
Contact

Comments