# How to take a number of readings within a certain range

Hi,

I have been googling this but I cant seem to find out how.

How in Arudino do you take the last few outputs and then if within a range (e.g. ones within 10 of the last, or last three outputs within 10 of each other) check that they are within 'X' (any number) range of each other?

I know you can debounce switches but this checks if equal above or below something - can you do this for analogue readers all at once (equal above or below 'X')? (sorry using the logical thinking)

maintain the last 10 values in a circular array and check against your min and max?

Confused about what you're trying to do, but maybe sort of possibly sounds like the absolute value function might help

Hey J-M-L Thank-you for your reply, how would you do this?

INTP, Thank-you for your comment, absolute value function would tell me the distance from 0 not the value between the numbers.

To give more explanation, I am using a ultrasonic proximity measure and it gives values but due to using with animals often it will randomly give a null reading (nothing detected) causing outliers within my data, I am trying to migrate this similarly to you do with a switch by taking the last few values and checking that the distance didnt move over 'X' distance due to the speed of the loop vs. the speed of the animal I can migrate this.

However whilst I know what I want to do, I am unsure how to code this in.

First of all, using an ultrasonic sensor on dogs is cruel. They can hear it.

There is no second of all, I don't know if I want to help you annoy these dogs :(

Hi INTP,

A dogs hearing range (min and max) not being breed dependant is 67 Hz to 45 kHz the sensor I am using is below 40 Hz, I have tested this before I did this project via datasheet and with my own dog. Thus this is outside their hearing range.

Do not worry I would never do anything to harm my dog :)

Your sensor is not 40Hz. Ultrasonic is 40kHz.

You would hear 40Hz as a subwoofer bass type note, human hearing range is approx 20Hz-20kHz.

Assuming no one is getting hurt…

Would something like this help

``````// https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
// possible Timer 2 Conflict (if you use tone)

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

#define NBSAMPLES 5
unsigned long  lastMeasures[NBSAMPLES];
uint8_t currentSample;
uint8_t lastMinIndex, lastMaxIndex;
unsigned long  minSample, maxSample;

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
currentSample = 0;
minSample = 0xFFFFFFFF;
maxSample = 0UL;
lastMinIndex = 0;
lastMaxIndex = 0;
}

void loop() {

// this is what you should do but I don't have a ping sensor with me
// so I'll fake it by generating a random value

// lastMeasures[currentSample]=sonar.ping_cm();
lastMeasures[currentSample] = random(0, 2000); // faking the ping

if (lastMeasures[currentSample] >= lastMeasures[lastMaxIndex]) {
maxSample = lastMeasures[currentSample];
lastMaxIndex = currentSample;
}
if (lastMeasures[currentSample] <= lastMeasures[lastMinIndex]) {
minSample = lastMeasures[currentSample];
lastMinIndex = currentSample;
}
currentSample = (currentSample + 1) % NBSAMPLES;

for (int i = 0; i < NBSAMPLES; i++) {
Serial.print(lastMeasures[i]);
Serial.print("\t");
}
Serial.print("Min index:");
Serial.print(lastMinIndex);
Serial.print(" (");
Serial.print(minSample);
Serial.print(")");

Serial.print("\t");
Serial.print("Max index:");
Serial.print(lastMaxIndex);
Serial.print(" (");
Serial.print(maxSample);
Serial.print(")");

Serial.println();
delay(1000);
}
``````

the sensor I am using is below 40 Hz

Nonsense. Report to moderator ignored.

@INTP: Why not use your electronic genius to construct a divide by 4 frequency divider, use a US mic and amp, clamp the headphones on and find out what your environs sound like to your dog. Bet all those squeaking hinges on cabinet doors and roaring wind from A/C vents and such are far more annoying than the clicks from a range finder. Maybe your voice sounds like fingernails on a blackboard to a dog. For PETA's sake!

HI, Is this related to your dog and human detector system? But now using Ultrasonic distance measurement.

http://forum.arduino.cc/index.php?topic=411689.msg2833588#msg2833588

Tom... :)

outsider: @INTP: Why not use your electronic genius to construct a divide by 4 frequency divider, use a US mic and amp, clamp the headphones on and find out what your environs sound like to your dog. Bet all those squeaking hinges on cabinet doors and roaring wind from A/C vents and such are far more annoying than the clicks from a range finder. Maybe your voice sounds like fingernails on a blackboard to a dog. For PETA's sake!

K