Rotations count per time

Hey so i am making some modifications and adding to this program i found. It basicly calculates the RPM of a motor based on the interruptions on some IR sensors and displays the value on an lcd screen.

int ledPin = 13;                // IR LED connected to digital pin 13
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;

#include <LiquidCrystal.h>\
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

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(16, 2);  // intialise the LCD

   //Interrupt 0 is digital pin 2, so that is where the IR detector is connected
   //Triggers on FALLING (change from HIGH to LOW)
   attachInterrupt(0, rpm_fun, FALLING);

   //Turn on IR LED
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH);

   rpmcount = 0;
   rpm = 0;
   timeold = 0;
 }

 void loop()
 {
   //Update RPM every second
   delay(1000);
   //Don't process interrupts during calculations
   detachInterrupt(0);
   //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;

   //Print out result to lcd
   lcd.clear();
   lcd.print("RPM=");
   lcd.print(rpm);

   //Restart the interrupt processing
   attachInterrupt(0, rpm_fun, FALLING);
  }

im going to add a menu with different options. i am going to keep the option to see the RPM but also add an option to see the RPS and RPH.

for that would i simply make the following change or do i need a different set of variables for each time i want displayed?
rpm = 30*1000/(millis() - timeold)*rpmcount; to
rpm = 500/(millis() - timeold)rpmcount; for RPS and
rpm = 30
10000/(millis() - timeold)*rpmcount; for RPH

If it was my project I would put the different values in different variables. For one thing it would be easier to check the calculations if you can print them all at the same time.

Also you can have some simpilifications

rps = rpm / 60
rph = rpm * 60

Why not start with rps ?

...R

omg i feel like a big dummy! i guess i was over thinking... if i already have rpm i can just do rpm*60 for hour and rpm/60 for second like you said. its more efficient and more clean.

i guess i can start with rps but would it really make much of a difference from what you told me to add?

bathtubbuddy:
but would it really make much of a difference from what you told me to add?

NO.

But it makes it easier to develop a more complex program if you approach the problem with some logic in mind.

...R

I understand. Thank you so much Robin2. i really need to work on my logic! All i need is to keep practicing! :slight_smile: