Advertisement
sanpai

Tachomter Completely Working Code

Apr 28th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1.  
  2. #include <LiquidCrystal.h>
  3. LiquidCrystal lcd(50, 51, 49, 48, 47, 46);
  4.  
  5. int rpmcount = 0;
  6. int rpm = 0;
  7. unsigned long lastmillis = 0;
  8. void setup(){
  9. Serial.begin(9600);
  10. attachInterrupt(2, rpm_fan, FALLING);
  11. lcd.begin(16,2);
  12. lcd.print("Current RPM:");
  13. }
  14. void loop(){
  15. if (millis() - lastmillis == 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).
  16. detachInterrupt(0);//Disable interrupt when calculating
  17. rpm = rpmcount*30; // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.
  18. lcd.setCursor(0, 1);
  19. lcd.print(" ");
  20. lcd.setCursor(0, 1);
  21. lcd.print(rpm);
  22. Serial.print("RPM =\t"); //print the word "RPM" and tab.
  23. Serial.print(rpm); // print the rpm value.
  24. Serial.print("\t Hz=\t"); //print the word "Hz".
  25. Serial.println(rpmcount); //print revolutions per second or Hz. And print new line or enter.
  26. rpmcount = 0; // Restart the RPM counter
  27. lastmillis = millis(); // Uptade lasmillis
  28. attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
  29. }
  30. }
  31. // this code will be executed every time the interrupt 0 (pin2) gets low.
  32. void rpm_fan(){
  33. rpmcount++;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement