Hello, everyone!
Actually I'm new to the Arduino and the forum but I love the Arduino and I like trying to create some projects with it. In those projects, I encountered some problems. One of them is handling multiple tasks with Arduino. There are many solutions to this problem but I wanted to create my own solution.
So, Here is MultiTasking Library!!!
GitHub: GitHub - devrim-oguz/MultiTasking: A library for multitasking in Arduino.
You can use it, edit it, share it or whatever you want under GNU General Public License. Feel free to share!
I would be very happy if you try and leave a comment about it.
It can handle multiple tasks at the same time. Well, sort of...
I will explain the working principle in below but it doesn't actually run them at the same time. It just creates a list of tasks and runs them one by one. It also can call a function at a given time period.
I haven't test it very good but it works just fine. If you find any bugs or problems, please leave a comment. And also I'm open to suggestions.
I made a stupid sketch to explain the usage of the library. You can have a look at it to understand how to use it.
The library has 6 main functions;
//=================================
Function: addThread
Usage: addThread( functionName );
you can only add functions that doesn't return anything and doesn't have inputs.
The function signature must be like this;
void theFunction( void );
If the function can't be added, it will return 0, otherwise 1.
With this command, you can set a function to run continuously. For example lets say we have a function called checkButtons which has no inputs and returns nothing. You can add it like that;
addThread( checkButtons );
//=================================
Function: setTimer
Usage: setTimer( functionName, timeInterval, timesToExecute );
you can only add functions that doesn't return anything and doesn't have inputs.
The function signature must be like this;
void theFunction( void );
If the function can't be added, it will return 0, otherwise 1.
With this command, you can set a function to run periodically, lets say every 1000 milliseconds. For example if you have a function called blinkLED, you can add it like that;
setTimer( blinkLED, 1000, 0 );
If you give 0 as timesToExecute, it will run forever. ( or at least until you stop it )
You can set it to be called finite times like this;
setTimer( blinkLED, 1000, 10);
This function will call the blinkLED function every 1000 milliseconds for 10 times.
//=================================
Function: removeThread
Usage: removeThread( functionName );
With this function, you can delete a thread added before.
//=================================
Function: killTimer
Usage: killTimer( functionName );
With this function, you can delete a timer added before.
//=================================
Function: startTasks
Usage: startTasks();
This function starts multi threading. The program stops after this function. So, no code will be executed after this function until you stop it.
This function essentially an endless loop which calls all the threads and timers continuously.
//=================================
Function: stopTasks
Usage: stopTasks();
This function stops multi threading. After this function, the code will keep continue where it left.
And 5 side functions;
//=================================
Function: isThreadRunning
Usage: isThreadRunning( functionName );
This function returns 1 if the function is in thread list and returns 0 if the function is not in thread list.
//=================================
Function: isTimerRunning
Usage: isTimerRunning( functionName );
This function returns 1 if the function is in timer list and returns 0 if the function is not in timer list.
//=================================
Function: flushThreads
Usage: flushThreads();
This functions removes all of the threads.
//=================================
Function: flushTimers
Usage: flushTimers();
This functions removes all of the timers.
//=================================
Function: flushAll
Usage: flushAll();
This functions removes all of the threads and timers.
//______________________________________________________________________________________________
How this library works;
-It creates two lists. One of them keeps threads and the other keeps timers.
Every thread takes 3 bytes and every timer takes 9 bytes of RAM.
The amount of maximum threads and timers could be adjusted in the file called MultiTasking.h.
In the default settings It can run 50 Threads and 20 Timers.
In those settings the library uses 346 bytes of RAM. ( Which takes %16 of the RAM in Arduino UNO )
The startTasks function continuously calls all the threads and calls the timer functions depending on the millis passed. You can still use the loop function, because it is added as a thread by default. For more details, you can look at the source code.
Here is the example sketch:
#include <MultiTasking.h> //Adding the MultiTasking library.
TaskList myTasks; //Creating a TaskList named myTasks.
int LED = 13; //Defining LED pin as 13.
int analogPin = A0; //Defining analog input pin as A0.
int analogValue = 0; //Creating an integer to hold analog readings.
void setup() { //Setup Function
Serial.begin(9600); //Starting serial in 9600 bauds.
pinMode( LED, OUTPUT ); //Setting LED pin as OUTPUT.
myTasks.setTimer( count, 1000, 10 ); //Adding a timer to call the function "count" every 1000 milisecond for 10 times.
myTasks.setTimer( newTasks, 15000, 1 ); //Adding a timer to call the function "newTasks" just for 1 time.
//Timer will start after 15000 miliseconds.( 15 second )
myTasks.startTasks(); //Starting tasks.
//No code will work underneath here until you stop MultiTasking.
//Don't use any delay function or endless loop, because it will stop the other tasks.
//Every code works one by one. So if you stop the flow of the code, the whole program will stop.
}
void loop() { //Loop function still works as a Thread. You can use it like a Thread.
}
void count() { //Declaring a function to print seconds until reset or start.
Serial.println(millis()/1000); //Printing seconds.
}
void newTasks() { //Declaring a function named newTasks which will add new tasks to the TaskList.
myTasks.addThread( measureAnalogValue ); //Adding a thread to run continuously withouth any delays.
myTasks.setTimer( blinkLED, 1000, 0 ); //Calling blinkLED function every second, forever.( or until you stop them )
myTasks.setTimer( sayHello, 5000, 0 ); //Calling sayHello function every 5 seconds, forever.
myTasks.setTimer( printAnalogValue, 500, 0 ); //Calling printAnalogValue function every 500 milisecond.
}
void measureAnalogValue() { //Declaring a function to run continuously. It reads the analog pin and writes it to an int.
analogValue = analogRead( analogPin ); //Reading analog pin...
}
void blinkLED() { //Declaring a function to blink a led.
static int LED_State = 0; //Declaring a static variable to know led's state.
if( LED_State == 0 ) { //If the led is off,
digitalWrite( LED, HIGH ); //Make it on and
LED_State = 1; //Save the state as on.
}
else { //If the led is on,
digitalWrite( LED, LOW ); //Make it off and
LED_State = 0; //Save the state as off.
}
}
void sayHello() { //Declaring a function to print some string to serial.
Serial.print( "Hello, Wolrd! " ); //Printing some string
Serial.print( "Millis: " ); // .
Serial.println( millis() ); //Printing millis.
}
void printAnalogValue() { //Declaring a function to print analogValue readed from the analog pin.
Serial.print( "Analog Input: " ); //Printing some string
Serial.println( analogValue ); //Printing analogValue.
}
//!!!Thank You For Using The Library!!! <3 <3 <3
I hope you like my library.
Sorry for my bad English
.
Best Regards...