#include <runningAvg.h>
#include <timeObj.h>

/*
  Running average.

  This class was developed as a super simple to use data smoother. You decide, when creating
  it, how many data points you would like to smooth your data over. Then each time you add a
  data point it returns the running average of the last n points that were added. Simple to
  use, easy to understand.

  Enjoy!

  -jim lee

*/

runningAvg  smoother(10);    // Our smoother. You can change the number of datapoints it will act on.
bool        sawEnd;
timeObj     checkTimer(100);

// Standard setup stuff..
void setup(void) {

   Serial.begin(57600);						                      // Fire up the serial stuff.
   sawEnd = true;
}


// Standard loop stuff..
void loop(void) {
   
   int    rawValue;
   float  ave;

   if (checkTimer.ding()) {
    rawValue = analogRead(A0);
    ave = smoother.addData(rawValue);     // Pop this number into our smoother. Out pops the average.
    if (!smoother.getDelta()) {           // It also will give us delta over the data set.
       if (!sawEnd) {
          Serial.print("Endpoint : ");
          Serial.println(rawValue);
          sawEnd = true;
       }
    } else {
        sawEnd = false;
    }
    checkTimer.start();
   }
}