Calculations

Hi, Im building an canbus display for obd2 data. Im struggling with calculation of the data.

The OBD2 pids says that i have to calculate the incoming data this way:

2
---------(256a+b)
65536

Where a and b is the incoming data.

It is the data for oxygen sensor fuel-air equivalence ratio

Best regards

What's the specific problem?

You have two bytes, A and B that came in serially. They represent what appears to be a 16-bit unsigned number. In the equation, the "256A+B" integrates the two individual bytes back into a 16-bit unsigned value. It's the same as:

unValue = (A << 8) + B;

If A = ABh and B = 5Ah then the combined 16-bit value is AB5Ah.

The rest is just arithmetic, which I would do using floats and casts as necessary:

void setup() 
{
    byte
        A, B;
    unsigned int
        unValue;
    float
        fResult;

    A = 0xAB;
    B = 0x5A;
    
    Serial.begin(9600);
    unValue = (A<<8)+B;
    fResult = 2.0 * (float)unValue / 65536.0;

    Serial.print( "A: " ); Serial.println( A, HEX );
    Serial.print( "B: " ); Serial.println( B, HEX );
    Serial.print( "unValue: " ); Serial.println( unValue, HEX );
    Serial.print( "fResult: " ); Serial.println( fResult, 3 );
    

}//setup

void loop() 
{

}//loop

Output:

A: AB
B: 5A
unValue: AB5A
fResult: 1.339

Verification:

AB5A == 43866d

2.0 * 43866.0 / 65536.0 = 1.38868

Thanks alot. Sorri, my programing skills are limited. i just started.