Steve_Massikker
Published © Apache-2.0

Arduino Train for Kids 2

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

IntermediateShowcase (no instructions)3 hours7,850

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
L9110s motor-driver
×1
Bluetooth HC-06
×1
Generic Arduino IR-sensor module
×1

Software apps and online services

Arduino Train for Kids 2
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Electric scheme

Diagramm 2foradnjvv

Code

Sketch for Loco

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

Credits

Steve_Massikker

Steve_Massikker

1 project • 48 followers
Contact

Comments