-1

I currently have a function that poles all my sensors every time through it's loop, but I want to pole one of the sensors only every 10th time. Currently there is a 10 millisecond delay at the beginning of my function. I know I could increment a counter and use modulo, but is there a better practice for this?

  • 1
    Please show us the sketch you're using. – VE7JRO Mar 16 at 4:31
  • It's a thousand line program half of which is dedicated to poling the various sensors. I just need the general solutions to doing something once for every 10 times you do something else. – bob mcgrath Mar 16 at 4:57
  • 1
    You've already hit the nail on the head with the modulo. Or there's good old count up until the variable hits 10 and set it back to zero. Aside from that it's going to be pretty situation specific. – Delta_G Mar 16 at 5:25
  • 1
    the word is poll, not pole – jsotola Mar 16 at 6:40
  • 1
    decrement a counter ... if it reaches zero, then poll the sensor and reload the counter ... the microcontroller has an efficient way of checking for zero values by way of the zero flag that is set by the decrement instruction – jsotola Mar 16 at 17:01
1
for(int i=0; i < 9999; ++i)
{
    if(i % 10 == 0)
    {
         //my actions on i = 0, 10, 20, 30 ...
    }
}
| improve this answer | |
1

Normally it's suboptimal to reduce the speed of loop just to be able to count them. Apply the BlinkWithoutDelay principle instead (which is not about Blink, but about WithoutDelay)

| improve this answer | |

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.