Created
March 25, 2021 22:23
-
-
Save gullinbursti/aa5dfb29bdde9368dd06f1116b48d3c8 to your computer and use it in GitHub Desktop.
Calc average value from given sample size w/ arduino-c
This file contains hidden or 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
| // immediate fan speed | |
| int fanRpm = (60000000 / float(pulseIn(tachPin, LOW, 100000))) * 0.5; | |
| // no average yet, use last fan reading | |
| if (avgRpm == 0) { | |
| avgRpm = fanRpm; | |
| } | |
| // check array vals for first -1 | |
| int emptyInd = -1; | |
| for (byte i=0; i<sampleSize; i++) { | |
| if (rpmSamples[i] == -1) { | |
| emptyInd = i; | |
| break; | |
| } | |
| } | |
| // no -1 founds, calc average | |
| if (emptyInd == -1) { | |
| long int sumRpm = 0; | |
| for (byte i=0; i<sampleSize; i++) { | |
| sumRpm += rpmSamples[i]; | |
| rpmSamples[i] = -1; | |
| } | |
| avgRpm = int(sumRpm / float(sampleSize)); | |
| // found a -1, so replace it w/ latest fan reading | |
| } else if (emptyInd > -1) { | |
| rpmSamples[emptyInd] = fanRpm; | |
| } | |
| // send average speed to 4 digi - 7 seggie | |
| displayDigits(avgRpm); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks complicated