Last active
July 26, 2022 21:12
-
-
Save mbrav/0043748bcb07d1b1aaba to your computer and use it in GitHub Desktop.
A simple Arduino loop "FPS" counter - v1.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); | |
} | |
} |
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
Thanks! this is cool