I'm trying to make a tachometer that measures the time between the rising edge of a square wave and shifts out the results to three shift registers. The 24 leds are supposed to light up in sequence like a bar graph.
After the RPM is divided by 250 (250 x 24 leds = 6000 RPM redline), the result is used to access an array containing hex values to be shifted out.
It compiles but I don't have a way to test it (Need to etch the board), I'm wondering if this looks correct to you guys, particularly the bitshifting to the second and third registers. Can you use bitshift with hex values, and am I using it correctly to output to a 24 led bargraph?
boolean Cycle = false; // set to 0 for PulseStartTime and set to 1 for each PulseEndTime
unsigned long PulseStartTime; // Saves Start of pulse in ms
unsigned long PulseEndTime; // Saves End of pulse in ms
unsigned long PulseTime; // Stores dif between start and stop of pulse
unsigned long RPM = 0; // RPM to ouptut (30*1000/PulseTime)
int shiftarray[25] = {0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF};
int latchPin = 8; //Shift register latch pin
int clockPin = 12; //Shift register clock pin
int dataPin = 11; //Shift register data pin
int ledData; //Value to access the array with
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
attachInterrupt(0, RPMPulse, RISING); // Attaches interrupt to Digital Pin 2
}
void RPMPulse()
{
if (Cycle) // Check to see if start pulse
{
PulseStartTime = millis(); // stores start time (TEST GOING TO MICROS)
Cycle = true; // sets counter for start of pulse
}
else
{
detachInterrupt(0); // Turns off inturrupt for calculations
PulseEndTime = millis(); // stores end time
Cycle = false; // resets counter for pulse cycle
calcRPM(); // call to calculate pulse time
}
}
void calcRPM()
{
PulseTime = PulseEndTime - PulseStartTime; // Gets pulse duration
RPM = 30*1000/PulseTime*2; // Calculates RPM
attachInterrupt(0, RPMPulse, RISING); // re-attaches interrupt to Digi Pin 2
}
void loop()
{
ledData = RPM/250; //Should divide RPM by 250 (On the gauge, one led is 250 RPM). Keep in mind any float values get tossed (499rpm would end up as 250)
shiftOut(dataPin, clockPin, LSBFIRST, shiftarray[ledData]);
shiftOut(dataPin, clockPin, LSBFIRST, (shiftarray[ledData] >> 8));
shiftOut(dataPin, clockPin, LSBFIRST, (shiftarray[ledData] >> 16));
delay(1000); // 1 sec delay for debugging
}