Skip to main content
added 328 characters in body
Source Link
user6569
user6569

I do not know if you plan on doing anything else on your arduino, but holding a delay inside the loop function is bad idea if you do, things need to happen outside of your code (any serial I/O for sure and others, right?).

That said, I like @zgrknr answer the best, although the others will also work. That answer allows using smaller numbers, that are easier to understand. Just make sure to keep your variables global, and not within loop or else they will get re-initialized every time.

As far as accuracy, do some real by the clock testing, and adjust your numbers, who cares if it is accurately counting as long as you can identify what numbers gives you the time period you want.

response to comment:

My firmware code looks like this, is yours different?

int main(void)
{
  init();
  initVariant();

#if defined(USBCON)
  USBDevice.attach();
#endif

  setup();

  for (;;) {
    loop();
    if (serialEventRun) serialEventRun();
  }        
  return 0;
}

I do not know if you plan on doing anything else on your arduino, but holding a delay inside the loop function is bad idea if you do, things need to happen outside of your code (any serial I/O for sure and others, right?).

That said, I like @zgrknr answer the best, although the others will also work. That answer allows using smaller numbers, that are easier to understand. Just make sure to keep your variables global, and not within loop or else they will get re-initialized every time.

As far as accuracy, do some real by the clock testing, and adjust your numbers, who cares if it is accurately counting as long as you can identify what numbers gives you the time period you want.

I do not know if you plan on doing anything else on your arduino, but holding a delay inside the loop function is bad idea if you do, things need to happen outside of your code (any serial I/O for sure and others, right?).

That said, I like @zgrknr answer the best, although the others will also work. That answer allows using smaller numbers, that are easier to understand. Just make sure to keep your variables global, and not within loop or else they will get re-initialized every time.

As far as accuracy, do some real by the clock testing, and adjust your numbers, who cares if it is accurately counting as long as you can identify what numbers gives you the time period you want.

response to comment:

My firmware code looks like this, is yours different?

int main(void)
{
  init();
  initVariant();

#if defined(USBCON)
  USBDevice.attach();
#endif

  setup();

  for (;;) {
    loop();
    if (serialEventRun) serialEventRun();
  }        
  return 0;
}
Source Link
user6569
user6569

I do not know if you plan on doing anything else on your arduino, but holding a delay inside the loop function is bad idea if you do, things need to happen outside of your code (any serial I/O for sure and others, right?).

That said, I like @zgrknr answer the best, although the others will also work. That answer allows using smaller numbers, that are easier to understand. Just make sure to keep your variables global, and not within loop or else they will get re-initialized every time.

As far as accuracy, do some real by the clock testing, and adjust your numbers, who cares if it is accurately counting as long as you can identify what numbers gives you the time period you want.