Detecting changes in array, where am I going wrong?

Hoping to have someone smarter than me (not hard to find) look over this bit of poorly written code and figure out what I'm doing wrong.

I have a robot that turns a servo with an ultrasonic rangefinder with a laser "death ray" attached to it. What I expect to happen is that the servo will turn 1 degree at a time, will record the distance to the nearest object at each degree, and then will scan at each degree and notice any change in distance... at which point it will fire its laser-death_ray-disintegration_device-laser_pointer at the object that has entered the scan area.

What I'm currently experiencing is that the "death ray" fires at anything beyond my minimum range (300 cm). I know that using this library (newPing.h) I will get a response of "0" for ranges beyond what I set as the max range. Sounds like an easy fix, right? Well, go ahead. What the hell am I doing wrong?

#include <NewPing.h>
#define trigPin 6
#define echoPin 7
#define MAX_DISTANCE 300 // Maximum distance to ping for (in cm)

NewPing sonar(trigPin, echoPin, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

// servo stuff
#include <Servo.h> //include servo library
Servo servoMain; // Define our Servo.  It will start set at 90 degrees
Servo servoQE; // Define our QE Servo

int laser1 = 2;  // set laser trigger to dig pin

// variables that we will need throughout
double currentdist;
double distancedata[181]; // defines our search deflection array
int def;

void setup()
{
  Serial.begin (9600);  //Serial input for rangefinder if we hook it up to computer
  servoMain.attach(14); // servo on pin A0
  servoQE.attach(4); // up and down servo on pin 4
  
  pinMode (laser1, OUTPUT); // prepares pin A1 to power the laser
  
  servoMain.write(90);
  servoQE.write(180);
}

void loop()
{
  double scandif;
  
  servoMain.write(0); // turns servo to start position
  delay (500); // gives servo time to get to 0 position
  
  for (int def=0; def <= 180; def++){  // this for loop will make the robot pan its servo 180 degrees and take a distance measure at each
    servoMain.write(def);  // Turn servo
    delay (100);
    distancedata[def] = ranger();    
    delay (100);
    

  }

  for (int iterations=0; iterations <= 10; iterations++){ // Sets total number of times that bot will conduct scan
   
    for (int def=180; def >= 0; def--){  // scan and check distances from 180 to 0 degrees
     servoMain.write(def);  // Turn servo
     delay (100);
     currentdist = ranger();
     scandif = (distancedata[def] - currentdist);  // checks the current distance against the previous scanned distance for that deflection
  
     if (distancedata[def] != 0){
       if (scandif >= 100){
          target();
       }
     }
  
     if (distancedata[def] <= 100){
          if (currentdist <= 200){
           target();
        }
     }
     delay (100);

    }
 
 
 
   for (int def=0; def <= 180; def++){  // scan and check distances from 0 to 180 degrees
     servoMain.write(def);  // Turn servo
     delay (100);
     currentdist = ranger();
     scandif = (distancedata[def] - currentdist);  // checks the current distance against the previous scanned distance for that deflection
  
     if (distancedata[def] != 0){
        if (scandif >= 100){
           target();
        }
      }
  
     if (distancedata[def] <= 100){
        if (currentdist <= 200){
           target();
        }    
     }
     
     delay (100);
   }
  }  
}

double ranger(){
   int uS = sonar.ping(); // coolguy command from newping library
   return (sonar.convert_cm(uS));  // convert ping time to cm and return
     }

void target() {
   servoQE.write(170);
   delay(500);
   digitalWrite(laser1, HIGH); // fires on new target
   delay(1500);
   digitalWrite(laser1, LOW);
   delay(1500);
   servoQE.write(180);
   delay(500);
}

The NewPing library has a method:

		unsigned int convert_cm(unsigned int echoTime);

You are calling that method:

   int uS = sonar.ping(); // coolguy command from newping library
   return (sonar.convert_cm(uS));  // convert ping time to cm and return

in a function that returns a double.

Why are you promoting an int to a double? That doubles the storage required with no increase in accuracy.

     scandif = (distancedata[def] - currentdist);

So, what value are you finding?

     if (distancedata[def] != 0){

Need I mention that absolute comparisons involving non-integer values is stupid? OK, I won't. If you promise to fix this.

The code that executes as the servo moves from 180 to 0 differs from the code that executes as the servo moves from 0 to 180 how? If the answer is not at all, the code belongs in a function called from both blocks.

I changed the doubles to int's, and everything still works about the same.

On the code that moves the servo 180-0 and then 0-180 the code is precisely the same. I had these in a separate function, but then I couldn't get them to reference the correct number in the array distancedata[def]. Would love to know how I was messing that one up.

Would love to know how I was messing that one up.

Would love to see the code that messes up.

I asked:

So, what value are you finding?

You answered:

Oh, wait, you didn't answer.

I think you need to change:

     if (distancedata[def] != 0){
       if (scandif >= 100){
          target();
       }
     }
  
     if (distancedata[def] <= 100){
          if (currentdist <= 200){
           target();
        }
     }

to

     if (distancedata[def] != 0){
       if (scandif >= 100){
          target();
       }
     }
     else if (distancedata[def] <= 100){
          if (currentdist <= 200){
           target();
        }
     }

Your two ifs would both apply is distancedata[def] was 0.