Skip to content

Instantly share code, notes, and snippets.

@netmute
Created May 13, 2012 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netmute/2689625 to your computer and use it in GitHub Desktop.
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

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

@netmute
Copy link
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