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);
}