Live data from Hacker News

Introducing multitasking to Arduino

blog.arduino.cc

11–20 of 88 posts

Re: Introducing multitasking to Arduino

#11

Earlier quoted context omitted.

Embedded can be a tiny little msp430 with RAM measured in bytes, or it can be the latest and greatest intel flagship - it's a description of how the chip is used more than the chip type. As CPUs get more powerful in general, so too do the little development boards. I can spend $10 on a board with multiple cores and megabytes of ram to drive my blinky lights and do wifi - that's enough power to run a full unix.

where? where can i get one?

Well, there's a "hardware" link at the top of the linked article for starters.

Or google "esp-32" to find store selling a very popular one.

Or check out what's available on sparkfun or adafruit.

Re: Introducing multitasking to Arduino

#12

I'm crappy at embedded development but isn't it pretty normal to handle "multitasking" with interrupts instead of using an event loop?

And/or using a RTOS. I remember using a RTOS on a PIC16/18 family chip like ten years ago.

But the most usual thing was knowing the precise timings that takes instructions and the peripherals to allow do multiple things at same time.

Re: Introducing multitasking to Arduino

#13

I'm crappy at embedded development but isn't it pretty normal to handle "multitasking" with interrupts instead of using an event loop?

They're looking at this from a higher level for defining the API and programming model people will use to write event-based code. An implementation might run using interrupts and a timer to drive an event loop on a single core, or it might scale itself out to run on multiple cores where available like they mention.

Re: Introducing multitasking to Arduino

#14

Nice, but pointless addition to an old processor. ESP32's are cheap and readily accessible and they come with FreeRTOS running already. If I absolutely need to multitask on a Mega328 Arduino, I use the protothreads library. But really, for anything heavy, I'll turn to an ESP32 or an STM32. Use the right tool for the job!

Don't forget that a lot of people are using Arduino framework code on ESP32, it works pretty well. So this development is also relevant for those other boards, maybe even more so.

Yup, and it's running FreeRTOS out of the box.

Re: Introducing multitasking to Arduino

#16
A couple years ago, I managed a summer/afterschool program that introduced thousands of kids (age 8-18) to the Arduino. When we would set up a software abstraction to simplify multitasking (something that came up often with more advanced students), we set up something like this:

  int time = 0;
  int max_period = 10000;

  void loop() {
      if (time % 100 == 0) {
          everyTenthSecond();
      }
      if (time % 1000 == 0) {
          everySecond();
      }
      // ...
  
      time = (time + 1) % max_period; 
  }
This `loop` assumes functions are perfectly non-blocking, which obviously is not always true, but works well for 99% of actual use cases. If keeping accurate time is required, the student would compare `time` with the elapsed millisecond time since the last loop invocation, adjust accordingly, and check for any periodic functions that weren't executed in that period. It gets tedious quite fast.

If I had to imagine a solution, it would be to simply have an easy way to enter parameters to the JS-equivalent of `setTimeout` and `setInterval`.

Re: Introducing multitasking to Arduino

#17

I'm crappy at embedded development but isn't it pretty normal to handle "multitasking" with interrupts instead of using an event loop?

That's exactly what's described.

Arduino's framework divides main() into two functions: Setup() and loop(), but they're essentially the same as whatever comes before the while loop, and the while loop itself, in a standard embedded c main function.

Then you just have interrupts setting states, and the loop responds to the change in state.

Re: Introducing multitasking to Arduino

#20
post #17

I'm crappy at embedded development but isn't it pretty normal to handle "multitasking" with interrupts instead of using an event loop?

That's exactly what's described. Arduino's framework divides main() into two functions: Setup() and loop(), but they're essentially the same as whatever comes before the while loop, and the while loop itself, in a standard embedded c main function. Then you just have interrupts setting states, and the loop responds to the change in state.

It's not quite the same as a while loop, because when you return from the loop() function, how long it takes before it gets called again isn't defined. Using arduino-pico, for example, it does some USB I/O handling (when using TinyUSB). If you write an actual while loop and don't return from the loop function often enough, you might starve the USB port implementation.

This adds enough of a delay that I moved some code to the other core on the Pico to get precise timing.

Post reply on HN