Using a MEGA to run multiple programs.

I am an old hardware guy and am just starting to learn the Arduino software. I am building a model RR and it will have multiple sections with sensors. One sensor will be used to start a program when a train enters that section and other sensors at each end of the section will indicate direction. Finally the train will be stopped or continue depending on whether or not the following section is occupied.

My question is: Can I accomplish this for multiple programs that will run independent of each other with a single MEGA?

I do have a number of Uno's available that can be used but in the interest of conserving space, I thought the MEGA might be an answer.

I can always build the logic needed to do this but it would be inflexible and the Arduino has peaked my interest in programming which is something that I vowed I would never do. Never say never!

Thanks for any ideas and/or criticism.

Sure You can do. Search for "Doing several things at the same time" by Robin2.

Welcome to the 'old hardware guy' club. There seem to be many old farts hanging out here.

Most Arduinos have a single core, so can only do one thing at the time.
But it can do multiple things so fast that for the user it seems to happen at the same time.
I suppose you're not talking about a 'real' bullet train.

The trick is to not use blocking code, like delay() and while().
For interlaced multi-tasking, the loop() should continue to loop without any hold-ups.

The first thing to do is to try to understand the BlinkWithoutDelay sketch in the examples of the IDE.
It's as logical as making your breakfast in the morning.
You don't wait for the toaster, or the coffee machine, or stare at your eggs boiling.
You just use the clock in a clever way, so everything is ready at the same time.

Don't see why a Mega is 'conserving space'. It's the largest one of all boards.
Leo..

Before you even begin, seriously consider track current based occupancy detection. It's more flexible, reliable and authentic. "pass by" detectors depend on simulation of block occupancy, so there are numerous situations where it is either awkward or will flat out fail.

Thanks for the reply. And don't be too free with the "old fart" thing-I am only 84! Univac & Control Data hardware customer service!

You seem to be a RR guy so here is my deal! 9 blocks, 3 or more trains and each block has current detector and an IR at the beginning and end. Also can use reed switches wherever I deem necessary.

I thought something like "if-else" and run code for the occupied block if=true. else check next block for occupied status. Etc, etc. Have to do some more thinking and probably incorporate interrupts too.

Being as stubborn as I am, that is what I want to try.

Thanks again!
Allan

Don't think about interrupts. Loop is running that fast, it will easily pick any change on the tracks.

I offer Mega Screw terminals shields if you need a handy way to connect up discrete wires.
The very first one I sold went to a model railroader.

@CrossRoads
Brilliant stuff. Do You such boards for my army of UNOs?

Railroader:
Don't think about interrupts.

+1
Generally speaking, interrupts are for things that must happen in microseconds.
Avoid for things that happen in milliseconds, and for bouncing switches.
Leo..

Yes I do.

Prices for both at my webste, http://www.crossroadsfencing.com/
(which I have been meaning to organize a little better, but haven't managed to yet)

Railroader:
Don't think about interrupts. Loop is running that fast, it will easily pick any change on the tracks.

My intention is to activate a subroutine when a train enters a block and test the following block when the train reaches either end.

I can activate the routine with the current sensor but the if I wait for the train to reach the end of the block before testing the following block, I tie up he CPU with a wait statement.

Therefore, I thought of: 1-activating the routine for the block with the current sensor, 2-determine the direction of travel with the IR that was just encountered and then, 3-using an interrupt when the train gets to the 2nd IR in order to test the following block.

Thanks for you input...….Allan

@CrossRoads
Oh, You have a supemarket of boards. impressive and interesting. You oftenly use quality sockets before the cheap ones.

mhcomp:
My intention is to activate a subroutine when a train enters a block and test the following block when the train reaches either end.

I can activate the routine with the current sensor but the if I wait for the train to reach the end of the block before testing the following block, I tie up he CPU with a wait statement.

Therefore, I thought of: 1-activating the routine for the block with the current sensor, 2-determine the direction of travel with the IR that was just encountered and then, 3-using an interrupt when the train gets to the 2nd IR in order to test the following block.

Thanks for you input...….Allan

I asure You that loop() will pick up such a change something like thousands of times per second.

Yes, the Made in Germany screw terminals are very nice.

Wawa:
+1
Generally speaking, interrupts are for things that must happen in microseconds.
Avoid for things that happen in milliseconds, and for bouncing switches.
Leo..

That's fine because I will have to store the input and use it to determine if the next block is still busy.

Railroader:
I asure You that loop() will pick up such a change something like thousands of times per second.

Yes, I know the program runs a lot faster than the RR ( the last system I worked on we were dealing in nanoseconds ) but I can't wait for one input while there may be other incidences of trains waiting to enter other blocks so the only way I see is to use an interrupt that will let me know when the second sensor in the block has been tripped.

And I do have a few Uno development boards.

Use a state machine. When the first event has occured, advance the state to the next dtate. That state will execute some code when the terms are fullfilled, else just do nothing.

mhcomp:
...but I can't wait for one input while there may be other incidences of trains waiting to enter other blocks...

But you don't wait for something to happen.
You just look if something has happened when you test in the next loop().
The StateChangeDetection example in the IDE is the next thing to explore.

What happens in a RR on a nanosecond timeframe?
Leo..

Railroader:
Don't think about interrupts. Loop is running that fast, it will easily pick any change on the tracks.

Well after studying the problem again, I think you may have a good point that what I need to do will run a lot faster than the train so I can just stack a bunch of if or if/else statements, along with the judicious use of variables, to accomplish what needs to be done without interrupts.

Keeps it a lot simpler too!

Thanks...…….Allan

Wawa:
But you don't wait for something to happen.
You just look if something has happened when you test in the next loop().
The StateChangeDetection example in the IDE is the next thing to explore.

What happens in a RR on a nanosecond timeframe?
Leo..

I was talking about the last computer system I worked on. In fact, it was the world's first "super computer", the Control Data 6600.