View Single Post
thingstodo
Master EcoModder

Join Date: Sep 2010
Posts: 1,438

ChargE (not yet running) - '92 Mazda MX6 LX
90 day: 33.89 mpg (US)

Ford Prefect - '18 Ford F150 XLT XTR
Thanks: 735
Thanked 529 Times in 424 Posts
5.5 mAh/min

The code does not mention calibration for an amp sensor - is this what you mean?

Quote:
 // interrupt to count amp hours used ISR(TIMER1_COMPA_vect) { CurrentRead = analogRead(CurrentPin); // Convert currentread to amps CurrentRead = (CurrentRead - 512) * .78125; // Convert to AH & add to total AHused = AHused + (CurrentRead * .001); }
If that's your manual calibration, I have one suggestion. See what you think:

Arduino floating point has 6 - 7 digits of precision. Your scale constant of 0.78125 uses up 5 of those digits.

1023 Max reading - 512 = 511. * 0.78125 and *.001 gives 0.39921875, which is already over the significant digits on the Arduino. Use another variable, let's call it AHusedLow, to keep track of some extra significant digits. Since your calculated number can't go over 0.4, we'll use 0.4 as the rollover

Quote:
 CurrentRead = analogRead(CurrentPin); // Convert currentread to amps CurrentRead = (CurrentRead - 512) * .78125; // Convert to AH & add to low partion of total AHusedLow = AHusedLow + (CurrentRead * .001); // Update AHused if (AHusedLow > 0.4) { AHused = AHused + 0.4; AHusedLow = AHusedLow - 0.4; }
I've used this technique of chaining together variables to fake double-precision floating point (and triple, and quad) before (and I likely will again) and it has served me well.

This needs to be well documented so that anyone changing the code knows about the method and how it works, to make sure that the rollover is high enough that it will not be exceeded in one call and as low as possible.

That's the only code issue that jumps out at me.

Depending on how many times per second your ISR is called (I could look it up from your code ... but I'm lazy) this could be a source of error ... but as far as I can see, this should not have anything to do with your observed .0055 Ah/minute drift.