void loop() {
if (Trigger) { // Print data if the "Trigger" is set
// calculate average speed for printing
Speed = int((Freq + FreqPrev1 + FreqPrev2) / 3);
// print the speed in big font
Speed = Speed % 1000; // drop any digits above 999
printbigchar(int(Speed / 100), 0); // print the speed hundreds
Speed = Speed % 100; // drop any digits above 99
printbigchar(int(Speed / 10), 1); // print the speed tens
Speed = Speed % 10; // drop any digits above 9
printbigchar(Speed, 2); // print the speed ones
// Main odometer update
TotalClicks += Freq; // Add Freq to TotalClicks
while (TotalClicks >= CalDist) { // Check if TotalClicks has reached CalDist (1/10 mile travelled)
TotalTenths ++; // increment TotalTenths and
TotalClicks -=CalDist; // reduce TotalClicks by CalDist
}
while (TotalTenths >= 10) { // Check if TotalTenths has reached 10
TotalMiles ++; // if so, increment TotalMiles and
TotalTenths -= 10; // decrease TotalTenths by 10
}
while (TotalMiles > 99999) { // Check if TotalMiles has reached 100000
TotalMiles -= 100000; // if so, decrease TotalMiles by 100000 (rollover odometer)
}
// Trip odometer update
DistanceClicks += Freq; // Add Freq to DistanceClicks
while (DistanceClicks >= CalDist) { // Check if DistanceClicks has reached CalDist (1/10 mile travelled)
DistanceTenths ++; // if so, increment DistanceTenths and
DistanceClicks -=CalDist; // reduce DistanceClicks by CalDist
}
while (DistanceTenths >= 10) { // Check if DistanceTenths has reached 10
DistanceMiles ++; // if so, increment DistanceMiles and
DistanceTenths -= 10; // decrease DistanceTenths by 10
}
while (DistanceMiles > 999) { // Check if DistanceMiles has reached 1000
DistanceMiles -= 1000; // if so, decrease DistanceMiles by 1000 (rollover odometer)
}
if (MenuMode == 0) { // if we're not in a menu
printmileage(); // then print the odometer
}
// foot distance display mode
// DistanceFeetTenths is value is 100 times the actual value. This to allow for
// much greater precision while avoiding the use of a float variable.
if ((MenuMode == 2) && MenuModeActive) { // are we in foot distance display mode? If so...
DistanceFeetTenths += (Freq * FeetScaleFactor); // Update DistanceFeetTenths
while (DistanceFeetTenths >= 1000) { // Has the tenths digit overflowed (exceeded 10.00)?
DistanceFeet ++; // If so, increment DistanceFeet by 1,
DistanceFeetTenths -= 1000; // and decrease tenths digit by 1000 (represending 10.00)
}
while (DistanceFeet > 99999) { // Has DistanceFeet reached 100000?
DistanceFeet -= 100000; // If so, roll it over (drop it by 100000 feet)
}
printfeet(); // print the distance in feet
}
// copy speed values for use next cycle
FreqPrev2 = FreqPrev1;
FreqPrev1 = Freq;
Trigger = false; // Reset "Trigger"...until TimerCount reaches CalValue again
}
// Read the pushbutton switches for user input
Button1State = digitalRead(Button1); // Read the state of Button1
Button2State = digitalRead(Button2); // Read the state of Button2
Button3State = digitalRead(Button3); // Read the state of Button3
Button4State = digitalRead(Button4); // Read the state of Button4
// ButtonPressed is true if any buttons are pressed, otherwise it's false
ButtonPressed = !(Button1State && Button2State && Button3State && Button4State);
// If a button was just pressed for the first time then reset the ButtonsOffTime timer
if ((ButtonPressed == false) && (ButtonPressedPrev == true)) {
ButtonsOffTime = millis();
}