I have a couple of tasks to do with arduino but one of them takes very long time, so I was thinking to use threads to run them simultaneously. I have an Arduino Mega
[Update] Finally after four years I can install FreeRTOS in my arduino mega. Here is a link
Using the ThreadHandler library to run 57 hard real-time threads on an Arduino Uno with only 2048 bytes of memory.
The Arduino has a single core, it can't do multithreading. But if you write non-blocking code you can make things appear to happen at the same time. I imagine the problem with your code now is that you have probably used the delay function. That function blocks, so nothing else can happen while the delay is running.
Yes, most microcontrollers have enough hardware support so that threading is possible. If the hardware call/return mechanism is stack based, then all you really need is arbitrary access to the stack.
The Arduino is a very simple processor with no operating system and can only run one program at a time. Unlike your personal computer or a Raspberry Pi, the Arduino has no way to load and run multiple programs. That doesn't mean that we can't manage multiple tasks on an Arduino.
In short: NO. But you may give it a shot at: http://www.kwartzlab.ca/2010/09/arduino-multi-threading-librar/
(Archived version: https://web.archive.org/web/20160505034337/http://www.kwartzlab.ca/2010/09/arduino-multi-threading-librar
Github: https://github.com/jlamothe/mthread
Not yet, but I always use this Library with big projects: https://github.com/ivanseidel/ArduinoThread
I place the callback within a Timer interrupt, and voilá! You have pseudo-threads running on the Arduino...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With