Skip to content

Instantly share code, notes, and snippets.

@gullinbursti
Created March 25, 2021 22:23
Show Gist options
  • Select an option

  • Save gullinbursti/aa5dfb29bdde9368dd06f1116b48d3c8 to your computer and use it in GitHub Desktop.

Select an option

Save gullinbursti/aa5dfb29bdde9368dd06f1116b48d3c8 to your computer and use it in GitHub Desktop.
Calc average value from given sample size w/ arduino-c
// 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);
@sachio222

Copy link
Copy Markdown

Looks complicated

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