Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: Multiple tasking on Teensy/Arduino

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    5

    Multiple tasking on Teensy/Arduino

    Hello,

    I would like to perform two tasks in parallel. One of them takes some time to be completed and thus the other one is performed once the first one is completed.

    There are multithreading topics mostly for arduino but since Teensy is a more powerful uC I thought there can be better options. I' m still seeking for a solution and If you have suggestions that would be great. Also I attached a simplified version of the code behaves the same.



    Looking forward to hearing from you.

    MTN.ino

  2. #2

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,084
    Which way is best for you depends on how you like to write code and the details of the sort of program you wish to create.

    Unfortunately there is no really beautiful mutlitasking system. Those which do exist come quite a few caveats. Many existing Arduino libraries aren't fully reentrant or thread safe, so even if you find a system you really like, which libraries you will use can also play a factor.

    From the code sample, you would probably want to change this into a state machine using millis() or elapsedMillis to replace the delay() functions.

    Code:
      //-------------------------------------------------------------------------------
      // Task 1  
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(500);
    Whether writing all your code as state machines without delay() is right for you, I can't say. But unless you *really* don't want to go that route, I'd suggest at least starting in that direction if you plan to use Arduino libraries. It gets the best compatibility with most existing code.

  4. #4
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    1,075
    Non-preemptive threads avoid state machine coding and most of the compatibility problems. Unfortunately, that isn't an option in the above library.

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Posts
    5
    Quote Originally Posted by PaulStoffregen View Post
    Which way is best for you depends on how you like to write code and the details of the sort of program you wish to create.

    Unfortunately there is no really beautiful mutlitasking system. Those which do exist come quite a few caveats. Many existing Arduino libraries aren't fully reentrant or thread safe, so even if you find a system you really like, which libraries you will use can also play a factor.

    From the code sample, you would probably want to change this into a state machine using millis() or elapsedMillis to replace the delay() functions.

    Code:
      //-------------------------------------------------------------------------------
      // Task 1  
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(500);
    Whether writing all your code as state machines without delay() is right for you, I can't say. But unless you *really* don't want to go that route, I'd suggest at least starting in that direction if you plan to use Arduino libraries. It gets the best compatibility with most existing code.

    Dear Paul,
    I tried using milis() method but didnot solved the problem. Here the "motor.runSpeed()" method of "AccelStepper" library doesn' t want to be interrupted. Any delay makes the stepper behave like jumping. So I need to keep it work as a background task but also I need control on it. Thus, there is a "motorRunning" check variable.

  6. #6
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    423
    Can you show your code for your milis() method.

  7. #7
    Junior Member
    Join Date
    Sep 2021
    Posts
    5
    Quote Originally Posted by WMXZ View Post
    Dear WMXZ,


    I tried to apply the led blink example. It worked but not properly. Step motor works at constant speed. I can change direction, start and stop it but no change on speed?? And still I hear kind of small jumps.

    MultiThreadEx_2.ino

  8. #8
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    423
    Quote Originally Posted by Teensying View Post
    Dear WMXZ,


    I tried to apply the led blink example. It worked but not properly. Step motor works at constant speed. I can change direction, start and stop it but no change on speed?? And still I hear kind of small jumps.

    MultiThreadEx_2.ino
    You still have 1 second of delay. What do you expect!!

  9. #9
    Junior Member
    Join Date
    Sep 2021
    Posts
    5
    Quote Originally Posted by BriComp View Post
    You still have 1 second of delay. What do you expect!!

    I didn' t get your point? One of the tasks can take longer than 1 sec or in periods of 1 sec and the other one is expected to run "runspeed" method all the time and seperately.

  10. #10
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    423
    If you don't show your code, then we are unlikely to be able to help you. The code in MultiThread_2.ino does NOT use milis() as was suggested in a previous reply in order to try to help you.

  11. #11
    Junior Member
    Join Date
    Sep 2021
    Posts
    5
    Attached is a similar short version of the main code.

    Task1 takes 650 msec so I divdied it to led on/off states. I measured this time using milis() func on real code. Since task1 takes 650 msec "runspeed" method is interrupted here which results in the motor behave like jumping.


    In brief, I want to control a motor and perform another task(s) independently. millis() method looks very useful for many tasks but in here since "runspeed()" must be driven in every cycle of the main loop, long(relatively) mid tasks make the motor start and stop.

    I also searched for other motor driver libs. but it takes the current task to another level and dont want to do that now.


    Anyother suggestion is also very welcome.


    Thank you.


    MTN_2.ino
    Last edited by Teensying; 09-14-2021 at 08:52 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •