Steve_Massikker
Published © CC BY

Automation of Railway Models Part 3 - Bidirectional Line

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

IntermediateShowcase (no instructions)2,220
Automation of Railway Models Part 3 - Bidirectional Line

Things used in this project

Hardware components

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

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

6 Hall railway

Hall 6 bi tsadbfgc8y

Code

TEST_Bidirectional_6_HALLs

Arduino
// 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);

}

Credits

Steve_Massikker

Steve_Massikker

7 projects • 44 followers
Contact

Comments