Hello everyone,
I’m new to Arduino and working on a car media player and OBD-II monitor using an Arduino Mega 2560. I need help figuring out the best way to handle multiple UART devices at the same time.
Hardware Setup:
-
Arduino Mega 2560
-
OBD-II CAN Module (Seeed Studio OBD-II CAN-BUS Development Kit) → Request and receives car data
-
Feasycom FSC-DB200 Bluetooth Module → Handles music/handsfree
-
GU280X16G-7000 VFD Display → Displays text and graphic
-
Analog Button Inputs → User controls for media & OBD functions
Tasks to Run in Parallel:
-
Receive OBD-II data (car speed, RPM, etc.).
-
Receive Bluetooth data (song metadata, call status, playback state, etc.).
-
Send updates to the display (text, images).
-
Send requests to OBD-II and Bluetooth module when needed.
-
Read user input from analog buttons.
Since OBD-II and Bluetooth send data almost continuously, I need a way to read both without missing data, while also sending periodic updates to the display and reading analog button inputs. Would FreeRTOS, or something similar help with task scheduling?
Any help or example code would be greatly appreciated! Thanks in advance!
In general, you need to figure what the minimum latency is, and what is the minimum indivisible operation. The minimum divisible operation time need to be less than the minimum latency.
IOW, your slow stuff must not interfere with the fast stuff. There are ways to make fast stuff slower, and slow stuff faster.
Practically speaking, a top level polling loop, with interrupts handling events that need low latency is usually enough. On the Mega you have several hardware UARTS. These are interrupt driven and store data in buffers, so hopefully you only have to respond to complete messages.
Using FreeRTOS won't necessarily make it easier, it may even make it harder.
ETA: there are dozens of tutorials on the subject, here is one https://www.instructables.com/Simple-Multi-tasking-in-Arduino-on-Any-Board/
Thanks for the reply, the tutorial on multitasking is a great read! It’s a bit complicated, especially when it comes to modifying libraries and other low-level tweaks, but I definitely have a better understanding of how things should be structured.
I’ve been a JS developer for 15 years, so the shift from JS to Arduino’s pooling and interrupts logic is a bit of a challenge for me. 
I’m thinking of trying Espruino for this project instead, since its event-driven model seems like a much better fit for what I need. I’ll experiment with that and see how it goes!