Two sketches o one Mega at the same time ? Possible or not ?

Good day to our forum !

I have a question that might sound silly.

I have an arduino mega board and two sketches. The sketches are almost identical, they use the same variables, pins etc. but the code is a little different.

If it is possible i would like to have them both uploaded at the same time but be able to selec which one to be executed (for example using a switch) as if the other one is not present on the Mega chip.

Is it possible ?

I thought i could merge the sketches and find a way to tell which one to run using if statement
for example :

if if (digitalRead(switch) == HIGH)
{
......
}
else
{
}

But the problem is that in both sketches i use the SerialEvent function and it will just not work...

Thank you !

Is it possible ?

No.

But usually it's quite simply to combine the two sketches into one and do the selection of the algorithms there.

If you need help with that you have to show us your sketches.

Thank you.

I am not sure i want to complicate things that much in my original sketch. I might use two different chips or not do it at all.

Ill get back with the needed information after i make up my mind.

Have a nice day !

Classic x-y problem.

You need to explain, in detail, what you want to achieve rather than asking for help with an impractical or impossible solution.

You should begin by posting both programs. Use the </> icon or surround the individual sketches with code tags like this:

[code] your code here [/code]

Use switch:case format for your sketch.

byte switchPin = 2; // pin a pin that is not a Tx/Rx pin
byte switchState;

void setup(){
pinMode (switchPin, INPUT_PULLUP); // toggle pin connects pin to Gnd, or leaves it open, internal pullup resistor
}

void loop(){
switchState = digitalRead(switchPin);
   switch(switchState){
   case 0:
   // one set of code
   break;
   case 1:
   // the other set of code
   break;
   }
}

Then since only one or the other is running at any one time they cannot interfere with each other.
https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

HellasT:
I am not sure I want to complicate things that much in my original sketch. I might use two different chips or not do it at all.

If they are nearly the same, it should be no trouble at all. Most of us do that all the time. It’s called editing. :sunglasses:

Do make up your mind. :grinning:

WattsThat:
Classic x-y problem.

You need to explain, in detail, what you want to achieve rather than asking for help with an impractical or impossible solution.

You should begin by posting both programs. Use the </> icon or surround the individual sketches with code tags like this:

[code] your code here [/code]

Please accept my apology.
I should have explained in post#1 but looking back at my first post i now realize that i gave you unusable information about what i was thinking to do.
I posted this question because from previous experience i had with other type of mcu’s there was separate storage space for 2nd sketch and i thought that arduinos might have a similar option. Since they do not, it is now clear to me that if i am to do it then i will have to edit my code and upload it as 1 sketch.

I appreciate your will to help me. I know that you people come across silly and pointless questions all day long and it was not my intention to clutter the forum with one more.

CrossRoads:
Use switch:case format for your sketch.

byte switchPin = 2; // pin a pin that is not a Tx/Rx pin

byte switchState;

void setup(){
pinMode (switchPin, INPUT_PULLUP); // toggle pin connects pin to Gnd, or leaves it open, internal pullup resistor
}

void loop(){
switchState = digitalRead(switchPin);
  switch(switchState){
  case 0:
  // one set of code
  break;
  case 1:
  // the other set of code
  break;
  }
}



Then since only one or the other is running at any one time they cannot interfere with each other.
https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

Thank you for the example ! I will consider this option.

Paul__B:
If they are nearly the same, it should be no trouble at all. Most of us do that all the time. It’s called editing. :sunglasses:

Do make up your mind. :grinning:

HellasT:
Please dont get me wrong, i appreciate your will to help me.It is not really an XY problem besides i already mentioned in my post #3 that i have not decided what i want to do just yet.

I posted this question because from previous experience i had with other type of mcu’s there was separate storage space for 2nd sketch.

I thought that arduinos might have a similar option.

Thank you for the example ! I will consider this option.

Paul__B:
If they are nearly the same, it should be no trouble at all. Most of us do that all the time. It’s called editing. :sunglasses:

Do make up your mind. :grinning:

Yes i have to make up my mind before i ask further :slight_smile:

HellasT:
I posted this question because from previous experience i had with other type of MCUs there was separate storage space for 2nd sketch

I rather doubt that. Perhaps you need to explain this more carefully and we may be able to sort it out?

HellasT:
I posted this question because from previous experience i had with other type of mcu's there was separate storage space for 2nd sketch and i thought that arduinos might have a similar option.

Perhaps you mean the boot loader program and the user sketch it can load after every Reset? Then your sketch already is the 2nd one.

Paul__B:
I rather doubt that. Perhaps you need to explain this more carefully and we may be able to sort it out?

Well it is not really a 2nd program memory rather than one program memory divided in "slots". I do not really want to name a specific microcontroller because i do not want to "advertise" it.

DrDiettrich:
Perhaps you mean the boot loader program and the user sketch it can load after every Reset? Then your sketch already is the 2nd one.

Actually i think the booloader gives that ability. when writing the sketch you can declare the slot number you want it to be stored at. On the code that will be loaded on the main program slot you can use a run command to switch to the program slot you desire.

I'm extremely curious about that other microcontroller. Don' worry about advertising, most of us will not run away from Arduino.

But I think you do not really talk about the microcontroller itself but a simple OS that is implemented on it that will allow you to upload and select multiple sketches.

In simple terms, the OS on the Arduino is the bootliader that allows you to load a sketch and run a sketch.

HellasT:
Well it is not really a 2nd program memory rather than one program memory divided in "slots". I do not really want to name a specific microcontroller because i do not want to "advertise" it.

I kind of thought as much.

This refers to memory "paging" where the addressing system cannot directly handle all of the available memory. It is used in various microprocessors including (some of) the PICs IIRC, and the original 8086 used in the IBM PC, though largely superseded (I think!)

This of course, has nothing to do with loading multiple programs but others have mentioned the concept of "operating systems" which permit separate programs to operate on a time-sharing process in order to appear to operate simultaneously. This necessarily results in constraints on the use of shared resources.

That aside, it does not seem to be what you are wanting at all. You original concern was to use two alternate - not simultaneous - codes performing similar functions using the same resources. You could as described, put the setup() code of each together into the single setup() section using a switch ("if -- else" statement) to determine which is actually executed, and arrange the single loop() function in the same manner with the same switch. It is not difficult at all.

The limitation is that it is wasteful of code space. If that matters, you need to do what I first advised and fully merge the two. At each point where they differ, consider why they differ with the possibilities that they should not actually differ, or that they do completely different things in which case this part of the code needs to be divided by an "if -- else" statement or that they merely use different parameters which can be set as variables at setup() time.

In the process, you may well find that the discipline of merging the alternate actions results in some simplifications which you missed seeing in the original version of either. :grinning:

sterretje:
I'm extremely curious about that other microcontroller. Don' worry about advertising, most of us will not run away from Arduino.
But I think you do not really talk about the microcontroller itself but a simple OS that is implemented on it that will allow you to upload and select multiple sketches.
In simple terms, the OS on the Arduino is the bootliader that allows you to load a sketch and run a sketch.

Thats right. Now if you really insist on me telling you which one it is then i guess i culd if the moderators/administrators are okay with this. Trust me though i am certain you will not leave arduino !

Paul__B:
it does not seem to be what you are wanting at all. You original concern was to use two alternate - not simultaneous - codes performing similar functions using the same resources. You could as described, put the setup() code of each together into the single setup() section using a switch ("if -- else" statement) to determine which is actually executed, and arrange the single loop() function in the same manner with the same switch. It is not difficult at all.

The limitation is that it is wasteful of code space. If that matters, you need to do what I first advised and fully merge the two. At each point where they differ, consider why they differ with the possibilities that they should not actually differ, or that they do completely different things in which case this part of the code needs to be divided by an "if -- else" statement or that they merely use different parameters which can be set as variables at setup() time.

In the process, you may well find that the discipline of merging the alternate actions results in some simplifications which you missed seeing in the original version of either. :grinning:

Yes that also right.

Bottom line is that i can upload only one sketch at a time or i have to write the code in such way that only the part i need wil run when i needed. All the above by practicing the methods you mentioned.

THANK YOU SO MUCH ALL OF YOU !

HellasT:
That's right. Now if you really insist on me telling you which one it is then I guess I could if the moderators/administrators are okay with this. Trust me though I am certain you will not leave Arduino!

Rest assured that there is no censorship or suggestion that it is not perfectly appropriate to discuss the relative merits of different named microcontroller families. :grinning:

The only thing that draws criticism is discussion of a project which is based on another microcontroller with no connection whatsoever with an Arduino-like system. On the other hand, that becomes a compliment of sorts, implying that this - Arduino - is the forum group where you get the most competent answers. :sunglasses: