how to use the first read of a sensor to compare with the other reads

hi i'm using arduino uno with distance sensor "LV-MaxSonar-EZ", trying to build a sentry gun from tf2.

I want to put the sensor on the legs of the gun and every time when someone pass and changes the first read of the sensor it starts to shoot at him.
But i want it to have some "safe distance" that not every interruption of the sensor will activate the loop.

right now all I have is that code which reading the sensor values and shows them in cm form in serial monitor

//Analog pin 1 for reading in the analog voltage from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
const int anPin = 1;

//variables needed to store values
long anVolt, inches, cm;
int sum=0;//Create sum variable so it can be averaged
int avgrange=60;//Quantity of values to average (sample size)

void setup() {
  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);
}
void loop() {
  pinMode(anPin, INPUT);
  //MaxSonar Analog reads are known to be very sensitive. See the Arduino forum for more information.
  //A simple fix is to average out a sample of n readings to get a more consistant reading.\\ 
  //Even with averaging I still find it to be less accurate than the pw method.\\ 
  //This loop gets 60 reads and averages them

  for(int i = 0; i < avgrange ; i++)
  {
    //Used to read in the analog voltage output that is being sent by the MaxSonar device.
    //Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
    //Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
    anVolt = analogRead(anPin)/2;
    sum += anVolt;
    delay(10);
  }  
  inches = sum/avgrange;
  cm = inches * 2.54;
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  //reset sample total
  sum = 0;
  delay(500);
}

So can you help mr do this?

I seriously hope you don't construct a real gun with this setup.

Your description sounds to me that you just have to compare the returned value to an upper and a lower limit. If that's not correct you might have to describe in more detail what you want to achieve.

pylon:
I seriously hope you don't construct a real gun with this setup.

Your description sounds to me that you just have to compare the returned value to an upper and a lower limit. If that's not correct you might have to describe in more detail what you want to achieve.

Well, it won't really shoot, it only blink a led hehe.....

I want to put it in the center of a room aiming an open door and every time when someone pass it starts "shooting" at him.
The sensor will recive something like between 198~202 cm and when he will recive something like 190 or lower it starts the loop.

And where exactly do you have your problem? Checking if the value is below 190cm shouldn't be the killer, should it?

pylon:
And where exactly do you have your problem? Checking if the value is below 190cm shouldn't be the killer, should it?

the problem is how can i store the first value of the sensor.
I can check the value in my room for ex. and write it manually.
But if i want to put it in other room or change the position i need to rewrite the new value of the distance.
so i want a program that stores the first value or even calculates the average of the 10 first values and save it so if it changes it starts a loop.
something like that:

setup:
-check sensor value
-save sensor value(value1)

void loop:

-if (sensor value< value1) than.....

I am no expert on distance sensors, and I haven't personally used one yet, but it would seem to me that in order to keep your sentry gun portable and transferable, all you would have to do is make a command that says: if motion is detected at a greater distance than say, 10 feet.. loop program to flash LED, swivel with servo/motor, etc.

I do not believe that an arduino can store a recorded value in it's memory on it's own without some sort of data logging shield, because the board only has so much memory in order to hold a program code.

Gustermaximus:
I am no expert on distance sensors, and I haven't personally used one yet, but it would seem to me that in order to keep your sentry gun portable and transferable, all you would have to do is make a command that says: if motion is detected at a greater distance than say, 10 feet.. loop program to flash LED, swivel with servo/motor, etc.

I do not believe that an arduino can store a recorded value in it's memory on it's own without some sort of data logging shield, because the board only has so much memory in order to hold a program code.

it doesn't need such memory...
i want to save one number like 250.

that every time that i turn on (or resets) the arduino will recive a value for ex' 200 cm
and if it recive value that smaller than the first value it starts a loop.
you know to keep it portable that i could use it anywhere

setup:
-check sensor value
-save sensor value(value1)

void loop:

-if (sensor value< value1) than.....

Why don't you just implement that? You just have to transform that into C code. You haven't specified where your problem is.

the problem is how can i store the first value of the sensor.

What do you mean by "store"? Assign it to a variable, if your description of the intended functionality applies, you don't have to store that over a power interruption.

pylon:

setup:
-check sensor value
-save sensor value(value1)

void loop:

-if (sensor value< value1) than.....

Why don't you just implement that? You just have to transform that into C code. You haven't specified where your problem is.

the problem is how can i store the first value of the sensor.

What do you mean by "store"? Assign it to a variable, if your description of the intended functionality applies, you don't have to store that over a power interruption.

well... that's the problem. i don't know how. i'm new to programming.
and i want to assign only the first read of the sensor to a variable and not the read of the sensor, which is changing.

well... that's the problem. i don't know how. i'm new to programming.

Make a try and we correct you if you're wrong. That's the way you learn something.

and i want to assign only the first read of the sensor to a variable and not the read of the sensor, which is changing

Assign the value to the variable in the setup() routine as this is run only once at startup.