Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(50, 51, 49, 48, 47, 46);
- int rpmcount = 0;
- int rpm = 0;
- unsigned long lastmillis = 0;
- void setup(){
- Serial.begin(9600);
- attachInterrupt(2, rpm_fan, FALLING);
- lcd.begin(16,2);
- lcd.print("Current RPM:");
- }
- void loop(){
- if (millis() - lastmillis == 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).
- detachInterrupt(0);//Disable interrupt when calculating
- 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.
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print(rpm);
- Serial.print("RPM =\t"); //print the word "RPM" and tab.
- Serial.print(rpm); // print the rpm value.
- Serial.print("\t Hz=\t"); //print the word "Hz".
- Serial.println(rpmcount); //print revolutions per second or Hz. And print new line or enter.
- rpmcount = 0; // Restart the RPM counter
- lastmillis = millis(); // Uptade lasmillis
- attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
- }
- }
- // this code will be executed every time the interrupt 0 (pin2) gets low.
- void rpm_fan(){
- rpmcount++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement