# Problem facing in RPM Sensing with mega2560 and LM393 Optical coupling sensor

Your new calc isn't going to work using two byte unsigned integers. You are multiplying 95 by 6000 which won't fit. I'd suggest that you return to the prior math for now.

wildbill:
Your new calc isn't going to work using two byte unsigned integers. You are multiplying 95 by 6000 which won't fit. I'd suggest that you return to the prior math for now.

Hi wildbill you are absolutely right.
so defining

``````unsigned long pulsesOfPeriod;[color=#222222][/color]
unsigned long the_real_RPM;
``````

``````int pulsesOfPeriod;[color=#222222][/color]
int the_real_RPM;
``````

should give plenty of room for big numbers.

if lower rpms should be measured with more precision you could use a longer timeintervall in the
if ( TimePeriodIsOver(RPM_Timer, 5000)

which means every 5 seconds which is five times longer than 1000 so the calculation has to be

``````the_real_RPM = pulsesOfPeriod * 3000 / TimeDifferenceSinceLastPeriod;
``````

in your last code you calculate

``````the_real_RPM = pulsesOfPeriod * 6000 / TimeDifferenceSinceLastPeriod;
``````

why 6000 ? it should be 3000 ???
best regards Stefan

best regards Stefan

StefanL38:
Hi wildbill you are absolutely right.
so defining

``````unsigned long pulsesOfPeriod;[color=#222222][/color]
``````

unsigned long the_real_RPM;

``````

``````
``````int pulsesOfPeriod;[color=#222222][/color]
int the_real_RPM;
``````

After the modification its working .

in your last code you calculate

``````the_real_RPM = pulsesOfPeriod * 6000 / TimeDifferenceSinceLastPeriod;
``````

why 6000 ? it should be 3000 ???

I think if time period increase ,need to multiply with 3.I tried to make the program with 2 Sec (2000Msec) Time period and its multiply with 3 that is 2000*3=6000,But its not working.By mistaken i have not deleted the value 6000 before posting.

the if-condition

``````if (  TimePeriodIsOver(RPM_Timer, 1000)  ) // 1000 means 1000 milliseconds = 1 second
``````

contains the timing constant. And the comment tries to explain it

if you want a 2 second intervall which is 2000 milliseconds you would code

``````if (  TimePeriodIsOver(RPM_Timer, 2000)  ) // 2000 means 2000 milliseconds = 2 seconds
``````

a more conventient way is to define another constant instead of a hard coded number and do the rpm-calculation including that constant

you can define at the top of your code outside any function to make it a global constant

``````const unsigned long MyIntervalInMSecs = 2000;
``````

the if-condition is then

``````if (  TimePeriodIsOver(RPM_Timer, MyIntervalInMSecs )  )
``````

then the calculation of the rpm has to be modified to

``````the_real_RPM = pulsesOfPeriod * 3 * MyIntervalInMSecs  /  TimeDifferenceSinceLastPeriod;
``````

so the if-condition that does the timing and the calculation use the same value

whenever you change the number in the line

``````const unsigned long MyIntervalInMSecs = 2000;
``````

all places that need this number "Know" about the new value because you are using a constant
You can do the same thing with variables.

best regards Stefan