1

I want to write a little library;
the unlibrarized functionality does:

checking in every loop-cycle some inputs,
doing some calculations with it
and finally setting a boolean to either true or false.

How do I periodically check the inputs inside the library?
I know that I could add a function-call to the library in the loop()-function. But I would prefer to just check the boolean in the loop.

Is there a way, to instantiate a class and start an periodically function by doing so? And if, how does this constant checking affect/coexist with my 'normal' loop()-function?

1

Generally speaking, no, you can not take control of the processor away from the main "thread" in a library. The normal solution is to provide an update method or something like that. you could do the calculations in a function that returns your boolean answer, then the loop code is basically the same with or without asynchronous updating.

If you can use interrupts to read your input, either as an external interrupt or on an internal timer interrupt, then what you want could be achieved because the interrupt would take control away from the main execution context and give it to you.

Interrupt routines need to be fast and simple though, and it makes the "cost" of using the library higher, since there is a limited number of interrupts/timers and other libraries may require the one you code yours to use.

Of course, the more powerful arduinos that actually run an os will have support for multiprocessing which you could leverage to get the behavior you want. This answer is intended for a standard AVR based arduino (uno, mega, etc.)

0

I know that I could add a function-call to the library in the loop()-function. But I would prefer to just check the boolean in the loop.

What's the difference?

In the loop you can call something like:

void loop ()
  {
...
  if (mylibrary.ready ())
    {
    // it's time to act!
    }
...
  }

You can make a function, that returns a boolean type, which tells you if the thing you need to do is ready. That function does the checking before returning true or false.

After all, you are planning to check the boolean periodically, presumably often enough to notice it changing, why not have that be the point of noticing whatever-it-is you need to notice?

  • Yes, your are right! That's also the way I decided to go. Though, I tried to achieved this to have a more convenient implementation of the library: This if(ready==true) could theoretically be somewhere in loop(). But my periodically checking is VERY time-sensitive. If I call the function to check in the if(), I have to be sure, that there is no delay which alters the result. – MaxBoehme Sep 12 '15 at 2:59
  • Now it is like: void loop() { freqdetec.checkFreq(); if (freqdetec.freqChanged()) { // do stuff } } The benefit from separating it into two calls is first, that it is more obvious what the single calls are doing, and second, I can place the checkFreq() in the beginning of the loop(), to avoid having it cluttered somewhere. – MaxBoehme Sep 12 '15 at 3:04
  • As another person answered, if it is highly time-sensitive you could use an interrupt (if this is a switch you are checking), or a timer firing periodically (if you need to test something like an analog reading). – Nick Gammon Sep 12 '15 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.