How to duplicate a program on one board

So I have a project for work, I need to run test stations for products we produce, I convinced the boss this was the best use of my time at the moment, finally I am getting paid for my hobby!

I need 18 free pins which means I am using the Mega board, I have written the program and it runs nicely. However one of the things that I need is for each arduino to run 2 separate bays, each running the program in isolation. That is not a problem, I have no delays, and each of my now 36 pins has a sister pin (I.e. ModeButton_A1 has a ModeButton_B2). The A1 and B2 part are just so it is easy to find and replace some parts

To make both bays work I have duplicated the sketch with an A and B sketch, like this

if (PrimeB_A1reading == HIGH) {
PrimeTimer_A1 = millis();
MPrimeFlag_A1 = 1;
PrimeFlag_A1 = 0;
ManualTimer_A1 = millis();
}

and then,

if (Prime_B2reading == HIGH) {
PrimeTimer_B2 = millis();
MPrime2Flag = 1;
Prime2Flag = 0;
ManualTimer_B2 = millis();
}

except alot more of a duplicate sketch all the way through

I also tried to plan ahead and made as many timing variables into unsigned long's so a change in one in setup will change everything in the program.

However now it has gone live I have hit a snag, some of the guys essentially beta testing my new program have asked for some changes, but this means I have to tweak the program, then copy and paste the changed parts and replace all the A1s for B2s.

Is there any way I can get around this so I can have a "master" sketch I can update that will update both my A1 and B2 sketches and then have to copy and paste them together for every tiny thing?

It depends on the secret master sketch.

You are leaning about planning ahead. Better take care of it now, as it won't be the last time.

Paul

When you start duplicating large segments of code like that, its time to look into using arrays.

if (Prime_reading[bay] == HIGH) {
  PrimeTimer[bay] = millis();
  MPrimeFlag[bay] = 1;
  PrimeFlag[bay] = 0;
  ManualTimer[bay] = millis();
}

Declare each variable as an array with as many elements as you have bays, they you change the value of bay to specify which one you are working on at any particular time.

I do not fully understand your problem, but #define could allow to change a single #define for activating one of the two versions.

94lester:
finally I am getting paid for asking others to do my job on a hobby forum

Fixed it for you...

94lester:
Is there any way I can get around this so I can have a "master" sketch I can update that will update both my A1 and B2 sketches and then have to copy and paste them together for every tiny thing?

There is an awful lot that you have not told us but my first thought is why have you got A1 and B2 sketches in the first place.

Can't you write a single program that can do either task, perhaps depending on whether one of the I/O pins is connected to 5v or to GND. In other words the program checks the status of that I/O pin at startup and then becomes either an A or a B.

If the only difference between A and B is a different list of I/O pins then put the pin numbers in 2 arrays and at startup copy the appropriate set to another array which is the one used by the working program.

If none of that seems relevant then you might consider providing a better description of the problem.

...R

twinkleyan:

finally I am getting paid for asking others to do my job on a hobby forum

Fixed it for you...

+1

...R

I'm thinking "state machine" reading this. Basic solution to a basic problem, there may be more to it. OP doesn't tell much.

94lester:
I need 18 free pins which means I am using the Mega board,/quote]

It does sound there's a long way for you to turn hobby into profession... an Uno has 20 I/O pins available (two are shared with the programming interface of course but as long as you know what you're doing you can use them as regular I/O just the same). The Nano and Pro Min have two more available even, albeit analog in only.