0

I am trying to implement a function that tells the computer to sample some data a given number of times per day. Let say for example i want to sample a sensor every 30 second over a time period of 24 hours. And Another sensor maybe every 120 second over a time period of 24 hours.

24 hours = 86400 seconds.

I have a counter running that contains the elapsed time in seconds.

Is there a way to implement a C++ conditional branch/function that achieves this in an elegant way?

5
  • this is too broad try to show some example code
    – johnny 5
    Jan 22, 2016 at 16:33
  • I understand. The final code will be implemented on arduino hardware but i think my problem applies to general c++ knowledge. My plan is to have a counter that counts seconds from 0 to 86400 and then resets. I don't know how often i should read data from the sensors yet, but say i want to read from sensor1 every 30 second and sensor2 every 120 second. Maybe i need some kind of if statement that read sensor1 when elapsed time is 30s,60s,90s,120s,150s,... and another if statement that read sensor2 when elapsed time is 120s,240s,360,... Is the modulus operator of any use in this case?
    – bubbleboy
    Jan 22, 2016 at 16:50
  • use a timer instead of a counter stackoverflow.com/questions/3220477/how-to-use-clock-in-c if(time%interval == 0)
    – johnny 5
    Jan 22, 2016 at 17:07
  • Yes! That piece of code is exactly what i need. Thank you very much.
    – bubbleboy
    Jan 22, 2016 at 17:12
  • Possible duplicate of Sleep function in C++
    – AndyG
    Jan 22, 2016 at 19:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.