Last active
August 29, 2015 14:02
-
-
Save 0xPIT/95a994b5ded9125f543e to your computer and use it in GitHub Desktop.
variant: update one digit per loop
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
// ---------------------------------------------------------------------------- | |
// RPM Counter | |
// ---------------------------------------------------------------------------- | |
// https://www.pjrc.com/teensy/td_libs_FreqCount.html | |
#include <FreqCount.h> | |
// Leonardo: Timer interrupt auf Pin 12 | |
// ---------------------------------------------------------------------------- | |
// configuration for 7segment display | |
// 6 = B, 7 = A , 8 = F, 9 = G, 10 = E, 11 = D, 12 = C, 13 = DP | |
uint8_t Segments[8] = { 7, 6, 12, 11, 10, 8, 9, 13 }; | |
uint8_t Numbers[10][8] = { | |
{ 1, 1, 1, 1, 1, 1, 0, 0 }, // 0 | |
{ 0, 1, 1, 0, 0, 0, 0, 0 }, // 1 | |
{ 1, 1, 0, 1, 1, 0, 1, 0 }, // 2 | |
{ 1, 1, 1, 1, 0, 0, 1, 0 }, // 3 | |
{ 0, 1, 1, 0, 0, 1, 1, 0 }, // 4 | |
{ 1, 0, 1, 1, 0, 1, 1, 0 }, // 5 | |
{ 1, 0, 1, 1, 1, 1, 1, 0 }, // 6 | |
{ 1, 1, 1, 0, 0, 0, 0, 0 }, // 7 | |
{ 1, 1, 1, 1, 1, 1, 1, 0 }, // 8 | |
{ 1, 1, 1, 1, 0, 1, 1, 0 } // 9 | |
}; | |
uint8_t DigitCommon[4] = { 5, 4, 3, 2 }; // 5, 4, 3, 2 from front to back | |
// ---------------------------------------------------------------------------- | |
void setup() { | |
uint8_t c; | |
Serial.begin(57600); | |
while (!Serial); | |
for (c = 0; c < sizeof(DigitCommon) / sizeof(uint8_t); c++) { | |
pinMode(DigitCommon[c], OUTPUT); | |
digitalWrite(DigitCommon[c], LOW); | |
} | |
for (c = 0; c < sizeof(Segments) / sizeof(uint8_t); c++) { | |
pinMode(Segments[c], OUTPUT); | |
digitalWrite(Segments[c], HIGH); | |
} | |
FreqCount.begin(200); // gate time in ms | |
} | |
// ---------------------------------------------------------------------------- | |
void showDigit(uint8_t value, uint8_t digit) { | |
digitalWrite(DigitCommon[digit], HIGH); | |
for (uint8_t segment = 0; segment < 8; segment++) { | |
digitalWrite(Segments[segment], (Numbers[value][segment] ? LOW : HIGH)); | |
} | |
digitalWrite(DigitCommon[digit], LOW); | |
} | |
// ---------------------------------------------------------------------------- | |
long lpow(int base, int exponent) { | |
long result = 1; | |
while (exponent) { | |
if (exponent & 1) { | |
result *= base; | |
} | |
exponent >>= 1; | |
base *= base; | |
} | |
return result; | |
} | |
// ---------------------------------------------------------------------------- | |
uint32_t previousMeasure; | |
uint32_t currentMeasure; | |
uint32_t rpm = 42; | |
int32_t delta; | |
uint8_t digit; | |
void loop() { | |
if (FreqCount.available()) { | |
currentMeasure = FreqCount.read(); | |
} | |
delta = currentMeasure - previousMeasure; | |
if (abs(delta) > 1) { | |
previousMeasure = currentMeasure; | |
rpm = currentMeasure * 5; // TODO hier umrechnen und/oder Gate Time in setup() anpassen | |
Serial.print("RPM: "); | |
Serial.println(rpm); | |
digit = 0; // reset display | |
} | |
// update only one digit per loop iteration for faster multiplex | |
showDigit((rpm / lpow(10, digit)) % 10, digit); | |
digit = ++digit % 4; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment