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,951 views
• 10 respects

## Components and supplies

 Arduino Nano R3
×1
 Hall module for Arduino
×6
 LED (generic)
×5
 Resistor 221 ohm
×5
×1

## Apps and online services

 Arduino IDE

### 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.

## 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
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;
// <--------------

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 =
// byte check_free_distance = x << 2;
//   if (check_free_distance == 0) Serial.print("free");
//   else Serial.print("trains");

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

}
```

## Schematics

• 6 projects
• 26 followers

October 5, 2017

#### Members who respect this project

and 4 others

See similar projects
you might like

#### Hacking Qualcomm (Quick Charge) QC 2.0/3.0 with ATtiny85

Project tutorial by Shahariar

• 1,247 views
• 17 respects

• 1,246 views
• 9 respects

• 7,683 views
• 51 respects

#### Program a DHT11 Temperature and Humidity Sensor

Project showcase by rowan07

• 3,106 views
• 13 respects

#### Robotic Hand with Wireless Glove Controlled | Arduino

Project showcase by Mert Arduino Project and Tech

• 2,492 views
• 1 comment
• 29 respects

#### Air Surfer

Project tutorial by Anton

• 14,064 views