math help

I am having an issue with an equation in arduino where the output never seems to match what I'm looking for. I keep getting 1, 10, 100 & other #'s I don't expect. I am trying to get something along these lines:
9.31, 6.42, 4.77, 3.93, 3.28, 2.79 (Gear Ratios)

Any suggestions to a better equation that will give the expected results? In the end I want to do some type of compare and get 1-6 for the above gear ratios

Here's the equation

temp = (RPM*81)/(MPH*1056);
sLCD.print(temp,2);

Heres the data types

unsigned int RPM;
unsigned int MPH;
float temp = 0;

Some of the other data if needed

RPM = hex2dec(&buffer[8],4)/4;
RAWSPD = hex2dec(&buffer[8],4);
KPH = RAWSPD/128;
MPH = (RAWSPD * 125UL) / (16UL * 1609UL);

MPH*1056

62mph?

81.0 and 1056.0

This is perhaps over the top, but the compiler will now convert them to float before doing the calculations.

temp = ( (float) RPM * 81.0) / ( (float) MPH * 1056.0);

nkotbox:
In the end I want to do some type of compare and get 1-6 for the above gear ratios

I suggest you don't put the gear ratios in the code.

A better way to do it would be to put the mid-point between adjacent gear ratios in an array. If you're representing your speed as floating point mph-per-rpm values, then it would be an array of floats.

To find the most likely gear for a given input ratio you iterate through the array until you find the first element that is greater than (or less than, if you start from the other end) your target value. The array index then tells you which gear the input ratio is closest to.

well, I tried

temp = ( (float) RPM * 81.0) / ( (float) MPH * 1056.0);

and I was still getting 100, 110, 11 - strange values
Im displaying the RPM and MPH data already so I think those values are ok.
In 4th gear (3.93) I was going 38mph at ~1800rpm's

Per the equation that should equal 3.63 but the display showed 100
It was steady on 100 in 1st
110 in 2nd
10 or 11 in 3rd
then 100 in 4th
err....... :~

nkotbox:

MPH = (RAWSPD * 125UL) / (16UL * 1609UL);

16 * 1609 = 25744 so:MPH = (RAWSPD * 125UL) /25744;
I don't know, but I would imagine that casting a fixed number (i.e. not a variable) is unneccessary. The compiler should know how to treat them.

I'm not sure, because I haven't needed to do much math with Arduino yet, but I don't think it usually likes large numbers. Maybe this might help?

http://forum.arduino.cc/index.php/topic,85692.0.html

nkotbox:
well, I tried

temp = ( (float) RPM * 81.0) / ( (float) MPH * 1056.0);

and I was still getting 100, 110, 11 - strange values
Im displaying the RPM and MPH data already so I think those values are ok.
In 4th gear (3.93) I was going 38mph at ~1800rpm's

The variable temp needs to be a float type as well, it is obvious that it is not.

Upload the whole sketch between [code] ... [/code] tags.

According to a previous post, the 'temp' is a float. Perhaps you do something with it later on that turns it into an integer ?

Does your LCD display floats correctly? Some LCD-libs don't support floats

simple test Try: sLcd.print(3.14159265, 4); should print 3.1416

lesson learned. I thought variables temp &TEMP were different in arduino but when I changed temp to Gear, it works as planned with the change I noted in an earlier post. I noted the temp gauge acting funny on a ride which is why I thought to change the name.

I will look thru the code, maybe I typed a temp somewhere in the temp gauge function.

Thanks for the help all.