Hi, All!
I'm new in Arduino field. After trying few examples, I realized that each time I solving same problem: handling few parallel processes.
So I wrote a smal library which helps to mimic multithreading.
Example:
Let say we have 2 processes which blinking in different frequencies.
//Thread 1
void loop() {
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, LOW);
delay(500);
}
And
//Thread 2
void loop() {
digitalWrite(4, HIGH);
delay(300);
digitalWrite(4, LOW);
delay(300);
}
Using Threads library we breaking each thread into 2 functions:
// 1-st thread s1_1 & s1_2 blink 500/500
unsigned long s1_1(void *param) {
digitalWrite(6, HIGH);
return 500l;
}
unsigned long s1_2(void *param) {
digitalWrite(6, LOW);
return 500l;
}
// 2-nd thread blink 300/300
unsigned long s2_1(void *param) {
digitalWrite(4, HIGH);
return 300l;
}
unsigned long s2_2(void *param) {
digitalWrite(4, LOW);
return 300l;
}
As you can see delay values returned by the functions.
Than we creating the threads:
void setup() {
t1 = new Thread(NULL);
t1->addFunc(s1_1);
t1->addFunc(s1_2);
t2 = new Thread(NULL);
t2->addFunc(s2_1);
t2->addFunc(s2_2);
// ...
}
Finally we can run it from loop function:
void loop()
{
t1->run();
t2->run();
}
Ok we have multithreading. Now let's add some local variables. Let's implement following thread:
unsigned long sos[] = {200,200,200,200,200,200,700,200,700,200,700,200,200,200,200,200,200,1500};
void loop() {
for(int i=0;i<18;i += 2) {
digitalWrite(5, HIGH);
delay(sos[i]);
digitalWrite(5, LOW);
delay(sos[i+1]);
} // end of for
}
We will implement it in one function and pass the parameter which points to the current interval.
The parameter class not only holds the variables, but have some functionality.
// Morse time intervals SOS: ...---...
unsigned long sos[] = {
200,200,200,200,200,200,700,200,700,200,700,200,200,200,200,200,200,1500};
/* Parameter class which passed between functions of 3rd thread */
class ParamMorse {
private:
int curr;
public:
ParamMorse(){
curr = 0;
}
int signal(){
return ((curr%2)==0)?HIGH:LOW;
}
long nextVal(){
long res = sos[curr];
curr++;
if(curr >= 18) {
curr=0;
}
return res;
}
};
// 3-rd thread blink with intervals from ParamMorse parameter
unsigned long s3_1(void *param) {
ParamMorse *p = (ParamMorse *)param;
digitalWrite(5, p->signal());
unsigned long t = p->nextVal();
return t;
}
Than we can add 3-rd thread to the example. (See full example in attached file)
To run it connect 3 leds to pins 4,5 and 6 and see what happends.
Any suggestions / fixes etc - very welcome!
Threads.zip (3.27 KB)