Home

Awesome

ArduinoThread Logo

ArduinoThreads Motivation

Arduino does not support isolated parallel tasks (Threads), but we can make the main loop switch function execution conditionally and thus simulate threading with Protothread mechanism. This library implements it and helps you to:

Blinking an LED is often the very first thing an Arduino user learns. And this demonstrates that periodically performing one single task, like toggling the LED state, is really easy. However, one may quickly discover that managing multiple periodic tasks is not so simple if the tasks have different schedule.

The user defines a Thread object for each of those tasks, then lets the library manage their scheduled execution.

It should be noted that these are not “threads” in the real computer-science meaning of the term: tasks are implemented as functions that are run periodically. On the one hand, this means that the only way a task can yield the CPU is by returning to the caller, and it is thus inadvisable to delay() or do long waits inside any task. On the other hand, this makes ArduinoThreads memory friendly, as no stack need to be allocated per task.

Installation

  1. Download the Master branch from gitHub.
  2. Unzip and modify the Folder name to "ArduinoThread" (Remove the '-master' suffix)
  3. Paste the modified folder on your Library folder (On your Libraries folder inside Sketchbooks or Arduino software).
  4. Restart the Arduino IDE

If you are here just because another library requires a class from ArduinoThread, then you are done now .

Getting Started

There are many examples showing many ways to use it. We will explain Class itself, what it does and how it does.

There are three main classes included in the library: Thread, ThreadController and StaticThreadController (both controllers inherit from Thread).

Create Thread instance:

Thread myThread = Thread();
// or, if initializing a pointer
Thread* myThread = new Thread();

Setup thread behaviour

You can configure many things:

myThread.enabled = true; // Default enabled value is true
myThread.setInterval(10); // Setts the wanted interval to be 10ms
/*
	This is useful for debugging
	(Thread Name is disabled by default, to use less memory)
	(Enable it by definint USE_THREAD_NAMES on 'Thread.h')
*/
myThread.ThreadName = "myThread tag";
// This will set the callback of the Thread: "What should I run"?
myThread.onRun(callback_function); // callback_function is the name of the function

Running threads manually

Ok, creating threads isn't too hard, but what do we do with them?

// First check if our Thread should be run
if(myThread.shouldRun()){
  // Yes, the Thread should run, let's run it
  myThread.run();
}

Running threads via a controller

If you had 3, 5 or 100 threads, managing them manually could become tedious. That's when ThreadController or StaticThreadController comes into play and saves you the repetitive thread management parts of code.

// Instantiate new ThreadController
ThreadController controller = ThreadController();
// Now, put bunch of Threads inside it, FEED it!
controller.add(&myThread); // Notice the '&' sign before the thread, IF it's not instantied as a pointer.
controller.add(&hisThread);
controller.add(&sensorReadings);
...

or

// Instantiate a new StaticThreadController with the number of threads to be supplied as template parameter
StaticThreadController<3> controller (&myThread, &hisThread, &sensorReadings);
// You don't need to do anything else, controller now contains all the threads.
...

You have created, configured, grouped it. What is missing? Yes, whe should RUN it! The following will run all the threads that NEED to run.

// call run on a Thread, a ThreadController or a StaticThreadController to run it
controller.run();

Congratulations, you have learned the basics of the ArduinoThread library. If you want to learn more, see bellow.

Tips and Warnings

noInterrupts();
// Put the code that CANNOT be interrupted...
interrupts(); // This will enable the interrupts egain. DO NOT FORGET!

Library Reference

Configuration options

Thread

ThreadController

StaticThreadController