running more than one program

Hi All,

since only one program can be loaded into an Arduino Uno at one time, how could you run differents sets of code depending on what the value of some pin was?

Cheers
David

int pin = ??;  // make ?? some pin
if (digitalRead(pin)==HIGH) {
  // execute this code
} else {
  // execute this code
}

Of course I have recently mentioned the Quantum Leaps Framework -- and the there is FreeRTOS -- I saw a recent claim that it might work on a MEGA2560. If I get ambitious I will let you know.

Quantum:

He has an implementation for the Mega2560 as I recall.

I will look at it any-day-real-soon-now.

Is this how you would do it?

void loop()
{
if ()
// do this
else
// do this
}

so it has to repeatedly do this check this if statement over and over again.

Cheers
David

dharr19:
so it has to repeatedly do this check over and over again.

When powered and not held in RESET, microcontrollers are always doing something. Might as well be checking a pin.

dharr19:
so it has to repeatedly do this check over and over again.

Not necessarily, but why is that a problem?

dharr19:
Hi All,

since only one program can be loaded into an Arduino Uno at one time, how could you run differents sets of code depending on what the value of some pin was?

Cheers
David

Presumably during setup, you do a read of the pin, and save away the state, and then in loop, you test that state. If you want to be fancy, you move each program into a separate function, and then use a function pointer to call the appropriate function inside of loop.

Something like:

void cherry_init (void)
{
    // init for cherry
}

void cherry_loop (void)
{
    // loop for cherry
}

void chocolate_init (void)
{
    // init for chocolate
}

void chocolate_loop (void)
{
    // loop for chocolate
}

void vanilla_init (void)
{
    // init for vanilla
}

void vanilla_loop (void)
{
    // loop for vanilla
}

void (*flavor_loop) (void) = vanilla_loop;

const int cherry_pin = 2;
const int chocolate_pin = 3;

void setup (void)
{
    // setup pins 2 and 3 as pull-up pins
#if defined(ARDUINO) && ARDUINO >= 100
    pinMode (cherry_pin, INPUT_PULLUP);
    pinMode (chocolate_pin, INPUT_PULLUP);
#else
    pinMode (cherry_pin, INPUT);
    pinMode (chocolate_pin, INPUT);
    digitalWrite (cherry_pin, HIGH);
    digitalWrite (chocolate_pin, HIGH);
#endif

    if (digitalRead (cherry_pin) == LOW) {
        cherry_init ();
        flavor_loop = cherry_loop;

    } else if (digitalRead (chocolate_pin) == LOW) {
        chocolate_init ();
        flavor_loop = chocolate_loop;

    } else {
       vanilla_init ();
    }
}

void loop (void)
{
    // call appropriate flavor, depending on pins 2 and 3 at startup
    flavor_loop ();
}

You can get even fancier using C++ features of virtual functions and super-classes, where you have a base type with a loop virtual function, and each superclass then defines its loop. But they are essentially extensions of function pointers.

Yup -- that's the way computers work all right -- in polled mode. But that is the programmer -- not the machine. My Arduino definitely has Interrupts available. (Mega 2560)

Are you looking for an Interrupt Driven Real Time Operating System?

The framework I mentioned and the FreeRTOS will both meet your (unstated) requirements.

Now what exactly do you want?

This inquiry is pretty vague.

That would be ok if a switch could cause a change while the program is running.

Cheers
David

WillR:
The framework I mentioned and the FreeRTOS will both meet your (unstated) requirements.
http://www.freertos.org/

I assume that the FreeRTOS framework would not be compatible with the Arduino IDE environment.

WillR

WillR:
Yup -- that's the way computers work all right -- in polled mode. But that is the programmer -- not the machine. My Arduino definitely has Interrupts available. (Meg

WillR:
Yup -- that's the way computers work all right -- in polled mode. But that is the programmer -- not the machine. My Arduino definitely has Interrupts available. (Mega 2560)

Are you looking for an Interrupt Driven Real Time Operating System?

The framework I mentioned and the FreeRTOS will both meet your (unstated) requirements.
http://www.freertos.org/

Now what exactly do you want?

This inquiry is pretty vague.

What I wanted.

Originally, I was wondering if the Arduino Uno could store more than one program at a time and found out of course that it can not. The next thought was, how do you run a specific piece of separate code without making indefinate calls to it via void loop(). I didn't see interrupts listed for the Arduino Uno and didn't get to the website you mentioned before the next comment was posted.

Are you looking for an Interrupt Driven Real Time Operating System?

The framework I mentioned and the FreeRTOS will both meet your (unstated) requirements.
http://www.freertos.org/

Now what exactly do you want?

This inquiry is pretty vague.

dharr19:
I see what WillR is talking about but the real issue is can the Arduino store more than one program.

No, not really. That's why the if-statements and functions are being given as answers.

You as the programmer would create a program that contains the code for both of your "programs".

The Arduino is based on a relatively simple microcontroller. It doesn't work like a PC. When it turns on, it starts executing code. You as the programmer have full control over how that code gets executed.

You may want to google "arduino task scheduler".

Here is one example: http://www.meatandnetworking.com/code/introducing-arduino-simple-task-scheduler/

Hi James,

I see what you mean :slight_smile:

Thanks JimEli,

JimEli:
You may want to google "arduino task scheduler".

Here is one example: http://www.meatandnetworking.com/code/introducing-arduino-simple-task-scheduler/

I will definately give this a try :slight_smile:

Well, actually there are 2 programs on a standard Arduino :

One is called Bootloader and after Reset it either receives a new sketch from the Serial line and stores it in Flash memory, or it jumps directly to what is stored in Flash.

The other one is your Arduino sketch, just surrounded by a small wrapper, which mainly once calls setup() once and then loop() forever.

You can write a different Bootloader ( e.g. loading a sketch from SD ),
and you can return to the bootloader startup point from within loop().

( A bit beyond Arduino IDE, agreed )

By that, you have something similar to a DOS, and can run different sketches that would not fit all into Flash memory at the same time.
Of course this is not optimal for task scheduling, rather to switch sketches without a connection to the Arduino IDE or a programmer.

RandallR:

WillR:
The framework I mentioned and the FreeRTOS will both meet your (unstated) requirements.
http://www.freertos.org/

I assume that the FreeRTOS framework would not be compatible with the Arduino IDE environment.

Never assume anything...

I have not tried -- I just got FreeRTOS running (correctly) on my LM3S811 -- so if I get bored -- maybe I will try my Arduino Mega 2560 -- it should work -- sort of.

You might have to change an if statement here and there.... :roll_eyes:

I would look at the State Machines Framework first though -- he has a specific version for Arduino.

See here and here:

http://www.state-machine.com/arduino/AN_Event-Driven_Arduino.pdf

hth...

I can not disagree with most of what has been said. However I could disagree with a few statements

That has little to do with the ability to run a multi-treading operating system. The 8080 was a very simple processor, probably less powerful than the ATMega328, however, it ran MP/M, a multi user operating system.

You are correct that the microcontroller has very limited resources and sharing those limited resources is more difficult

That is exactly what the PC does. When you turn it on it begins executing code. The difference is the code. Desktop and Laptop computer begin loading other another program, the operating systems, and the boot strap program then passes control to the OS. The only difference in the process is the nature of the program that is executed.

I guess there is another difference. The non-volatile code in your PC is never changed where the non-volatile code in your Arduino is changed often.

And that's the truth. For good and for bad. If something is not working as desired, you got no one to blame but yourself. But when it goes right, you get to claim most of the credit.

RandallR:
I can not disagree with most of what has been said. However I could disagree with a few statements

You took everything I said out of context.

Many new Arduino users need to make the transition from how their PC works to how an embedded microcontroller platform works. I specifically avoided all the pointless detail that you went into, to make for a change in thinking.

I had no intention of writing a Wikipedia article on the differences between an embedded microcontroller and a PC.

As I said, I can not disagree with most of what is said. Most of what has been said is correct.
My perspective is to say that the PC and Arduino are more alike than different. The biggest difference is the available resources. I also see no need to lower the bar of possibilities. It may be more difficult but Real Time processing is possible. Multi-threading can be done. Running multiple programs is a possibility. All these thing have been done in the past, they can be done now.

If someone want to run more than one program at the same time, point out the obstacles but also point to possible methods around or over those obstacles. Look at how the problems were solved in the past and at least consider tailoring a similar solution for the current environment.

You are right as well that many Arduino users are new and only have PC experience, and modern PC experience at that. They don't remember Bill Gates wondering why anyone would ever want more that 640K of memory. A question like this is an opportunity to teach. Not to spoon feed the answer but also not the discourage.

The Arduino takes me back to my first computer with only 4K of memory. It was a simpler time. It was a time when it was actually possible to know and understand all the hardware and all the software.

I apologize if stepped on some toes. My intention was not to offend. It was to spur dialog and discussion.