XRAD's confusion on saving a global variable running avg

I am using a sensor to asses luminosity. I am using this running avg code Arduino Playground - RunningAverage . I have no issue getting the running avg of the light sensor. For my project, I need to compare last reading to new reading. However, I cannot save (myRA.getAverage(), 3) or (myRA.getAverage(), 0) as a uint16_t global variable...... Although, I can print it no issue.

Any help appreciated!

//
//    FILE: runningAverageTest.pde
//  AUTHOR: Rob Tillaart
//    DATE: 2012-12-30
//
// PUPROSE: show working of runningAverage
//

#include "RunningAverage.h"

RunningAverage myRA(10);
int samples = 0;

void setup(void) 
{
  Serial.begin(115200);
  Serial.println("Demo RunningAverage lib");
  Serial.print("Version: ");
  Serial.println(RUNNINGAVERAGE_LIB_VERSION);
  myRA.clear(); // explicitly start clean
}

void loop(void) 
{
  long rn = random(0, 1000);
  myRA.addValue(rn * 0.001);
  samples++;
  Serial.print("Running Average: ");
  Serial.println(myRA.getAverage(), 3);

  if (samples == 300)
  {
    samples = 0;
    myRA.clear();
  }
  delay(100);
}

Try

float f = myRA.getAverage();
Serial.println( f, 3 );

Why not?

Note: the 'comma expressions' "(myRA.getAverage(), 3)" and "(myRA.getAverage(), 0)" are both the same as saying "myRA.getAverage()". There is nothing magical about the comma. It just separates two arguments to the .print() function. It does nothing to the float value. If the first argument to .print() is a 'float' value, the optional second argument tells .print() how many digits to show after the decimal point. The default is 2.

Thank you both! I did not even try breaking up the args :roll_eyes:. I did set to '0' second arg to get rid of x2 place holders....I will try 'float' too....will give it a shot when I get off work.....

Thank you 'guix' and 'john'! your suggestions worked just fine. sample of functional loop:

void loop()
{

  TCA9548A(0);//bus '0'
  //Serial.println("  RIGHT EYE");
  simpleRead();
  rightEye = lightX;

  long rn = rightEye;
  RIGHTEYE.addValue(rn);//(rn * 0.001);
  samples++;
  Serial.print(samples);
  Serial.print("\t");
  Serial.print(RIGHTEYE.getMin(), 0);
  Serial.print("\t");
  Serial.print(RIGHTEYE.getAverage(), 0);
  Serial.print("\t");
  Serial.println(RIGHTEYE.getMax(), 0);

  if (samples >= 10) {
    if (RIGHTEYE.getMax() > RIGHTEYE.getMin()) {//only do this if light increases
      percentChange = (((RIGHTEYE.getMax()) - (RIGHTEYE.getMin())) /  RIGHTEYE.getMin()) * 100.0;
      Serial.print("% change RIGHT: ");
      Serial.println(percentChange);
      //percentChange = 0;
      samples = 0;
      RIGHTEYE.clear();
      Serial.println("\nCNT\tMIN\tAVG\tMAX");
    }

    if (percentChange > 5) {
      for (int i = 0; i < NUMPIXELS; i++ )
        leds[i].setRGB(255, 255, 255);  // grb
      FastLED.show();
    }

    else if (percentChange <= 5) {
      for (int i = 0; i < NUMPIXELS; i++ )
        leds[i].setRGB(0, 255, 0);  // grb
      FastLED.show();
    }

    percentChange = 0;// not sure if needed yet..
    //delay(10);
  }
}

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.