taking a reading if the sensor detects more than the moving average

I’m using the Moving Average Library to take the average of readings from a sensor (an air pressure sensor), and display on the monitor if there is a “spike” of more than 2 from the average. But it’s not working -

the code runs, prints the initial reading to the monitor, but nothing more happens. I have tested and the sensor is working fine - what am I doing wrong?

/* ---------------------------------------------------------------
    I am trying to record a reading when the reading from the air
       pressure sensor jumps suddenly. Pressure Sensor is a MPX5500DP 
    I am using the movingAvg library to simplify the maths a bit
   ---------------------------------------------------------------*/

#include <movingAvg.h>                  // https://github.com/JChristensen/movingAvg

const uint8_t airSensor(A0);           // connect pressure sensor from A0 pin to ground
movingAvg airAvg(10);                 // define the moving average object


/* --------------------------------------------
/*  setup the average, pullup the air sensor, 
 *   begin the serial monitor and show an initial 
 *   reading so we knnow it is working
   --------------------------------------------*/
void setup()
{
    airAvg.begin();
    pinMode(airSensor, INPUT_PULLUP);
    Serial.begin(9600);
    Serial.println("initial reading");
    int pc = analogRead(airSensor); // read the sensor
    Serial.println(pc);               // print it to serial monitor

}
/* --------------------------------------------
/*  Each loop should comapare the reading against 
 *   the moving average, and if it is greater than 
 *   the specific amount, print this to the monitor
   --------------------------------------------*/
void loop()
{
    int    pc       = analogRead(airSensor);   // read the sensor
    int    avg      = airAvg.reading(pc);      // calculate the moving average
    int    avgPlus2 = avg + 2;               // to simplify conditional below
    
    // if the reading is greater than the average, print it
    if (pc > avgPlus2)                                         
    {                                            
           Serial.print("reading - ");
           Serial.print(pc);          // print the individual reading
           Serial.print(", avg - ");
           Serial.println(avg);       // print the moving average
    }

}

When debugging problems with conditional statements it is often useful to print the values being tested to see whether they look reasonable. Have you tried that ?

  pinMode(airSensor, INPUT_PULLUP);

Looks very odd for an analogue input

cheers for the quick response - I've removed the IF statement so it just runs through what the sensor value is, and what the running average is, and I can trigger the sensor and it all looks fine. I don't understand why the IF statement doesn't seem to be working!

You never read a value in “loop”.

Paul

Paul_KD7HB:
You never read a value in "loop".

Paul

void loop()
{
    int    pc       = analogRead(airSensor);   // read the sensor

I don't understand, every example I look at appears to read the sensor in the "loop". Can you elaborate a little more?

eg

https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/

void loop()
{
  int pc  = analogRead(airSensor);

Yes, you are clearly reading the sensor in loop()

As suggested in reply #1, please add some Serial prints before the conditional statement to verify that the values are what you think. What do the print outs show?

/* ---------------------------------------------------------------
    I am trying to record a reading when the reading from the air
       pressure sensor jumps suddenly. Pressure Sensor is a MPX5500DP
    I am using the movingAvg library to simplify the maths a bit
   ---------------------------------------------------------------*/

#include <movingAvg.h>                  // https://github.com/JChristensen/movingAvg

const uint8_t airSensor(A0);           // connect pressure sensor from A0 pin to ground
movingAvg airAvg(10);                 // define the moving average object


/* --------------------------------------------
  /*  setup the average, pullup the air sensor,
     begin the serial monitor and show an initial
     reading so we knnow it is working
   --------------------------------------------*/
void setup()
{
  airAvg.begin();
  pinMode(airSensor, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("initial reading");
  int pc = analogRead(airSensor); // read the sensor
  Serial.println(pc);               // print it to serial monitor

}
/* --------------------------------------------
  /*  Each loop should comapare the reading against
     the moving average, and if it is greater than
     the specific amount, print this to the monitor
   --------------------------------------------*/
void loop()
{
  int    pc       = analogRead(airSensor);   // read the sensor
  int    avg      = airAvg.reading(pc);      // calculate the moving average
  int    avgPlus2 = avg + 2;               // to simplify conditional below
  Serial.print("reading - ");
  Serial.print(pc);          // print the individual reading
  Serial.print(", avg - ");
  Serial.print(avg);       // print the moving average
  Serial.print(", avg +2 - ");
  Serial.println(avgPlus2);

  // if the reading is greater than the average, print it
  if (pc > avgPlus2)
  {
    Serial.print("reading - ");
    Serial.print(pc);          // print the individual reading
    Serial.print(", avg - ");
    Serial.println(avg);       // print the moving average
  }
}