Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const byte tachInterrupt = 1; // tach signal on interrupt 1 (digital pin 3)
- byte engineCylinders = 8; // for tach calculation (pulses per revolution = 2 * cylinders / cycles)
- byte engineCycles = 4; // for tach calculation
- int refreshInterval = 750; // milliseconds between sensor updates
- unsigned long previousMillis = 0;
- volatile int RPMpulses = 0;
- void setup()
- {
- pinMode(tachPin, INPUT_PULLUP); // enable internal pullup for tach pin
- attachInterrupt(tachInterrupt, countRPM, FALLING);
- Serial.begin(9600);
- }
- void loop()
- {
- if(millis() - previousMillis > refreshInterval)
- {
- previousMillis = millis();
- Serial.println(getRPM());
- }
- }
- // counts tach pulses
- void countRPM()
- {
- RPMpulses++;
- }
- // checks accumulated tach signal pulses and calculates engine speed
- // returns engine speed in RPM
- // Resolution: 30000 * engineCycles / refreshInterval / engineCylinders RPM (for default values = 20 RPM)
- int getRPM()
- {
- int RPM = int(RPMpulses * (60000.0 / float(refreshInterval)) * engineCycles / engineCylinders / 2.0 ); // calculate RPM
- RPMpulses = 0; // reset pulse count to 0
- RPM = min(9999, RPM); // don't return value larger than 9999
- return RPM;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement