#include "NixieTube.h"
#include "math.h"
const int COUNT = 2;
const float TIRE_DIAMETER_IN_INCHES = 21.89;
const int HALL_SENSOR = 2;
const float PI_APPROX = 3.1415;
const float MM_PER_INCH = 25.4;
const unsigned int MS_PER_HOUR = 3600000;
const unsigned int MM_PER_KM = 1000000;
const float KM_PER_MILE = 1.60934;
bool triggered = false;
unsigned long elapsed = 0;
float tireCircInMm = 0;
unsigned int MAX_SPEEDO_VAL = pow(10, COUNT);
NixieTube tube(11, 12, 13, 10, COUNT);
// pin_ds, pin_st. pin_sh, pin_oe(pwm pin is preferred), COUNT
void speedoTrigger()
{
triggered = true;
}
void setup()
{
pinMode(HALL_SENSOR, INPUT); //setup the pin to accept input from the hall sensor
attachInterrupt(0, speedoTrigger, RISING); //speedometer sensor interrupt
tireCircInMm = TIRE_DIAMETER_IN_INCHES * PI_APPROX * MM_PER_INCH; //get the diameter in mm
for(byte i=0; i < COUNT; i++)
{
tube.setBackgroundColor(i, Blue); // set background LED light to blue for all of the nixies
}
tube.setBrightness(0xff); // brightness control, 0x00(off)-0xff
tube.display(); //turn it on
}
void loop()
{
if(triggered)
{
unsigned int pulsesPerHour = 0;
float kmPerHour = 0;
float mph = 0;
triggered = false; //reset interrupt value so we can come back to this code the next time the wheel goes around
elapsed = millis() - elapsed; //get the number of milliseconds that have elapsed since the last trigger event
pulsesPerHour = MS_PER_HOUR / elapsed; //number of pulses per hour at the current rate
kmPerHour = pulsesPerHour * tireCircInMm / MM_PER_KM; //1000000 is the number of mm in a km
mph = kmPerHour / KM_PER_MILE; //convert from km per hour to miles per hour
while(mph > MAX_SPEEDO_VAL) //reduce to the correct number of significant digits so we don't overflow the display (e.g. two nixies mean 99 max, three nixies 999 max)
{
mph = mph - MAX_SPEEDO_VAL;
}
tube.printf("%i", (int)mph); //output the value to the nixie(s) in serial mode
tube.display(); //show it
}
}