[multitasking] Delay-function replacement?

hey,

i have been thinking for really long about the Delay(); function not bieng proper coding, and i am pretty much looking for an replacement of any sort to not run 100% cpu on something that just doesnt do anything.

for example, multiple time in my projects, i had to deal with how to controll the lcd AND the motors AND the user input at the same time, WHITOUT using any delays wich messes up either LCD control, motor signals, or missing the user input..

i always found my way with this, but more and more i come to realize that i want to make or find something to replace the Delay(); functions. and thus i thought, why not try some sort of virtual multithreading?

the idea of me is to basicly skip blocks of code until it is time for them to run. but i do not know what i am getting myself into as of now, and perhaps it might even already exist. therefore i'd like to hear your opinions on what you think of Delay and virtual multithreading / multitasking (if i am naming that correctly <.<)

dears,

nick

look at the blink without delay example for ideas :slight_smile:

yes i have written similar code like that blink whitout delay example. but it will be a problem if you did this in a larger scale, because of a couple of reasons;

  • "1 milli" is not always a the same amount of time. (or rather the clock cycle's are not always taking the same amount of time)
  • a separate interval can pass, while the processor is still executing code of another passed interval
  • when used in combination with comunication librarys, delays cannot be be avoided since they use delay functions themselfs (correct me if im wrong here)
  • and a whole other bunch of stuff why that isnt an efficient way to do it like that on big projects

i think we need an advanced library to handle all of the above. but will that work on arduino (lol)??

nick

nickn4:
i think we need an advanced library to handle all of the above. but will that work on arduino (lol)??

You're getting into a pretty advanced topic here, one that is sure to trip up beginners, so you will be limiting your potential audience.

MANY years ago, I helped develop a multi-threaded executive that ran on a 1 MHz 6800 processor -- much less power than used in any Arduino. So it is theoretically possible. It was all written in assembler at the time. It could've be written in a higher level language, but some assembler is still needed to handle context switches between the various tasks. Memory will also be an issue -- if you want the various processes to be truly independent, each will need its own stack, which can use up memory real fast. While it was a low-end processor we were using 30 years ago, we did have more than 2k of RAM.

It can be done, even on a low-end processor like that in the Arduino, but I predict the difficult part will be dealing with limited RAM. This will not be a trivial task.

Perhaps a State Machine library that would allow you to easily write a state machine for each 'process'.

good, so there is some unrefined potential.
i am going to work out some idea's that are blooming inside my head, and try to come up with a model to get the user-end of the library/framework as noob friendly as possible.

if anyone else has idea's, just write them even tho they make no sense at all, maybe i can get some inspiration out of your storys :wink:

nick

There are a few multitasking libraries for Arduinos, some of them have a replacement for delay(), which only delays the task in which it is called.

http://playground.arduino.cc/Code/QP
http://www.out--there.com/blog/rtos-for-arduino/

awsome, i will look into it

edit, those look promising, that will same me lots of trouble, real thanks!