Multitasking or MultiThreads with SPLat as an example

I chuckle when I see comments like, "What are you doing that needs multitasking?" Unless you've tried it, you can't come close to appreciate the programme design power in your hands. Let me explain.

SPLat Controls in Australia have a multitasking technology that is quite simple to use with their controllers. Assume you have two independent programmes: one called pushbutton and the other FillControl. The first simply monitors a group of pushbuttons and sets flags to be read by other programmes. The second has a focus on filling a tank as a function of the pushbutton flags. You could say they are functions but actually the descriptive, "TASK" is more applicable.

The code:
Launchtask Pushbutton
LaunchTask FillControl
RunTasksForever

Pushbutton
YieldTask
......
......
goto Pushbutton
FillControl
......
wait
......
goto FillControl

If there is a timing event or even a pause, there is no effect on this technique. The controller will check the timing or pause condition, if it hasn't been met, it will leave the task and execute the next task at the point it had left off. If that second task has a timing or pause condition and its conditions hasn't been met, it leaves that task and returns to the first where it left off. If there are no timing or pause conditions in the task, the first line in the task should include yieldTask to exit the task. Seems odd but when the controller returns, it starts off where it left off.... skips the leave statement, executes the task, returns to the top and leaves. Of course, under the hood we have files points and such that we need not have to worry about. Of course, you could have more than two tasks. Imagine 20 tight small programmes easy to maintain or troubleshoot. You could comment out a launch line quite easily. Or redirect to a test task.

I love the concept and used their product a number of times. It reduces programming and fits nicely into object programming techniques. They do have a low cost controller called EC1 you could try out. Hope they still have it.

I would love to use Arduino in a similar fashion. Mega2560 could have the power to do it. Anyone?

PS.... I wrote the above for a reply to a similar inquiry for Mulitasking. I felt it could be used to start a new thread.

Thanks, @TorontoFred. It's always an interesting discussion. Of course multitasking is great. But you have to consider the implementation details and the software environment. Standardization and resource allocation are two large obstacles to the adoption of pre-emptive multitasking on the Arduino. You have to consider that there is no kernel or BIOS in the Arduino world. Also, many users are beginners and want to learn simple, basic code. For many, the use of library functions is a challenge. In a way, it is great that they can design programs that are completely stand alone, because almost everything is out in the open and can be seen. Also, round-robin multitasking can do most of the things that a pre-emptive system can (in this limited hardware environment). Let the fun begin. There are a lot of opinions on this subject.

Why write point less bloatware? or is this really just an Ad for some ones pointless product?

Mark

Yes, I really hope this isn't a shill.

Torontofred:
Assume you have two independent programmes: one called pushbutton and the other FillControl. The first simply monitors a group of pushbuttons and sets flags to be read by other programmes. The second has a focus on filling a tank as a function of the pushbutton flags.

Sounds very like the system in Several Things at a Time and in Planning and Implementing a Program

...R

I use this RBD::Timer library. It only uses millis, not delay or interrupts to track time so it's non-blocking. Kind of in the manner you described as wanting to find; you can use this to fire events when running an Arduino loop as a state machine.

The magic happens with onRestart being a self-resetting function. It will only return true once, when the state changes. This allows the programmer to work with isolated single events inside of a state machine instead of having to manage the flood of loops happening outside of this function. It's advanced enough to control your entire sketch program flow, but simple enough to get started with because it's in plain english.

Here is an example of firing an event every 3 seconds:

#include <RBD_Timer.h> // https://github.com/alextaujenis/RBD_Timer

RBD::Timer timer;

void setup() {
  timer.setTimeout(3000);
}

void loop() {
  if(timer.onRestart()) {
    // run code once each event
  }
}

Or use it to tween values over time:

#include <RBD_Timer.h> //https://github.com/alextaujenis/RBD_Timer
#include <RBD_Light.h> //https://github.com/alextaujenis/RBD_Light

RBD::Timer timer;
RBD::Light light(13);

void setup() {
  timer.setTimeout(3000);
}

void loop() {
  if(timer.isActive()) {
    light.setPwmPercent(timer.getPercentValue());
  }
  else {
    timer.restart();
  }
}

The other libraries in this framework are sprinkled with the self-resetting functions (events) as well, like motor.onTargetSpeed() or human_sensor.onTouch().