Advertisement
sanpai

Tachometer arduino

Apr 7th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1.  
  2.  
  3. volatile byte rpmcount;
  4. unsigned int rpm;
  5. unsigned long timeold;
  6. void setup()
  7. {
  8. Serial.begin(9600);
  9. attachInterrupt(A5, rpm_fun, RISING);
  10. rpmcount = 0;
  11. rpm = 0;
  12. timeold = 0;
  13. }
  14. void loop()
  15. {
  16. if (rpmcount >= 2) {
  17. //Update RPM every 20 counts, increase this for better RPM resolution,
  18. //decrease for faster update
  19. rpm = 30*1000/(millis() - timeold)*rpmcount;
  20. timeold = millis();
  21. rpmcount = 0;
  22.  
  23. Serial.println(rpm);
  24. rpm=0;
  25. delay(100);
  26. }
  27. }
  28. void rpm_fun()
  29. {
  30. rpmcount++;
  31. //Each rotation, this interrupt function is run twice
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement