Writing a multitasking application directly on Arduino is obviously not perfect and complex to develop. I’ve used SimpleTimer as the basis for multitasking on this watering project, and in the setup, SimpleTimer doesn’t work effectively.
Using the operating system will be a good solution to multi-task scheduling.
By a coincidence, I found AtomThreads, a small system that supports 8-bit microcontrollers.
Therefore, AtomThreads-Arduino is packaged so that AtomThreads can be directly used in Arduino.
#include <AtomThreads.h>
#define IDLE_STACK_SIZE_BYTES 128
#define MAIN_STACK_SIZE_BYTES 204
#define DEFAULT_THREAD_PRIO 16
static ATOM_TCB main_tcb;
static uint8_t main_thread_stack[MAIN_STACK_SIZE_BYTES];
static uint8_t idle_thread_stack[IDLE_STACK_SIZE_BYTES];
uint8_t status;
void setup(){
SP = (int)&idle_thread_stack[(IDLE_STACK_SIZE_BYTES/2) - 1];
status = atomOSInit(&idle_thread_stack[0], IDLE_STACK_SIZE_BYTES, FALSE);
if (status == ATOM_OK) {
avrInitSystemTickTimer();
status = atomThreadCreate(&main_tcb,
DEFAULT_THREAD_PRIO, main_thread_func, 0,
&main_thread_stack[0],
MAIN_STACK_SIZE_BYTES,
FALSE);
if(status == ATOM_OK) { atomOSStart(); }}}static void main_thread_func (uint32_t) {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN.OUTPUT);
while (1) {
loop();
}
}
// Loop and print the time every second.
void loop() {
digitalWrite(LED_BUILTIN.HIGH); // turn the LED on (HIGH is the voltage level)
atomTimerDelay(SYSTEM_TICKS_PER_SEC); // wait for a second
digitalWrite(LED_BUILTIN.LOW); // turn the LED off by making the voltage LOW
atomTimerDelay(SYSTEM_TICKS_PER_SEC);
}
Copy the code
Watering uses atomThreadCreate to create different processes for different tasks and atomTimerRegister and atomTimerCancel to control automatic backlighting. The reliability and real-time of the whole project are much better than before, see github.com/Lupino/wate…