Need help for a calculation

Hi

I´m building a "Sukitronic" for my MC with some basic info's to be read on a lcd located on dashboard.
Build on a Nano every board.
One info will be gear indication.
I have a tacho signal from engine and from output shaft - transmission. So I will be able to calculate the ratio to determine the gear.
But I need help for the math to include a percentage calc.

Example:
5th gear has a ratio on 0,468. 3800rpm input gives 1778rpm output. 1778/3800 = 0,468
6th gear has a ratio on 0,523. 3800rpm input gives 1987rpm output. 1987/3800 = 0,523

If I would like to calculate if the actual ratio is within 5% of theoretic ratio, how could I do that the most simple and return if its true or false?

Like:
if (1778 / 3800 == 0,468(+- 5%)) return True
else return False

Thanks
Brian

consider

bool
isEqualPct (
    float val0,
    float val1,
    float pct )
{
    float  d = val1 * pct / 100;
    if ((val1 - d) <= val0 && val0 <= val1 + d)
        return true;
    return false;
}

// -----------------------------------------------------------------------------
void
test (
    float val0,
    float val1,
    float pct )
{
    char s [80];
    char str0 [10];
    char str1 [10];
    char str2 [10];

    dtostrf(val0, 6, 3, str0);
    dtostrf(val1, 6, 3, str1);
    dtostrf(pct,  4, 1, str2);

    sprintf (s, "test: val0 %s, val1 %s, pct %s  %s", str0, str1, str2,
        isEqualPct (val0, val1, pct) ? "true" : "false");
    Serial.println (s);
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    test (1778.0 / 3800, 0.468, 5);
    test (0.444, 0.468, 5);
    test (0.445, 0.468, 5);
    test (0.491, 0.468, 5);
    test (0.492, 0.468, 5);
}

// -----------------------------------------------------------------------------
void loop () {
}

This is closer to your example so it may help you understand it, but it is not an efficient or complete solution such as @gcjr has presented:

Like:

if (1778 / 3800 == 0,468(+- 5%)) return True
else return False


Like:

if ( 1778.0 / 3800 > 0.468 * (95.0 / 100)   &&  1778.0 / 3800 < 0.468 * ( 105.0 / 100) ) return True
else return False

The ‘.0’ in, for example, 95.0 forces a floating point instead of integer calculation.

Edit:

You could probably have a function:

byte gear( float engineSpeed , float shaftSpeed ) {
// if engineSpeed > 0 and shaftSpeed > 0 returns the gear (assuming the clutch is not slipping)

}

long 3800 * 100 / 1987 = 191, long 3800 * 100 / 1778 = 213.
Maybe: :slight_smile:

long ratio =  inRPM * 100 / outRPM;
If(ratio >  181 && ratio < 200)
  gear = 5;
else if(ratio > 202 && ratio < 224)
  gear = 6;
else if( // blah, blah

Thanks for the replies.

That gave me something to work with. Nice with different approaches, good training for a newbie.

@6v6gt
I actually did have a slipping clutch.
New friction plates and good engine oil didn't take it all, so I'v got some heavy duty racing springs installed also.
That works, but now I'm also about to get a left forearm as Hulk ;).