in reply to Re: Call function no more than every 0.1 seconds
in thread Call function no more than every 0.1 seconds

in the Arduino world you read the system time, add the desired time period, call the function, and loop until the system time matches the saved time.

Well, that's one way to do it, but IMHO not the most efficient. The generic way to do this with a microcontroller would be to use one of the uC's timers, typically using a 32.768 kHz crystal as the clock source, set up one of the timer's compare units to trigger an interrupt when the timer value matches the user-configured interval (in other words, have the hardware do what you're describing doing in software), and put the uC to sleep or do other things until the interrupt fires. For example, on the Arduino Uno, one could use the ATmega328P's Timer2.

Minor edits.

  • Comment on Re^2: Call function no more than every 0.1 seconds

Replies are listed 'Best First'.
Re^3: Call function no more than every 0.1 seconds
by jmlynesjr (Deacon) on Aug 13, 2020 at 01:22 UTC

    Since I came out of the real-time software world, I would use a processor timer interrupt as you pointed out. However, most of the Arduino world wouldn't understand event driven code if they tripped over it. They use delay() which is blocking - a bad idea.

    I participated in the Open Source Ventilator Project in connection with the University of Florida. The Delay() loop people won out and ran off a contributor that had developed an event driven design because they didn't understand it and didn't want to learn. The end result was a working $200 ventilator, out for manufacture, with some really nasty delay() loops. Good luck with maintenance...

    James

    There's never enough time to do it right, but always enough time to do it over...

      However, most of the Arduino world wouldn't understand event driven code if they tripped over it. They use delay() which is blocking - a bad idea.

      Heh, true. Arduino does have the advantage of having made microcontrollers more accessible, but at the cost of people not learning as much about the hardware at a lower level.

      I've been working with one lately and I've already run into several issues: that the SD library needs a 512 byte buffer that it apparently allocates dynamically (or at least the complier doesn't detect the need for it, so I manually have to keep an eye on the RAM usage and use globals instead of the stack as much as I can, ugh), that constant strings are stored in RAM, that delay() itself uses interrupts, the low-power library I was using uses the watchdog timer which conflicted with my use of the watchdog... I've learned quite a few things about the libraries by tripping over them.

        The Vent project was based on the Arduino Nano processor as most of the rapid prototype team came out of the Amateur Radio community and were familiar with the Nano from the uBitx SSB transceiver(hfsignals.com). It was constantly on the edge of running out of RAM and EEPROM. If you get above 70% memory utilization at compile time you stand the chance of seeing a stack overflow at runtime. In the end all the 3rd party libraries were replaced with internal code with the minimal required functionality. Arduino libraries tend to be bloated, blocking, and poorly documented. That said, it's an easy system to learn and works well for personal hobby applications where setup(), loop(), and delay() aren't an issue. It's much more involved to setup a pseudo real-time environment.

        There is a way to tell the compiler to move constant strings into the EEPROM. I'll look it up and get back to you.

        James

        There's never enough time to do it right, but always enough time to do it over...