Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Call function no more than every 0.1 seconds

by jmlynesjr (Deacon)
on Aug 12, 2020 at 01:21 UTC ( #11120634=note: print w/replies, xml ) Need Help??


in reply to Call function no more than every 0.1 seconds

I'm not sure how you do it in Perl, but 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.

This extends the Setup()/Loop() abstraction with pseudo tasking.

James

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

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

Replies are listed 'Best First'.
Re^2: Call function no more than every 0.1 seconds
by haukex (Bishop) on Aug 12, 2020 at 20:24 UTC
    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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11120634]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2022-01-24 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (64 votes). Check out past polls.

    Notices?