Detecting sudden or smooth movement with ultrasound sensor

I’ve been trying to write a sketch that will output what kind of change the distance reads of a seeduino ultrasound sensor undergo. The sensor is going next to a door and I want it to detect door closing (decrease), door opening(increase) or bump (someone passes). So far I got this, it works pretty well untill I try determining the kind of change the values have:

const int pingPin = 12;

unsigned long pingpreviousMillis;
unsigned long pingInterval = 500;

const int openeyesInterval = 250; //different intervals I can set to
const int activeInterval = 500;
const int standbyInterval = 750;

int newRead;
int previousRead;
const int tolerance = 2;
const byte storageNum = 5; // stability when 5 equal reads
const byte typeStorageNum = 24; //when looking for type 3 seconds readinc every 250ms
int latestVals[storageNum];
int typeVals[typeStorageNum];
byte typeCounter;
byte valCounter;
boolean openeyesState = LOW;
boolean counting = LOW;
byte soFar = 0;

enum changeTypes {none, doorsClosing, doorsOpening, bump, notyet};
enum modelName {mopen, mclose};
byte changeType;
void setup() {
  Serial.begin(9600);
}

void loop() {
  VarPingInterval();
  int hello = ChangeType();
  if (hello != notyet) {
    Serial.println(hello);
  }
}     

byte ChangeType() { //should compose an array with latest values and when counter is at max reset it and return type (enum changeTypes)
  if (newRead != -1) {
    typeVals[typeCounter] = previousRead;
    if (typeCounter > typeStorageNum) {
      int difference;
      int differenceMem;
      for (byte i = 1; i < typeStorageNum; i++) {
        if (typeVals[i - 1] > typeVals[i]) {
          differenceMem = difference;
          difference = typeVals[i - 1] - typeVals[i];
          if ((Tolerable(difference, differenceMem) == HIGH) && (soFar != doorsOpening)) {
            soFar = doorsClosing;
          } else {
            soFar = 0; // if any change it's a bump
            typeCounter = 0;
            return bump;
          }
        }
        if (typeVals[i - 1] < typeVals[i]) {
          differenceMem = difference;
          difference = typeVals[i] - typeVals[i - 1];
          if (Tolerable(difference, differenceMem) == HIGH && soFar != doorsClosing) {
            soFar = doorsOpening;
          } else {
            soFar = 0;
            typeCounter = 0;
            return bump;
          }
        }
      }
      if (soFar == doorsClosing) { // if stable then a close
        soFar = 0;
        typeCounter = 0;
        return doorsClosing;
      }
      if (soFar == doorsOpening) {
        soFar = 0;
        typeCounter = 0;
        return doorsOpening;
      }
      typeCounter = 0;
      return bump;
    }
    typeCounter++;
    return notyet;
  }
  return notyet;
}

void VarPingInterval() { // if sensor read is stable lower read interval to save battery if variation increment interval until stable again
  if ((DistanceChange() == HIGH) || (counting != LOW)) {
    if (newRead != -1) {
      latestVals[valCounter] = previousRead;
      if (valCounter > storageNum) {
        int result;
        for (byte i = 0; i < storageNum; i++) {
          result += latestVals[i];
        }
        result /= storageNum;
        if (result == previousRead) {
          Serial.println(previousRead);
          pingInterval = activeInterval;
          openeyesState = LOW;
          counting = LOW;
          changeType = none;
          valCounter = 0;
          return;
        }
        openeyesState = HIGH;
        valCounter = 0;
        return;
      }
      valCounter++;
    }
  }
}
  
boolean Tolerable(int newValue, int compareValue) { // are the two values within tolerance's distance (2 cm +-)
  if ((newValue < (compareValue + tolerance) && newValue > (compareValue - tolerance))) {
    return HIGH;
  }
  return LOW;
}

boolean DistanceChange() { // returns HIGH if distance has changed or VarPingInterval is reading for stability
  newRead = ReadDistance();
  if ((newRead != -1) && Tolerable(newRead, previousRead) == LOW) {
    previousRead = newRead;
    pingInterval = openeyesInterval;
    openeyesState = HIGH;
    counting = HIGH;
    return HIGH;
  } else {
    if (counting != LOW) {
      return HIGH;
    }
    return LOW;
  }
}

int ReadDistance() { // read Ultrasound distance
  unsigned long currentMillis = millis();
  if ((currentMillis - pingpreviousMillis) > pingInterval) {
    pingpreviousMillis = currentMillis;
    long cm;
  
    pinMode(pingPin, OUTPUT);
    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin, LOW);
  
    pinMode(pingPin, INPUT);
    cm = pulseIn(pingPin, HIGH) / 29 / 2;
    
    return cm;
  } else {
    return -1;
  }
}

Thanks, can’t find my way through this one

  if ((newRead != -1) && Tolerable(newRead, previousRead) == LOW) {

Tolerable() returns true or false. Why are you comparing that to LOW?

Some comments in that code (NOT at the end of a line) would go a long ways towards explaining what you expect the code to do.

You have an absolutely huge amount of code to determine if the difference between the current reading and the previous reading is increasing, decreasing, or what's left? What is a bump?

Tolerable() returns true or false. Why are you comparing that to LOW?

In my experience 0 = LOW = false, though false would be more appropriate.

What is a bump?

bump (someone passes).

Well, when DistanceChange() detects movement the interval between sensor readings decreases to ensure proper detection (and when readings are stable it will go back to normal (VarPingInterval()) ChangeType() should list latest readings in an array and when the array is full it will scan it for a consistent increase or decrease of value or sudden increase/decrease(bump(someone passes through doorway))).

You have an absolutely huge amount of code to determine if the difference between the current reading and the previous reading is increasing, decreasing, or what's left?

How would you do it??

RonaldPoFo: Well, when DistanceChange() detects movement the interval between sensor readings decreases to ensure proper detection (and when readings are stable it will go back to normal (VarPingInterval())

What's the reason for varying the ping interval? Is there some disadvantage or cost to doing pings at the shorter interval?

What's the reason for varying the ping interval? Is there some disadvantage or cost to doing pings at the shorter interval?

Battery.