Motion-following device not working - sketch related

Hi folks,
I'm wondering if someone doesn't have anything better to do, would you have a peek at my sketch for me? It passes compiling but doesn't work. Quite simply (or not) I took Devid Mellis's Ping sketch and Calvin Kielas-Jensen's "motion following robot" (written for 4-pin ping sensors) and tried to stitch them together. Obviously I did something wrong. And just for a little context; a month ago I'd never heard of Arduino and had never written any code in my life (although this was mostly borrowing).
May I upload the sketch into a reply right here?
Many thanks,
-Mac

Macadoo:
May I upload the sketch into a reply right here?

Yes, you may. :slight_smile:

Hit the "Reply" button, not "Quick Reply", then paste the code into the reply.
After doing that, select the code and then press the </> button to enclose the code within code tags.

Alternatively, paste the code, then type [code] immediately before it, then [/code] immediately after it.

Before posting the code, press Ctrl-T or click on ">Tools >Auto format" in the IDE to format it correctly.

This is the device I'm trying to replicate:

I built this with the four-pin sensors and uploaded Calvin's code and it worked very well. But the cheap sensors don't respond to humans, just a flat surface. Maxbotix has their EZ0s that supposedly detect the human form, much more sensitive with a better beam pattern.

This is Calvin's Instructable:

/* 3-pin ultrasonic X2 sensors with servo. A motion-following device for a kinetic sculpture

   This sketch is based on sketches by David Mellis and Calvin Kielas-Jensen but adapted for two three-pin sensors

   The circuit:
	 +V connection of the PING))) attached to +5V
	 GND connection of the PING))) attached to ground
	 SIG connections of the PINGS))) attached to digital pin 7 and 8, servo on pin 9



   This example code will be in the public domain if I can ever get in working

*/

#include <NewPing.h>

#include <Servo.h>

Servo myservo;

// this constant won't change.  It's the pin number
// of the sensor's output:
const int lPingPin = 8, rPingPin = 7;

// establish variables for duration
// and the distance result in inches
long Rduration, Lduration, Rinches, Linches;

int threshold = 15; //Sensor threshold in inches

int angle = 80; //Initial angle

void setup() {
  // initialize serial communication:
  {
    Serial.begin(9600);
  }
  myservo.attach(9);
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(lPingPin, OUTPUT);
  digitalWrite(lPingPin, LOW);
  delayMicroseconds(2);           //**Here I doubled David's single sensor code and replaced "pingPin with lPingPin and rPingPin
  digitalWrite(lPingPin, HIGH);   // for the left and right sensors
  delayMicroseconds(5);
  digitalWrite(lPingPin, LOW);
  pinMode(lPingPin, INPUT);

  pinMode(rPingPin, OUTPUT);
  digitalWrite(rPingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(rPingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(rPingPin, LOW);
  pinMode(rPingPin, INPUT);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(lPingPin, INPUT);  //IS THIS CORRECT???
  pinMode(rPingPin, INPUT);  //IS THIS CORRECT???
  duration = pulseIn(lPingPin, HIGH);  //IS THIS CORRECT???
  duration = pulseIn(rPingPin, HIGH);  //IS THIS CORRECT???

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  {
    Serial.print("Left: ");
    Serial.print(Linches);
    Serial.println(" in");
    Serial.print("Right: ");
    Serial.print(Rinches);
    Serial.println(" in");
  }
  follow();
}

long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

void follow()
{
  if (Linches <= threshold || Rinches <= threshold)
  {
    if (Linches + 2 < Rinches)
    {
      angle = angle - 2;
    }
    if (Rinches + 2 < Linches)
    {
      angle = angle + 2;
    }
  }
  if (angle > 160)
  {
    angle = 160;
  }
  if (angle < 0)
  {
    angle = 0;
  }
  myservo.write(angle);
}

The first thing I'd change is to move the 'pulseIn()' statements to immediately after the pulse is sent, as was done in the original sketch. As you have it, both 'pings' are sent, then both measurements are taken together, so each sensor will receive an echo from the other one.

You have one extra 'pinMode()' statement for each sensor to make the pins inputs. That's not necessary, because you already make them inputs immediately after the 'ping'.

Also, you don't need to include the "NewPing" library, since you never use it. (Although it is actually a better way to do ultrasonic ranging.)

Furthermore, I just noticed, you use the same variable to store the results of both pings, so the second result immediately overwrites the first.
You create new local variables called 'duration', 'inches' and 'cm' and use them for both readings, whereas you should use the original independent variables 'Rduration', 'Lduration', 'Rinches', 'Linches'.

And you don't need the 'cm' variable or this at all, since you never use the result:-cm = microsecondsToCentimeters(duration);

In other words, it's a mess, lol. Thanks very much OldSteve, I'll take another stab at it in the morning. Obviously I don't have a deep enough understanding of the inner workings quite yet, but I'll get there.
I'll post back after I make the changes.

Macadoo:
In other words, it's a mess, lol. Thanks very much OldSteve, I'll take another stab at it in the morning. Obviously I don't have a deep enough understanding of the inner workings quite yet, but I'll get there.
I'll post back after I make the changes.

No, not a mess at all. Considering you're just starting out, you're doing fine. It'll all be second nature in no time. :slight_smile:

You da man! All your suggestions were right on the money. Once I made the changes I had to tinker a bit more, fix some missing declarations, etc. but it's working. Thank you for your help. I don't know why I didn't see those redundant lines. Oh, yes I do, because I had been at it for 12 hours straight, lol. Sometimes one needs to just walk away, get some space. But other times one doesn't have time to walk away. That's where friends come in handy. And you, sir, are a steely-eyed Arduino man :smiley:

Now, to find a strong, solid, Arduino-compatible servo motor that can handle my needs.

Macadoo:
You da man! All your suggestions were right on the money. Once I made the changes I had to tinker a bit more, fix some missing declarations, etc. but it's working. Thank you for your help. I don't know why I didn't see those redundant lines. Oh, yes I do, because I had been at it for 12 hours straight, lol. Sometimes one needs to just walk away, get some space. But other times one doesn't have time to walk away. That's where friends come in handy. And you, sir, are a steely-eyed Arduino man :smiley:

Now, to find a strong, solid, Arduino-compatible servo motor that can handle my needs.

Excellent. Glad you got it working. :slight_smile: