Using multiple Mega controllers on a large model railway

Hello,
I am designing a large model railway in a barn measuring 30 feet by 30 feet. It will encompass two large terminal stations, two large through stations and several intermediate stations.

I intend to use multiple Mega controllers at each terminus as I anticipate some 50+ track locations and 40+ signals in each terminus, its approach and yard.

After I have programmed a controller, I want to move it to its permanent location and then program another one, and so on.

If I bring a controller back in to the computer from the layout, I will want the IDE to identify the board and show its configuration and settings. If there is a way to set something that would, for example, define one board as number 1, then define number 2, and so on, that is what I am asking about.

Is there a way to have boards defined or setup this way? I'm suspecting there must be, but just can't find anything explaining how this is done without possibly damaging the board.

As an FYI, I am new to Arduino so will be slowly building up over the coming year or so. Plus, I don't see any need for my multiple Megas to be wired together, I just anticipate that I will need 5 or 6 of them to run the railway and power the lighting on my control panels.

Many thanks in advance and apologies if this has already been answered somewhere.

Best wishes
Michael

Do these Mega controllers all run the same code? You can store a node ID in the internal EEPROM to allow each node to identify itself for the purposes of addressing them on a wired/wireless network from the main controller.

1 Like

You could have details in setup that print to the serial monitor when connected . You can stick a coloured dot onto the processor to identify it ( works very well )
Maybe you could have identical software on all and set specific options with digital inputs ?

The IDE won’t identify the board , unless you somehow mess with its processor signature , which will/may cause problems in itself .

1 Like

Put an ID in EEPROM. Oh, someone already said that.

1 Like

Ideally, every Mega will run much the same code as @markd833 mentioned. The different things that they do will be data driven. That data can be displayed through serial, including a hard coded ID.

That's probably an aspirational goal though. As you program a Mega for each terminus, you'll start to see commonalities, but initially I expect that there will be a lot of terminus specific hard coding in each one.

Look at arrays, structs and arrays of structs. They're likely to be very useful, even if they seem tricky at first.

1 Like

Certainly, I'd like to have as much in common as possible to make coding easier. So let's start with the termini.

If I have, for example, six terminus platforms plus another 20 or so sidings, I'd need 26 IR sensors and 26 LEDs on my control panel. Then, when a track is occupied the LED would illuminate and stay illuminated until the train vacates the section.

So presumably I'd write the code once and wire up the necessary IR sensors and LEDs?

Tell me, do the Megas remember their programming when the power is turned off and back on again, or do they need to remain powered continuously?

Ideally, I'd like to turn off the power after an operating session then turn it back on and have all the sensors identify which tracks are occupied and illuminate the required LEDs on the panel.

Yes I had already determined that I probably should not mess around with the internal settings. I've seen some posts where folks have messed up their boards doing this.

Perhaps, as you suggest. simply putting a number or a blob of paint on a board will suffice.

May seem a silly question, but presumably when a board is connected to the PC, the IDE reads the instructions from the board and displays them?

Yes, the program (aka sketch) is remembered in the internal flash memory of the device. You also have EEPROM memory in the device where you can store device specifics (such as a unique ID) and other configuration data that will be remembered when power is removed. Be careful with this memory as it has a limited amount of writes - around 10,000 (or is it 100,000?). Then there's RAM which is used for your general program variables etc - which gets lost when power is removed.

Do you have a controller device in mind to oversee all of this?

1 Like

Yes, but be careful with all those LEDs. Presumably your IR sensor is one of those reflective ones that sends out IR light and detects if any is bounced back off an object? Each one of those has an LED to generate the IR light, so 26 IR LEDs + 26 visible LEDs on your control panel takes a fair bit of power. Of course, not all the visible LEDs may be on at once but plan for the worst case.

With the control panel LEDs, rather than using one digital output per LED, you can make the setup a lot simpler if you use a MAX7219 to drive them. It can drive up to 64 LEDs and does all the hard work for you. And it's available in a nice big DIL package!

1 Like

No. The program you write exists as one or more text files on your PC. The IDE invokes a compiler for you to translate your code into binary and sends it to the Arduino.

That binary code will persist on the Arduino until you overwrite it and will survive power cycling.

Be aware though that any data that your program has gathered by reading sensors will vanish when the power is lost. For example, if the Arduino saw a sensor at the entry to a siding indicate that rolling stock went in, that knowledge will be lost when it is switched off. EEPROM or FRAM or SD cards are ways to ensure that that information is retained.

1 Like

That doesn't require any memory. The system is always able to read track occupancy. In fact it should always be doing it.

That is not the case with certain optical sensor systems, where track occupancy is calculated from entry/exit events. It is a good reason to use track current sensing, or a combination of track current and optical sensing.

Actually, I would expect a system of your expected complexity to be a logic implementation nightmare using optical sensors alone.

1 Like