Guest User

Spikee

a guest
Aug 15th, 2011
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1.  
  2.  
  3. #include <LiquidCrystal.h>
  4. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  5.  
  6. volatile byte rpmcount;
  7. volatile int status;
  8.  
  9. unsigned int rpm;
  10.  
  11. unsigned long timeold;
  12.  
  13. void rpm_fun()
  14. {
  15. //Each rotation, this interrupt function is run twice, so take that into consideration for
  16. //calculating RPM
  17. //Update count
  18. rpmcount++;
  19.  
  20.  
  21. }
  22.  
  23. void setup()
  24. {
  25. lcd.begin(20, 4);
  26. //Interrupt 0 is digital pin 2
  27. //Triggers on FALLING (change from HIGH to LOW)
  28. attachInterrupt(2, rpm_fun, FALLING);
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. rpmcount = 0;
  36. rpm = 0;
  37. timeold = 0;
  38. status = LOW;
  39. }
  40.  
  41. void loop()
  42. {
  43. //Update RPM every second
  44. delay(1000);
  45. lcd.clear();
  46. lcd.setCursor(0,0);
  47. lcd.print(" RPM: ");
  48. //Don't process interrupts during calculations
  49. detachInterrupt(2);
  50. //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
  51. //happened once per revolution instead of twice. Other multiples could be used
  52. //for multi-bladed propellers or fans
  53. rpm = 30*1000/(millis() - timeold)*rpmcount;
  54. timeold = millis();
  55. rpmcount = 0;
  56.  
  57. lcd.setCursor(14,0);
  58. lcd.print(rpm);
  59.  
  60.  
  61.  
  62. //Restart the interrupt processing
  63. attachInterrupt(2, rpm_fun, FALLING);
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment