I managed to display instant fuel consumption in a hour and per 100 km, but i am struggling with average fuel consumption in 100 km.
Fuel consumption is in byte 2 and 3 and it is in microlitres.
Roll over should be somewhere 32767, when value is about that 65536 then it goes to back ~ 32000 and i wonder why it does not go back to zero.
I tried to make it with same principle as johnwasser advised to do with distance counter, but no success.
If i am not badly wrong this does not tell average consumption,
trip average consumption = 100 * instant consumption in hour / kilometers
It need to do like this: Average = 100 * fuel used / km.
So it is necessary to store used fuel somehow first.
With this it it add newFuel value to totalFuelUsed which is not correct because newFuel is too small value if comparing to fuelNow.
fuelCountLitres should be same as totalFuelUsed until 32767 is reached, newFuel should go to zero, and litres should store to fuelCountlitres but does not happen correctly.
newFuel is a local variable (and only known inside the if). Everytime that you go through loop() and the if (rxId == 0x480) evaluates to true, your variable is recreated and contains fuel_now.
if i want to store litres to variable, how i prevent it incrementing value when litres does not change?
totalFuelUsed += fuelCountLitres
I want to store litres before roll over, it happens when litres are 0,06 then it start counting from 0.
If litres are for example 0,01 then totalFuelUsed has something like 0,04 and when litres change from 0,01 to 0,02 then it keeps adding that 0,02 to totalFuelUsed 0,04+0,02+0,02... Again when litres change to 0,03 there is already much more in totalFuelUsed. How to prevent this?
To the next problem...
I tried to understand from other posts how to avoid that: invalid operands of types 'double' and 'int' to binary 'operator& '.
Fuel consumption is in double variable, so, should i send that to dash app in 4 bytes?
With this it is sent to app, but fuel consumption and average speed values fluctuate fast there. For example fc value goes back and forth 45-50 and very fast.
// build 8th CAN frame, trip1 & avg speed
buf[0] = (trip1 & 0xff);
buf[1] = (trip1 >> 8 & 0xff);
buf[2] = averageTrip1Speed;
buf[3] = (0xff);
buf[4] = (0xff);
buf[5] = (0xff);
buf[6] = (0xff);
buf[7] = (0xff);
// write 8th CAN frame to serial
SendCANFrameToSerial(07, buf);
// build 9th CAN frame, fuel consumption
memcpy(buf, &trip1FuelConsumption, 4);
// write 9th CAN frame to serial
SendCANFrameToSerial(08, buf);
Because there is no target IDs for fuel consumption and average speed, need to name own, then can map these in dash app.
I get values to text gauges, but fc and avg speed fluctuate fast for some reason.