I have 2 programs made for my arduino.The first one is for solving a maze automatically and the second one is for controlling the robot with a android app.I wanted to control the robot with the app I made as soon as it has finished solving the maze. Is it possible to upload both the program in the arduino and run it one at a time?If so can you tell me how?
-
Why don't you make just one program to run all?Butzke– Butzke2015年10月28日 09:25:21 +00:00Commented Oct 28, 2015 at 9:25
-
Never mind I just found the solution.Thank you for your reply though.U Zuno Kunno– U Zuno Kunno2015年10月28日 09:35:23 +00:00Commented Oct 28, 2015 at 9:35
-
If you know the solution you should post it as an answer.Majenko– Majenko2015年10月28日 09:53:26 +00:00Commented Oct 28, 2015 at 9:53
-
The answer is a little bit complicated.I'll post it as soon as possible.U Zuno Kunno– U Zuno Kunno2015年10月28日 13:35:49 +00:00Commented Oct 28, 2015 at 13:35
1 Answer 1
Once I made a sketch to run multiple programs. I wrote a setup
and loop
function for every different "program", (e.g. setup_p1
, setup_p2
, ... loop_p1
, ...) and then wrote a simple main
sketch to handle them all.
In my application I choose the program at startup with a 3-dipswitch, but you can easily switch this to allow "on the fly" switching.
I choose to use a callback because it's faster than just checking the mode every time, at least in my case
// callback for loop function
typedef void (*loopfunction)();
loopfunction currentloop;
void setup()
{
// Set dip-switch pins as input
pinMode(MODE_PIN2,INPUT_PULLUP);
pinMode(MODE_PIN1,INPUT_PULLUP);
pinMode(MODE_PIN0,INPUT_PULLUP);
// Concatenate the dip-switch values
byte mode = 7 - ((digitalRead(MODE_PIN2) << 2) | (digitalRead(MODE_PIN1) << 1) | digitalRead(MODE_PIN0));
// choose the correct mode
switch(mode)
{
case 0: // Shutdown - do nothing
break;
case 1: // Program 1
setup_p1();
currentloop = loop_p1;
break;
case 2: // Program 2
setup_p2();
currentloop = loop_p2;
break;
...
}
// Not a valid program: halt
if (!currentloop)
{
while(1);
}
}
void loop()
{
// Execute the current loop
currentloop();
}