.

Welcome to My HO Model Railroad Blog

February 3, 2017 Arduino MEGA Signal Controller

The Arduino's are fascinating creatures!  This post is about using an Arduino MEGA 2560 as a Signal Controller.  The MEGA has 54 I/O ports and 16 analog input ports.  My Arduino sketch will drive 16 LED 3 color signal heads using 48 outputs.  The 16 analog inputs will handle the outputs from 16 occupancy detectors or block controls.

Its rather easy to drive a signal system by using a simple truth table for each signal head color.

Green = advance block high + signal block high = Green on
              else green off

Yellow = advance block low + signal block high = Yellow on
              else yellow off

Red= signal block low = Red on

The info above is for one direction (CCW), a separate code is required for reverse direction (CW) simply by reversing the order of the block numbers.  For my layout I used CCW (Counter Clock Wise) for West and CW (Clock Wise) for East.


Below is my Arduino MEGA 2560 Sketch for the controller.  The setup definitions are for a total of 16 blocks.  I only listed two CCW blocks for this post, additional blocks can be added with copy and paste and changing to block and signal head numbers for the proper sequence up to 16 blocks.


========================================================================

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);
       
  }

  
  
   }

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



No comments: