Comment

Comment by primitivesuave

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`.

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton