Skip to content

Instantly share code, notes, and snippets.

@netmute
Created May 13, 2012 18:26
Show Gist options
  • Select an option

  • Save netmute/2689625 to your computer and use it in GitHub Desktop.

Select an option

Save netmute/2689625 to your computer and use it in GitHub Desktop.
Run a loop at defined intervals on Arduino.
/* Run a loop at defined intervals.
This was tested with an Arduino Uno.
Other boards might require tweaking of the boardFrequency constant.
*/
/* How many times per second timedLoop() gets executed */
int loopFrequency = 1;
void timedLoop() {
/* Do stuff. */
}
const int boardFrequency = 14796;
int freqTimer = 0;
void loop() {
if ((freqTimer % (boardFrequency / loopFrequency)) == 0) {
freqTimer = 0;
timedLoop();
}
freqTimer++;
}
@mirceapricop
Copy link
Copy Markdown

To be safe, you should also do freqTimer = 0 before timedLoop(), and make it an int.

@netmute
Copy link
Copy Markdown
Author

netmute commented May 13, 2012

Good point, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment