Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. const byte tachInterrupt = 1; // tach signal on interrupt 1 (digital pin 3)
  2. byte engineCylinders = 8; // for tach calculation (pulses per revolution = 2 * cylinders / cycles)
  3. byte engineCycles = 4; // for tach calculation
  4. int refreshInterval = 750; // milliseconds between sensor updates
  5. unsigned long previousMillis = 0;
  6.  
  7. volatile int RPMpulses = 0;
  8.  
  9. void setup()
  10. {
  11. pinMode(tachPin, INPUT_PULLUP); // enable internal pullup for tach pin
  12. attachInterrupt(tachInterrupt, countRPM, FALLING);
  13. Serial.begin(9600);
  14. }
  15.  
  16. void loop()
  17. {
  18. if(millis() - previousMillis > refreshInterval)
  19. {
  20. previousMillis = millis();
  21. Serial.println(getRPM());
  22. }
  23. }
  24.  
  25. // counts tach pulses
  26. void countRPM()
  27. {
  28. RPMpulses++;
  29. }
  30.  
  31. // checks accumulated tach signal pulses and calculates engine speed
  32. // returns engine speed in RPM
  33. // Resolution: 30000 * engineCycles / refreshInterval / engineCylinders RPM (for default values = 20 RPM)
  34. int getRPM()
  35. {
  36. int RPM = int(RPMpulses * (60000.0 / float(refreshInterval)) * engineCycles / engineCylinders / 2.0 ); // calculate RPM
  37. RPMpulses = 0; // reset pulse count to 0
  38. RPM = min(9999, RPM); // don't return value larger than 9999
  39. return RPM;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement