The delay() function helps stop the program from running for a period of time, but nothing else is executed in your code during that time. Use the following methods to keep your program running while you wait.
There are several options for multitasking, including millis(), TaskScheduler, and FreeRTOS.
The simplest method to run a task periodically in loop() is employing millis(). It works because the program is constantly checking the clock to determine if enough time has passed for the action to be executed.
Your Arduino as a State Machine (Multitasking)
The ESP-WROOM-32D processor has dual-core capabilities. FreeRTOS firmware is already installed. FreeRTOS is an open-source Real-time Operating system that is very useful for multitasking. RTOS helps in managing the resources and maximizing the system's performance.
Core 0 - runs WiFi & Bluetooth, so be careful not to overload this core
Core 1 - Arduino code runs on this core.
Advantages of Multi-core processor
Multi-core processors are useful when more than 2 processes are working simultaneously.
As work is distributed among different cores, its speed increases and multiple processes can be finished simultaneously.
Power consumption can be reduced because when any core is in idle mode, it can shut down the peripherals that are not used at that time.
Dual-core processors have to switch between different threads less often than single-core processors because they can handle two at once instead of one at a time.
Speed Test
Dual Core Programming
FreeRTOS stands for "Free Real-Time Operating System." It is software that helps manage the tasks and resources of a microcontroller, ensuring they run in a timely and organized manner. It provides features like multitasking, task scheduling, memory management, and synchronization between tasks. You can build more complex projects with multitasking capabilities, handle real-time requirements, and efficiently manage system resources. It is especially useful when developing applications that involve simultaneous tasks or time-critical operations, such as IoT devices, robotics, or data acquisition systems.
Here's how it works:
a. Task Management: You can create tasks in your program, each performing a specific job or function. For example, one task can read sensor data, another can process the data, and a third can control an output device. FreeRTOS ensures these tasks run independently and in a coordinated manner.
b. Task Scheduling: FreeRTOS schedules and switches between tasks based on their priorities or predefined intervals. It ensures that higher-priority tasks get more processing time and can respond quickly to events.
c. Resource Management: FreeRTOS helps manage system resources like memory, ensuring efficient usage among different tasks. It also provides synchronization mechanisms like semaphores and mutexes to coordinate access to shared resources.
d. Real-Time Capabilities: FreeRTOS is specifically designed for real-time applications where precise timing is crucial. It enables you to meet strict deadlines, handle time-sensitive operations, and respond promptly to external events.
Manage FreeRTOS tasks - Suspend, Delay, Resume, Delete
How to use FreeRTOS
Multitasking with FreeRTOS
TaskScheduler for Arduino is a library that helps you schedule and manage project tasks without using a full-fledged operating system. It provides a simple way to organize and control different actions or functions that must be executed at specific times or intervals.
With TaskScheduler, you can:
Schedule Tasks: You can define tasks and specify when and how often they should run. For example, you can set a task to run every 1 second, every 5 minutes, or at a specific time of day.
Manage Task Priorities: You can assign priorities to tasks, determining the order in which they are executed. Tasks with higher priorities will be executed before tasks with lower priorities.
Handle Task Execution: Each task is associated with a function or block of code that will be executed when the task is scheduled to run. Inside the function, you can perform specific actions or tasks that you want to accomplish.
React to Events: TaskScheduler allows you to trigger tasks based on external events, such as a button press or a sensor reading. You can set up tasks to respond to these events and perform the actions you'd like.
Accurate and Easy Task Scheduling