Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- volatile byte rpmcount;
- volatile int status;
- unsigned int rpm;
- unsigned long timeold;
- void rpm_fun()
- {
- //Each rotation, this interrupt function is run twice, so take that into consideration for
- //calculating RPM
- //Update count
- rpmcount++;
- }
- void setup()
- {
- lcd.begin(20, 4);
- //Interrupt 0 is digital pin 2
- //Triggers on FALLING (change from HIGH to LOW)
- attachInterrupt(2, rpm_fun, FALLING);
- rpmcount = 0;
- rpm = 0;
- timeold = 0;
- status = LOW;
- }
- void loop()
- {
- //Update RPM every second
- delay(1000);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print(" RPM: ");
- //Don't process interrupts during calculations
- detachInterrupt(2);
- //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
- //happened once per revolution instead of twice. Other multiples could be used
- //for multi-bladed propellers or fans
- rpm = 30*1000/(millis() - timeold)*rpmcount;
- timeold = millis();
- rpmcount = 0;
- lcd.setCursor(14,0);
- lcd.print(rpm);
- //Restart the interrupt processing
- attachInterrupt(2, rpm_fun, FALLING);
- }
Advertisement
Add Comment
Please, Sign In to add comment