Project showcase

Automation of Railway Models Part 3 - Bidirectional Line © CC BY

Introduce to code for complex railway models. The control points for the movement of several trains via one line with a change of direction.

  • 1,497 views
  • 2 comments
  • 10 respects

Components and supplies

Apps and online services

About this project

Capability diagram

Before I bring the sketch from my real layout, in this post I will give a general solution. It is quite workable and you can apply it in your designs.

In this example, I use a variable of byte type to write the state of the rail blocks. As a result, we are able to transmit this number via bus I2C or serial port. It also makes it possible to perform mathematical operations with data (see the end of the sketch). The principle of recording data in the pictures below.

Byte

See my site for more details.

Without DDС management, only Open components. Please comments!

Schematics

6 Hall railway
Hall 6 bi tsadbfgc8y

Code

TEST_Bidirectional_6_HALLsArduino
// ARDUINORAILMAKET.RU
// TEST_Bidirectional_6_HALLs.ino
// EXAMPLE 5a | 05-05-2016
// Author: Steve Massikker

//// GPIO PINS////

#define HALL_0 2
#define HALL_1 3
#define HALL_2 4
#define HALL_3 5
#define HALL_4 6
#define HALL_5 7

#define LED_0 8
#define LED_1 9
#define LED_2 10
#define LED_3 11
#define LED_4 12

//// VARIABLES////
unsigned long delayMillis;
boolean hall_0, hall_1, hall_2, hall_3, hall_4, hall_5;
byte x;

void setup() {

// Initialize serial
  Serial.begin(9600);

// Initialize GPIO
  pinMode(HALL_0, INPUT);
  pinMode(HALL_1, INPUT);
  pinMode(HALL_2, INPUT);
  pinMode(HALL_3, INPUT);
  pinMode(HALL_4, INPUT);
  pinMode(HALL_5, INPUT);
  pinMode(LED_0, OUTPUT);
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);

// At start or reset
  bitSet(x, 7); // reserved | not used
  Serial.println(" ");
  Serial.println("Start sketch");
  Serial.println("----------");
  Serial.println("0B76543210");
  Serial.println("----------");

}

void loop() {

// HALLS STATES
  if (digitalRead(HALL_0) == LOW) {
    hall_0 = true;
    bitSet(x, 0);
    delayMillis = millis();
  }
  else hall_0 = false;

  // -------------->
  if (digitalRead(HALL_1) == LOW) { hall_1 = true; bitSet(x, 1); }
  else hall_1 = false;

  if (digitalRead(HALL_2) == LOW) { hall_2 = true; bitSet(x, 2); }
  else hall_2 = false;

  if (digitalRead(HALL_3) == LOW) { hall_3 = true; bitSet(x, 3); }
  else hall_3 = false;

  if (digitalRead(HALL_4) == LOW) { hall_4 = true; bitSet(x, 4); }
  else hall_4 = false;
  // <--------------

  if (digitalRead(HALL_5) == LOW) {
    hall_5 = true;
    bitSet(x, 5);
    delayMillis = millis();
  }
  else hall_5 = false;

  // DIRECTION | bit(6)
  // LR
  if (bitRead(x, 0) == 1) {
    if (hall_1) { bitSet(x, 6); bitClear(x, 0); }
  }
  // RL
  if (bitRead(x, 5) == 1) {
    if (hall_4) { bitClear(x, 6); bitClear(x, 5); }
  }

  // TRANSFER
  if (bitRead(x, 6) == 1) {
    // LR
    if (bitRead(x, 1) == 1) {
      if (hall_2) bitClear(x, 1);
    }

    if (bitRead(x, 2) == 1) {
      if (hall_3) bitClear(x, 2);
    }

    if (bitRead(x, 3) == 1) {
      if (hall_4) bitClear(x, 3);
    }

    if (bitRead(x, 4) == 1) {
      if (hall_5) bitClear(x, 4);
    }
  }
  else {
    // RL
    if (bitRead(x, 4) == 1) {
      if (hall_3) bitClear(x, 4);
    }

    if (bitRead(x, 3) == 1) {
      if (hall_2) bitClear(x, 3);
    }

    if (bitRead(x, 2) == 1) {
      if (hall_1) bitClear(x, 2);
    }

    if (bitRead(x, 1) == 1) {
      if (hall_0) bitClear(x, 1);
    }
  }

// SIGNAL

  if (bitRead(x, 0) == 1) digitalWrite(LED_0, HIGH);
  else digitalWrite(LED_0, LOW);

  if (bitRead(x, 1) == 1) digitalWrite(LED_1, HIGH);
  else digitalWrite(LED_1, LOW);

  if (bitRead(x, 2) == 1) digitalWrite(LED_2, HIGH);
  else digitalWrite(LED_2, LOW);

  if (bitRead(x, 3) == 1) digitalWrite(LED_3, HIGH);
  else digitalWrite(LED_3, LOW);

  if (bitRead(x, 4) == 1) digitalWrite(LED_4, HIGH);
  else digitalWrite(LED_4, LOW);

  if (millis() > (delayMillis + 1000)) { 
    bitClear(x, 0);
    bitClear(x, 5);
  }

// OPTIONAL
  // int number_of_trains_on_distance =
  //   bitRead(x, 1) + bitRead(x, 2) + bitRead(x, 3) + bitRead(x, 4);
  // byte check_free_distance = x << 2;
  //   if (check_free_distance == 0) Serial.print("free");
  //   else Serial.print("trains");

  Serial.print("->");
  Serial.println(x,BIN);

}

Comments

Similar projects you might like

Arduino Obstacle Avoidance Robot with Ultrasonic HC-SR04

Project tutorial by Jorge Rancé

  • 345 views
  • 1 comment
  • 3 respects

Using Finite State Machines

by Gustavo Gonnet

  • 5,818 views
  • 2 comments
  • 16 respects

Alexa: "Your Clothes Are Dry"

Project in progress by TNunnster

  • 1,336 views
  • 0 comments
  • 5 respects

Temperature Streaming with Arduino + Big Data Tools

Project showcase by Gabriel Rodriguez

  • 1,299 views
  • 1 comment
  • 14 respects

How to Configure NeoPixels Using Vixen Lights and Arduino

Project tutorial by Victor Aguilar

  • 137 views
  • 0 comments
  • 3 respects

Bluetooth Speaker w/ Music-Reactive LED Matrix

Project tutorial by Modustrial Maker

  • 2,811 views
  • 0 comments
  • 10 respects
Add projectSign up / Login