View Single Post
Old 06-12-2009, 12:09 PM   #10 (permalink)
rmccomiskie
EcoModding Lurker
 
Join Date: Aug 2008
Location: Massachusetts USA
Posts: 84

Ziggy - '95 Audi S6 Sedan

Manfred - '97 Audi A6 Quattro Sedan
90 day: 20.61 mpg (US)

Clarabell - '03 Audi A4 Quattro Avant

Sherman - '98 Audi A6 Quattro Avant

Cab - '96 Audi Cabriolet
Thanks: 0
Thanked 2 Times in 2 Posts
Quote:
Originally Posted by randerson0248 View Post
...I think the refresh rate is too fast. I see instant mileage jumping around several times per second. I think updating data every 1/2 second or so would be about right.


Software mods: MPG is a rolling average refreshed every 2 seconds, top right is miles to empty, bottom right is projected total tank miles. Running ATmega328.

Code:
//Code for display of rolling average MPG at less frequent intervals
//Based on MPGuino v0.75
//Captures the last 10 MPG values
//Calculates the average MPG for those 10 values
//Refreshes the MPG display each 2 seconds


//insert before setup()

pFunc displayFuncs[] ={ 
  ...
  doDisplayInstantA,  //insert this in displayFuncs[]


//insert in setup()

  displayFuncNames[x++]=  PSTR("InstantA "); //add this for new display template


//insert before loop(), add these global vars to retain values across loops

//mpg averaging
unsigned long instAvgMpg = 0;
unsigned long instMpg = 0;
unsigned long prevMpg = 0;
byte loopIdx = 0;
byte dispIdx = 0;


//insert in loop() right after sei();

    //accumulate mpg values in an array of 10 values, oldest values are overwritten
    instMpg = instantmpg();                      //get current mpg
    if (instMpg >= 999999000) instMpg = prevMpg; //if unbelievably high, use mpg from previous loop
    prevMpg = instMpg;                           //keep track of value for next time through loop
    avgBuff[loopIdx][mpgIdx] = instMpg;          //store the current mpg in array
    loopIdx++;                                   //increment the loop number
    if (loopIdx > 9) loopIdx = 0;                //keep track of 10 loops max
    
    //mpg averaging & display damping
    if (dispIdx <= 0) {                          //recalculate after some number of loops
      instAvgMpg = 0;                            //initialize to zero
      for (byte x=0; x<10; x++) instAvgMpg += avgBuff[x][mpgIdx]; //sum all array values
      instAvgMpg = instAvgMpg / 10;              //calculate avg of array values
      dispIdx = 4;                               //recalculate after 4 loops (~2 seconds)
    }
    dispIdx--;                                   //decrement index each time through loop


//insert new display template containing instAvgMpg

void doDisplayInstantA(){displayTripCombo('i','M',instAvgMpg,'S',instantmph(),'G','H',instantgph(),'D',current.miles());}
It works for me! YMMV.

Last edited by rmccomiskie; 06-12-2009 at 12:55 PM..
  Reply With Quote