Multitasking arduino code for servo operation

HI, as a novice i was wondering if anyone can help with coding relating to the you tube video from th elittle wicket railway, i have a basic grasp of the coding but when it comes to discussing the use of Millis with respect to multi tasking i am lost.

(343) Add Realistic Slow Motion to Servo Point Motors - YouTube

Would anyone be able to recommend any information that will explain this in a laymans language, any help would be appreciated.

Many thanks
Colin

Can you post your code, please?
I don't really want to have to sit through a video with illegible coding.
The blink without delay example in the IDE is a good starting point to understanding simple multitasking.

Too broad of a request.


What parts of using a millis( ) based timer are you having trouble understanding ?

Study the 5 examples (i.e. 5 different levels of functionality) discussed here.

The standard Blink without delay-code is hard to understand for three reasons

  1. Variables are scatterd to multiple places
  2. some names of variables are counter-intuitive
  3. it does not use an everyday analogon to explain how it works

This tutorial is easier to understand

best regards Stefan

1 Like

Thanks Larry D, I will have a look, and will try to explain more what I was after if the issue is still muddy. Either way I’lll let you know.

Thanks again

Regards

Colin

It's about five or six lines of actual code.
How hard is that?

It boils down to "at some precalculated point in time, do a single, very quick operation"

(I think, poetically "scatterd" should have an apostrophe between the 'r' and the 'd')

:astonished:

here I have an example how to get non blocking slow servo movements:

https://werner.rothschopf.net/microcontroller/202207_millis_slow_servo.htm

and here on wokwi to play around:

1 Like

There are different styles and different opinions how a code must look like to be easy to understand
This is my version. I took the Wokwi-examle code of noiasca and modified his code into a IMHO easier to understand version

... and you introduced a class with protected variables and an incorrect time comparison (">" instead of ">=") to someone who didn't understand blink without delay, and that's somehow "easy to understand"? Without waiting to see what it was s/he didn't understand?

Well my personal favority is this tutorial

Everyone seems to be somewhat "touchy/oversensitive" today; however, in the name of truth, there can never be multasking on an UNO since there is only one (1) processor core.

Just saying ...

Ah I think this is where I have gone wrong in asking, I should have said I wanted it in a slow motion movement as it will be used for a model railway turnout hence the question about multitasking. Apologies for this.

The term you're looking for is "cooperative multitasking", which is what "blink without delay" demonstrates in its simplest form (a single task).

Hello smithy58

The sketch needs to run a timer to generate a dAngle/dTime.

As basis the BLINKWITHOUTDELAY example, as mother of all Arduino timers, will be a good start point.

Give them a try and check it.

Have a nice day and enjoy coding in C++.

1 Like

If the OP wants to move the servos slowly then move the servos in tiny increments. This is a common enough request that using this thing

The search box is a useful tool.


For instance, using words such as "MOVE SERVOS IN INCREMENTS" in the search box one may find some useful results.

Other search words include "level shifter", "round", "millis()", "MOVE SERVOS IN INCREMENTS"

Look at File|Examples|02.Digital|BlinkWithoutDelay to use millis() for timing instead of delay().

1 Like

@smithy58
so far so clear or do you need further advice?

presumably the drawback of using servos is that they move at their rated speed when commanded to a new position. the obvious answer to incrementally change position with a delay between each increment and the problem with that delays prevent other operations from happening at the same time

the conventional alternative to delay() is checking millis() to perform some action after the delay period has expired instead of waiting.

but then to allow multiple actions to happen simultaneously requires tracking each possible action and sub-state of that action. a data driven approach using a table, either arrays or an array of structure is a conventional approach and making it work for one action makes it possible to work for N-actions.

each iteration of loop() sequences thru the table to do 2 things

  • check if a button is pressed to change the desired state of the switch, and
  • check if the time delay using millis() has expired and move the servo to the next incremental position

no doubt there are web pages describing all of the above separately and combining them all is the typical challenge for less experienced people

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.