Skip to content

Instantly share code, notes, and snippets.

@mbrav
Last active July 26, 2022 21:12
Show Gist options
  • Save mbrav/0043748bcb07d1b1aaba to your computer and use it in GitHub Desktop.
Save mbrav/0043748bcb07d1b1aaba to your computer and use it in GitHub Desktop.
A simple Arduino loop "FPS" counter - v1.1
/*
Simple Arduino loop FPS counter v1.1
Created 2 Mar 2015
Updated 22 Feb 2016
by Michael Braverman
CHANGE LOG
v1.1 - added optimizations
- changed '>' operators to '>='
v1.0 - first version
LINK
https://gist.github.com/mbrav/0043748bcb07d1b1aaba
*/
void setup() {
// The higher the baud rate the more precise the result will be
Serial.begin(115200);
}
void loop() {
// A function for demo
// can be deleted
doSomething();
// Measure the framerate every 5 seconds
// The higher this value the more precise the benchmark result will be
// However, it does not matter if something beyond 10 seconds is set
fps(5);
}
static inline void fps(const int seconds){
// Create static variables so that the code and variables can
// all be declared inside a function
static unsigned long lastMillis;
static unsigned long frameCount;
static unsigned int framesPerSecond;
// It is best if we declare millis() only once
unsigned long now = millis();
frameCount ++;
if (now - lastMillis >= seconds * 1000) {
framesPerSecond = frameCount / seconds;
Serial.println(framesPerSecond);
frameCount = 0;
lastMillis = now;
}
}
void doSomething() {
// Insert you resource intesive code here
// or delete the whole function
for (int i = 0; i <= 10; i++) {
digitalRead(3);
}
}
@Zaxuhe
Copy link

Zaxuhe commented Nov 11, 2019

Thanks! this is cool

@mbrav
Copy link
Author

mbrav commented Nov 26, 2019

Thanks! this is cool

You're welcome! Glad you found it helpful.

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