Move large number of leds to shift registers

I have been given a sketch that controls 3 leds (red green and yellow) on 16 signal lights or in other words 48 leds.

Due to the size of the file, I have added the ino file as an attachment.

The wiring recommendation is to custom make 3 mega extension boards.

I have zero programming ability. I have been watching enough tutorials on train control that make it appear to me that a better way to handle this is to use shift registers.

I know how to wire them after using them in another application. But changing this sketch to allow the use of shift registers is a non starter.

Is there someone who could make that change for me?

Thank you for your time.

mel_block_detection.ino (20.5 KB)

Your code really needs some arrays and for loops.

Here's what I use to drive many LEDs using shift registers: Arduino Helpers: 1.SPI-Blink.ino
It allows you to just use the familiar digitalWrite() function, so you could re-use parts of your code.

Pieter

Is there someone who could make that change for me?

If you want someone to write code for you ask a moderator (use the report to moderator button) to move this thread to the Gigs and Collaborations section. It is more likely to be seen by members that take on jobs. Expect to pay.

I see a couple of problems:

  1. You’ve got ALL the pin assignments duplicated:

int greenledPin1 = 1; // Signal Head 1 Green
int yellowledPin2 = 2; // Signal Head 1 Yellow

and neither set occurs before setup(). Move one or the other, delete the duplicates.

  1. You read a lot of pins, but do nothing with the result. Might as well delete those lines, they are not doing anything:
    digitalRead(blockdetector1Pin);
    digitalRead(blockdetector2Pin);

With a little formatting, adding { } around the if() statements, commenting out the digitalRead()s that aren’t doing anything, and deleting the { } that aren’t doing anything, your code becomes this and compiles ok:

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 A1 = D55
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

byte x;
void setup()
{
// simplify the OUTPUT and INPUT_PULLUP coding
  for (x = 1; x <43; x=x+1){
  pinMode(x, OUTPUT);
  }
  for (x = 55; x < 69; x=x+1){
  pinMode(x,  INPUT_PULLUP);
  }
}

void loop()
{
  // CCW Block 1 Green
  // read from the button pin

  if (digitalRead(blockdetector1Pin) == HIGH  &&  digitalRead(blockdetector2Pin) == HIGH) // read two switches
 { 
    digitalWrite(greenledPin1, LOW);
  }
  else
  {
    digitalWrite(greenledPin1, HIGH);
  }

// and do the same for the rest

}

I would also not use 0, 1 for any buttons or LEDs, leaving those free for Serial I/O to the PC.