Block Control

Block occupancy detection on a model railroad.

This material is based on the work of Mel Perry.  His original complete work can be seen here. My model railroad 2 –

Please Support Us By Looking At Our Shop
Please Support Use By Going Here.

The materials have been condensed here to make it easy for you to install.

This section is under construction

The detection circuit

The arduino Mega 260 with extension shields added for ease of wiring.

 

 

 

 

The sketch

 

The sketch below is a complete 14 block controller, it can be copied and pasted to the Arduino IDE.

 

void setup()

{

int greenledPin1 = 1;    // Signal Head 1 Green

int yellowledPin2 = 2;   // Signal Head 1 Yellow

int redledPin3 = 3;      // Signal Head 1 Red

int greenledPin4 = 4;    // Signal Head 2 Green

int yellowledPin5 = 5;   // Signal Head 2 Yellow

int redledPin6 = 6;      // Signal Head 2 Red

int greenledPin7 = 7;    // Signal Head 3 Green

int yellowledPin8 = 8;   // Signal Head 3 Yellow

int redledPin9 = 9;      // Signal Head 3 Red

int greenledPin10 = 10;  // Signal Head 4 Green

int yellowledPin11 = 11; // Signal Head 4 Yellow

int redledPin12 = 12;    // Signal Head 4 Red

int greenledPin13 = 13;  // Signal Head 5 Green

int yellowledPin14 = 14; // Signal Head 5 Yellow

int redledPin15 = 15;    // Signal Head 5 Red

int greenledPin16 = 16;  // Signal Head 6 Green

int yellowledPin17 = 17; // Signal Head 6 Yellow

int redledPin18 = 18;    // Signal Head 6 Red

int greenledPin19 = 19;  // Signal Head 7 Green

int yellowledPin20 = 20; // Signal Head 7 Yellow

int redledPin21 = 21;    // Signal Head 7 Red

int greenledPin22 = 22;  // Signal Head 8 Green

int yellowledPin23 = 23; // Signal Head 8 Yellow

int redledPin24 = 24;    // Signal Head 8 Red

int greenledPin25 = 25;  // Signal Head 9 Green

int yellowledPin26 = 26; // Signal Head 9 Yellow

int redledPin27 = 27;    // Signal Head 9 Red

int greenledPin28 = 28;  // Signal Head 10 Green

int yellowledPin29 = 29; // Signal Head 10 Yellow

int redledPin30 = 30;    // Signal Head 10 Red

int greenledPin31 = 31;  // Signal Head 11 Green

int yellowledPin32 = 32; // Signal Head 11 Yellow

int redledPin33 = 33;    // Signal Head 11 Red

int greenledPin34 = 34;  // Signal Head 12 Green

int yellowledPin35 = 35; // Signal Head 12 Yellow

int redledPin36 = 36;    // Signal Head 12 Red

int greenledPin37 = 37;  // Signal Head 13 Green

int yellowledPin38 = 38; // Signal Head 13 Yellow

int redledPin39 = 39;    // Signal Head 13 Red

int greenledPin40 = 40;  // Signal Head 14 Green

int yellowledPin41 = 41; // Signal Head 14 Yellow

int redledPin42 = 42;    // Signal Head 14 Red

int blockdetector1Pin = A1;    // Block 1 Occupancy Detector

int blockdetector2Pin = A2;    // Block 2 Occupancy Detector

int blockdetector3Pin = A3;    // Block 3 Occupancy Detector

int blockdetector4Pin = A4;    // Block 4 Occupancy Detector

int blockdetector5Pin = A5;    // Block 5 Occupancy Detector

int blockdetector6Pin = A6;    // Block 6 Occupancy Detector

int blockdetector7Pin = A7;    // Block 7 Occupancy Detector

int blockdetector8Pin = A8;    // Block 8 Occupancy Detector

int blockdetector9Pin = A9;    // Block 9 Occupancy Detector

int blockdetector10Pin = A10;  // Block 10 Occupancy Detector

int blockdetector11Pin = A11;  // Block 11 Occupancy Detector

int blockdetector12Pin = A12;  // Block 12 Occupancy Detector

int blockdetector13Pin = A13;  // Block 13 Occupancy Detector

int blockdetector14Pin = A14;  // Block 14 Occupancy Detector

  pinMode(greenledPin1, OUTPUT);

  pinMode(yellowledPin2, OUTPUT);

  pinMode(redledPin3, OUTPUT);

  pinMode(greenledPin4, OUTPUT); 

  pinMode(yellowledPin5, OUTPUT);

  pinMode(redledPin6, OUTPUT); 

  pinMode(greenledPin7, OUTPUT);

  pinMode(yellowledPin8, OUTPUT);

  pinMode(redledPin9, OUTPUT);

  pinMode(greenledPin10, OUTPUT); 

  pinMode(yellowledPin11, OUTPUT);

  pinMode(redledPin12, OUTPUT);   

  pinMode(greenledPin13, OUTPUT);

  pinMode(yellowledPin14, OUTPUT);

  pinMode(redledPin15, OUTPUT);

  pinMode(greenledPin16, OUTPUT); 

  pinMode(yellowledPin17, OUTPUT);

  pinMode(redledPin18, OUTPUT); 

  pinMode(greenledPin19, OUTPUT);

  pinMode(yellowledPin20, OUTPUT);

  pinMode(redledPin21, OUTPUT);

  pinMode(greenledPin22, OUTPUT); 

  pinMode(yellowledPin23, OUTPUT);

  pinMode(redledPin24, OUTPUT); 

  pinMode(greenledPin25, OUTPUT);

  pinMode(yellowledPin26, OUTPUT);

  pinMode(redledPin27, OUTPUT);

  pinMode(greenledPin28, OUTPUT); 

  pinMode(yellowledPin29, OUTPUT);

  pinMode(redledPin30, OUTPUT); 

  pinMode(greenledPin31, OUTPUT);

  pinMode(yellowledPin32, OUTPUT);

  pinMode(redledPin33, OUTPUT);

  pinMode(greenledPin34, OUTPUT); 

  pinMode(yellowledPin35, OUTPUT);

  pinMode(redledPin36, OUTPUT); 

  pinMode(greenledPin37, OUTPUT);

  pinMode(yellowledPin38, OUTPUT);

  pinMode(redledPin39, OUTPUT);

  pinMode(greenledPin40, OUTPUT); 

  pinMode(yellowledPin41, OUTPUT);

  pinMode(redledPin42, OUTPUT); 

  pinMode(blockdetector1Pin,  INPUT_PULLUP);

  pinMode(blockdetector2Pin,  INPUT_PULLUP);

  pinMode(blockdetector3Pin,  INPUT_PULLUP);

  pinMode(blockdetector4Pin,  INPUT_PULLUP);

  pinMode(blockdetector5Pin,  INPUT_PULLUP);

  pinMode(blockdetector6Pin,  INPUT_PULLUP);

  pinMode(blockdetector7Pin,  INPUT_PULLUP);

  pinMode(blockdetector8Pin,  INPUT_PULLUP);

  pinMode(blockdetector9Pin,  INPUT_PULLUP);

  pinMode(blockdetector10Pin, INPUT_PULLUP);

  pinMode(blockdetector11Pin, INPUT_PULLUP);

  pinMode(blockdetector12Pin, INPUT_PULLUP);

  pinMode(blockdetector13Pin, INPUT_PULLUP);

  pinMode(blockdetector14Pin, INPUT_PULLUP);

}

void loop()

{

int greenledPin1 = 1;    // Signal Head 1 Green

int yellowledPin2 = 2;   // Signal Head 1 Yellow

int redledPin3 = 3;      // Signal Head 1 Red

int greenledPin4 = 4;    // Signal Head 2 Green

int yellowledPin5 = 5;   // Signal Head 2 Yellow

int redledPin6 = 6;      // Signal Head 2 Red

int greenledPin7 = 7;    // Signal Head 3 Green

int yellowledPin8 = 8;   // Signal Head 3 Yellow

int redledPin9 = 9;      // Signal Head 3 Red

int greenledPin10 = 10;  // Signal Head 4 Green

int yellowledPin11 = 11; // Signal Head 4 Yellow

int redledPin12 = 12;    // Signal Head 4 Red

int greenledPin13 = 13;  // Signal Head 5 Green

int yellowledPin14 = 14; // Signal Head 5 Yellow

int redledPin15 = 15;    // Signal Head 5 Red

int greenledPin16 = 16;  // Signal Head 6 Green

int yellowledPin17 = 17; // Signal Head 6 Yellow

int redledPin18 = 18;    // Signal Head 6 Red

int greenledPin19 = 19;  // Signal Head 7 Green

int yellowledPin20 = 20; // Signal Head 7 Yellow

int redledPin21 = 21;    // Signal Head 7 Red

int greenledPin22 = 22;  // Signal Head 8 Green

int yellowledPin23 = 23; // Signal Head 8 Yellow

int redledPin24 = 24;    // Signal Head 8 Red

int greenledPin25 = 25;  // Signal Head 9 Green

int yellowledPin26 = 26; // Signal Head 9 Yellow

int redledPin27 = 27;    // Signal Head 9 Red

int greenledPin28 = 28;  // Signal Head 10 Green

int yellowledPin29 = 29; // Signal Head 10 Yellow

int redledPin30 = 30;    // Signal Head 10 Red

int greenledPin31 = 31;  // Signal Head 11 Green

int yellowledPin32 = 32; // Signal Head 11 Yellow

int redledPin33 = 33;    // Signal Head 11 Red

int greenledPin34 = 34;  // Signal Head 12 Green

int yellowledPin35 = 35; // Signal Head 12 Yellow

int redledPin36 = 36;    // Signal Head 12 Red

int greenledPin37 = 37;  // Signal Head 13 Green

int yellowledPin38 = 38; // Signal Head 13 Yellow

int redledPin39 = 39;    // Signal Head 13 Red

int greenledPin40 = 40;  // Signal Head 14 Green

int yellowledPin41 = 41; // Signal Head 14 Yellow

int redledPin42 = 42;    // Signal Head 14 Red

int blockdetector1Pin = A1;    // Block 1 Occupancy Detector

int blockdetector2Pin = A2;    // Block 2 Occupancy Detector

int blockdetector3Pin = A3;    // Block 3 Occupancy Detector

int blockdetector4Pin = A4;    // Block 4 Occupancy Detector

int blockdetector5Pin = A5;    // Block 5 Occupancy Detector

int blockdetector6Pin = A6;    // Block 6 Occupancy Detector

int blockdetector7Pin = A7;    // Block 7 Occupancy Detector

int blockdetector8Pin = A8;    // Block 8 Occupancy Detector

int blockdetector9Pin = A9;    // Block 9 Occupancy Detector

int blockdetector10Pin = A10;  // Block 10 Occupancy Detector

int blockdetector11Pin = A11;  // Block 11 Occupancy Detector

int blockdetector12Pin = A12;  // Block 12 Occupancy Detector

int blockdetector13Pin = A13;  // Block 13 Occupancy Detector

int blockdetector14Pin = A14;  // Block 14 Occupancy Detector

{

  // CCW Block 1 Green

  // read from the button pin

      digitalRead(blockdetector1Pin);

      digitalRead(blockdetector2Pin);

          if (digitalRead(blockdetector1Pin) == HIGH  &&  digitalRead(blockdetector2Pin) == HIGH) // read two switches

         digitalWrite(greenledPin1,LOW);

  else

  {

      digitalWrite(greenledPin1, HIGH);

   }

  //CCW Block 1 Yellow

  // read from the button pin

          digitalRead(blockdetector1Pin);

      digitalRead(blockdetector2Pin);

  if (digitalRead(blockdetector1Pin) == HIGH  &&  digitalRead(blockdetector2Pin) == LOW)

      digitalWrite(yellowledPin2,LOW);

  else

  {

      digitalWrite(yellowledPin2, HIGH);

   }

    //CCW Block 1 Red

  // read from the button pin

      digitalRead(blockdetector1Pin);

       if (digitalRead(blockdetector1Pin) == LOW)

      digitalWrite(redledPin3,LOW);

  else

  {

      digitalWrite(redledPin3, HIGH);

          }

  // ==============================================================

      {

  // CCW Block 2 Green

    // read from the button pin

      digitalRead(blockdetector2Pin);

      digitalRead(blockdetector3Pin);

         

  if (digitalRead(blockdetector3Pin) == HIGH  &&  digitalRead(blockdetector3Pin) == HIGH) // read two switches

         digitalWrite(greenledPin4,LOW);

  else

  {

      digitalWrite(greenledPin4, HIGH);

   }

  //CCW Block 2 Yellow

  // read from the button pin

            digitalRead(blockdetector2Pin);

      digitalRead(blockdetector3Pin);

  if (digitalRead(blockdetector2Pin) == HIGH  &&  digitalRead(blockdetector3Pin) == LOW) //read two switches

      digitalWrite(yellowledPin5,LOW);

  else

  {

      digitalWrite(yellowledPin5, HIGH);

   }

    //CCW Block 2 Red

  // read from the button pin

      digitalRead(blockdetector2Pin);

       if (digitalRead(blockdetector2Pin) == LOW)

      digitalWrite(redledPin6,LOW);

  else

  {

      digitalWrite(redledPin6, HIGH);

          }

         }

 

  // ==================================================================

  {

  // CCW Block 4 Green

    // read from the button pin

      digitalRead(blockdetector4Pin);

      digitalRead(blockdetector5Pin);

          if (digitalRead(blockdetector4Pin) == HIGH  &&  digitalRead(blockdetector5Pin) == HIGH) //read two switches

         digitalWrite(greenledPin7,LOW);

  else

  {

      digitalWrite(greenledPin7, HIGH);

   }

  //CCW Block 4 Yellow

  // read from the button pin

            digitalRead(blockdetector4Pin);

      digitalRead(blockdetector4Pin);

  if (digitalRead(blockdetector4Pin) == HIGH  &&  digitalRead(blockdetector5Pin) == LOW)

      digitalWrite(yellowledPin8,LOW);

  else

  {

      digitalWrite(yellowledPin8, HIGH);

   }

    //CCW Block 4 Red

  // read from the button pin

      digitalRead(blockdetector4Pin);

        if (digitalRead(blockdetector4Pin) == LOW)

      digitalWrite(redledPin9,LOW);

  else

  {

      digitalWrite(redledPin9, HIGH);

          }

    }}

  // ================================================================

{

  // CCW Block 5 Green

    // read from the button pin

      digitalRead(blockdetector5Pin);

      digitalRead(blockdetector6Pin);

  if (digitalRead(blockdetector5Pin) == HIGH  &&  digitalRead(blockdetector6Pin) == HIGH) // read two switches

      digitalWrite(greenledPin10,LOW);

  else

  {

      digitalWrite(greenledPin10, HIGH);

   }

  //CCW Block 5 Yellow

  // read from the button pin

            digitalRead(blockdetector5Pin);

      digitalRead(blockdetector6Pin);

  if (digitalRead(blockdetector5Pin) == HIGH  &&  digitalRead(blockdetector6Pin) == LOW)

      digitalWrite(yellowledPin11,LOW);

  else

  {

      digitalWrite(yellowledPin11, HIGH);

   }

    //CCW Block 5 Red

  // read from the button pin

      digitalRead(blockdetector5Pin);

      if (digitalRead(blockdetector5Pin) == LOW)

      digitalWrite(redledPin12,LOW);

  else

  {

      digitalWrite(redledPin12, HIGH);

          }

  //==================================================================

  {

  // CCW Block 7 Green

    // read from the button pin

      digitalRead(blockdetector7Pin);

      digitalRead(blockdetector8Pin);

          if (digitalRead(blockdetector7Pin) == HIGH  &&  digitalRead(blockdetector8Pin) == HIGH) // read two switches

         digitalWrite(greenledPin13,LOW);

  else

  {

      digitalWrite(greenledPin13, HIGH);

   }

  //CCW Block 7 Yellow

  // read from the button pin

            digitalRead(blockdetector7Pin);

      digitalRead(blockdetector8Pin);

  if (digitalRead(blockdetector7Pin) == HIGH  &&  digitalRead(blockdetector8Pin) == LOW)

      digitalWrite(yellowledPin14,LOW);

  else

  {

      digitalWrite(yellowledPin14, HIGH);

   }

    //CCW Block 7 Red

  // read from the button pin

      digitalRead(blockdetector7Pin);

       if (digitalRead(blockdetector7Pin) == LOW)

      digitalWrite(redledPin15,LOW);

  else

  {

      digitalWrite(redledPin15, HIGH);

          }

  } 

  // =============================================================

{

  // CCW Block 8 Green

  // read from the button pin

      digitalRead(blockdetector8Pin);

      digitalRead(blockdetector9Pin);

         if (digitalRead(blockdetector8Pin) == HIGH  &&  digitalRead(blockdetector9Pin) == HIGH) // read two switches

         digitalWrite(greenledPin16,LOW);

  else

  {

      digitalWrite(greenledPin16, HIGH);

   }

  //CCW Block 8 Yellow

  // read from the button pin

    { 

      digitalRead(blockdetector8Pin);

      digitalRead(blockdetector9Pin);

  if (digitalRead(blockdetector8Pin) == HIGH  &&  digitalRead(blockdetector9Pin) == LOW)

      digitalWrite(yellowledPin17,LOW);

  else

  {

      digitalWrite(yellowledPin17, HIGH);

   }

{

    //CCW Block 8 Red

  // read from the button pin

      digitalRead(blockdetector8Pin);

       if (digitalRead(blockdetector8Pin) == LOW)

      digitalWrite(redledPin18,LOW);

  else

  {

      digitalWrite(redledPin18, HIGH);

         }

  // =============================================================================

  // CW

  // =============================================================================

    {

  // CW Block 1 Green

    // read from the button pin

      digitalRead(blockdetector1Pin);

      digitalRead(blockdetector12Pin);

              if (digitalRead(blockdetector1Pin) == HIGH  &&  digitalRead(blockdetector12Pin) == HIGH) // read two switches

         digitalWrite(greenledPin25,LOW);

  else

  {

      digitalWrite(greenledPin25, HIGH);

   }

  //CCW Block 1 Yellow

  // read from the button pin

            digitalRead(blockdetector1Pin);

      digitalRead(blockdetector12Pin);

  if (digitalRead(blockdetector1Pin) == HIGH  &&  digitalRead(blockdetector12Pin) == LOW)

      digitalWrite(yellowledPin26,LOW);

  else

  {

      digitalWrite(yellowledPin26, HIGH);

   }

    //CCW Block 1 Red

  // read from the button pin

      digitalRead(blockdetector12Pin);

       if (digitalRead(blockdetector12Pin) == LOW)

      digitalWrite(redledPin27,LOW);

  else

  {

      digitalWrite(redledPin27, HIGH);

            }

  // ==============================================================

      {

  // CCW Block 12 Green

    // read from the button pin

      digitalRead(blockdetector12Pin);

      digitalRead(blockdetector11Pin);

            if (digitalRead(blockdetector12Pin) == HIGH  &&  digitalRead(blockdetector11Pin) == HIGH) // read two switches

         digitalWrite(greenledPin28,LOW);

  else

  {

      digitalWrite(greenledPin28, HIGH);

   }

  //CCW Block 12 Yellow

  // read from the button pin

            digitalRead(blockdetector12Pin);

      digitalRead(blockdetector11Pin);

  if (digitalRead(blockdetector12Pin) == HIGH  &&  digitalRead(blockdetector11Pin) == LOW) //read two switches

      digitalWrite(yellowledPin29,LOW);

  else

  {

      digitalWrite(yellowledPin29, HIGH);

   }

    //CCW Block 12 Red

  // read from the button pin

      digitalRead(blockdetector12Pin);

       if (digitalRead(blockdetector12Pin) == LOW)

      digitalWrite(redledPin30,LOW);

  else

  {

      digitalWrite(redledPin30, HIGH);

        }

         }

  // ==================================================================

  {

  // CCW Block 7 Signal 8 Green

    // read from the button pin

      digitalRead(blockdetector7Pin);

      digitalRead(blockdetector6Pin);

           if (digitalRead(blockdetector7Pin) == HIGH  &&  digitalRead(blockdetector6Pin) == HIGH) //read two switches

         digitalWrite(greenledPin31,LOW);

  else

  {

      digitalWrite(greenledPin31, HIGH);

   }

  //CCW Block 7 Signal 8 Yellow

  // read from the button pin

            digitalRead(blockdetector7Pin);

      digitalRead(blockdetector6Pin);

  if (digitalRead(blockdetector7Pin) == HIGH  &&  digitalRead(blockdetector6Pin) == LOW)

      digitalWrite(yellowledPin32,LOW);

  else

  {

      digitalWrite(yellowledPin32, HIGH);

   }

    //CCW Block 7 Signal 8 Red

  // read from the button pin

      digitalRead(blockdetector7Pin);

        if (digitalRead(blockdetector7Pin) == LOW)

      digitalWrite(redledPin33,LOW);

  else

  {

      digitalWrite(redledPin33, HIGH);

          }

    }}

  // ================================================================

{

  // CCW Block 6 Signal 7 Green

    // read from the button pin

      digitalRead(blockdetector6Pin);

      digitalRead(blockdetector5Pin);

            if (digitalRead(blockdetector6Pin) == HIGH  &&  digitalRead(blockdetector7Pin) == HIGH) // read two switches

         digitalWrite(greenledPin34,LOW);

  else

  {

      digitalWrite(greenledPin34, HIGH);

   }

  //CCW Block 6 Signal 7 Yellow

  // read from the button pin

            digitalRead(blockdetector6Pin);

      digitalRead(blockdetector5Pin);

  if (digitalRead(blockdetector6Pin) == HIGH  &&  digitalRead(blockdetector5Pin) == LOW)

      digitalWrite(yellowledPin35,LOW);

  else

  {

      digitalWrite(yellowledPin35, HIGH);

   }

    //CCW Block 6 Signal Red

  // read from the button pin

      digitalRead(blockdetector6Pin);

       if (digitalRead(blockdetector6Pin) == LOW)

      digitalWrite(redledPin36,LOW);

  else

  {

      digitalWrite(redledPin36, HIGH);

          }

  }}}}}}

 

 


We hope that you found the information in this page useful. The internet is all about getting and giving. And giving back is the best. Please donate $1 or more to aid us in our continuing quest to add to and polish the information in our site.


Scroll to top