Multiple tasking on Teensy/Arduino

Status
Not open for further replies.

Teensying

Member
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.

View attachment MTN.ino
 
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.
 
Non-preemptive threads avoid state machine coding and most of the compatibility problems. Unfortunately, that isn't an option in the above library.
 
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.
 
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.
 
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.


View attachment MTN_2.ino
 
Last edited:
Status
Not open for further replies.
Back
Top