ultrasound HY-SRF05 on Arduino Nano with data stream averaging

Hi everyone,
this is my first post here, but I’ll try my best.
I searched for the answer in existing topics but with no success … so here’s my problem:

I’d like to code an object class to handle my ultrasound HY-SRF05 sensor on Arduino Nano v3. The sensor connection and reading the values works allright. I tried to implement a smoothing approach posted here https://www.arduino.cc/en/Tutorial/BuiltInExamples/Smoothing but I wanted to be “classy” and code a more general object class that I could later use for other projects.

When I open the Serial plotter I can see a working noisy signal from the ultrasound but the smoothed/averaged value is mathematicly incorrect on the first glance. I’m not a C programmer so I sniff the problem there :slight_smile:

class Ultrasound {
  byte trigPin; // ultrasound trigger pin
  byte echoPin; // ultrasound echo pin
  long interval; // intervall between readings in millis
  short unsigned avgNum; // number of readings in running average
  long unsigned prevMillis; // time of previous reading
  float value; 
  float average;
  int *readings; // the readings set for the running average
  float sum;
  
  public:
  Ultrasound(int tPin, int ePin, long interv, int aNum = 10) {
    trigPin = tPin;
    echoPin = ePin;
    interval = interv;
    avgNum = aNum;
    initReadings(aNum);
  }
  float getValue() {
    return(value);
  }
  float getAverage() {
    return(average);
  }  
  // I'd like to be able to change the reading interval during runtime
  void setReadingInterval(long interv) {
    interval = interv;
  }
  // I'd like to be able to change the averaging number during runtime
  void setAverageRange(int num) {
    avgNum = num;
    initReadings(num);
  }
  void setup() {
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
  }
  void loop() {
    short unsigned readIndex = 0;
    long unsigned curMillis = millis();
    
    if (curMillis - prevMillis >= interval) {
      value = readValue();
      prevMillis = curMillis;

      // subtract the last reading:
      sum = sum - readings[readIndex];
      // read from the sensor:
      readings[readIndex] = value;
      // add the reading to the total:
      sum = sum + value;
      // advance to the next position in the array:
      readIndex = readIndex + 1;
    
      // if we're at the end of the array...
      if (readIndex >= avgNum) {
        // ... jump back to the beginning:
        readIndex = 0;
      }
      // calculate the average:
      average = sum / avgNum;
    }
  }

  private:
  float readValue(byte lowTime = 2, byte highTime = 5) {
    long response;
    digitalWrite(trigPin, LOW);
    delayMicroseconds(lowTime);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(highTime);
    digitalWrite(trigPin, LOW);
    // get the ultrasound response
    response = pulseIn(echoPin, HIGH);
    // calculate the distance in cm
    return(response/58.31);
  }
  // needed to initialize the readings list which has unknown length at compile time
  void initReadings(int num) {
    // initialize all the readings to 0:
    readings[num] = {0};
  }
};

// trigger and echo pins for the ultrasound sensor
const byte pTrig = 4;
const byte pEcho = 5;

// construct the object instance
Ultrasound us(pTrig, pEcho, 10, 1000);

void setup() {
  us.setup();
  // start serial communication
  Serial.begin(9600);
}

void loop()
{
  us.loop();

  Serial.print(us.getValue());
  Serial.print(" ");
  Serial.print(us.getAverage());
  Serial.println(" ");
}

I will be very glad for any suggestions.
Cheers!

void initReadings(int num) {
    // initialize all the readings to 0:
    readings[num] = {0};
  }
};

Oops.

TheMemberFormerlyKnownAsAWOL:

void initReadings(int num) {

// initialize all the readings to 0:
   readings[num] = {0};
 }
};



Oops.

Thank you for your reply, but i don't quite understand what you mean by that ... ?

Ask yourself “how big is the array “readings”?”, and follow back from there.

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