#include <runningAvg.h>

/*
  Running average.

  This class was originally 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(5);   // Our smoother. You can change the number of datapoints it will act on.
char        inBuff[80];    // A char buffer (c string) to hold your typings.
int         index;         // An index to be used in storing charactors into our char buffer.


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

   Serial.begin(9600);						// Fire up the serial stuff.
   inBuff[0] = '\0';                   // Clear the c string.
   index = 0;                          // The next char we read in goes here.
   Serial.println(F("Enter numbers, one at a time"));	// Tell Mrs user to start inputting numbers.
   Serial.println(F("This will average the last 5 you entered."));	//
}


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

   char  aChar;   // A char to catch your typings in.
   float aValue;  // The float version of the number you typed.
   float ave;     // The Average that the smoother will return to us.
   
   if (Serial.available()) {                    // If there is a char waiting to be read..
      aChar = Serial.read();                    // Grab and save the char.
      if (aChar=='\n') {                        // If its a newline char.. (Make sure the serial monitor is set to newline.)
         aValue = atof(inBuff);                 // Decode what we have read in so far as a float value. (Decimal number)
         ave = smoother.addData(aValue);        // Pop this number into our smoother. (Running average object.) Out pops the average.
         Serial.println();                      // From here down its just grabing info from the
         Serial.print(F("Entered    : "));      // SMoother and displaying it on the serial monitor.
         Serial.println(aValue,4);
         Serial.print(F("Average   : "));
         Serial.println(ave,4);
         Serial.println(F("--------------"));
         inBuff[0] = '\0';								// Clear the inBuff string.
         index = 0;										// Reset the index to start a new number string.
      } else {                                  // Else, it wasn't a newline char..
         inBuff[index++] = aChar;               // So we save the char we got into the c string.
         inBuff[index] = '\0';                  // And pop an end of string after it.
      }
   }
}