servo sweep with PING sensors

I am working on a project in which I want to control two servo motors that I want to use to rotate a PING sensor and A camera on each servo. I want the servos to stop when the ping sensors read an object withing a given distance. The overall objective is to create different “Zones” of distance with different responses for each zone. I am having trouble controlling the servos and can use any help.

here is what I have been working with:

#include <NewPing.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "MediaPlayer.h"
#include <ServoTimer2.h>

int ledPin = 13;
int servo1pos = 1000;
int servo2pos = 0;
#define PING_PIN  11  
#define MAX_DISTANCE 100

ServoTimer2 servo1;
ServoTimer2 servo2;
MediaPlayer mediaPlayer;

void setup() {
  pinMode (ledPin, OUTPUT);

void loop() {
  int ping1dist;
  ping1dist = poll0();
  Serial.print("distance in centimeters = ");
if(ping1dist > 40)
{ int i = i;
 i =+ 50;}
  ping1dist = poll0();
    Serial.print(" Distance is greater than 100");

//Ping1 Subroutine
int poll0() {
  delay(30);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS =; // Send ping, get ping time in microseconds (uS).

int dist = uS / US_ROUNDTRIP_CM;
return dist;

Basic problem solving: divide and conquer. There is too much happening to just visualize in my mind and I do not wish to build out your hardware. But, generally, a project should be constructed in layers; do the complex stuff like mechanics first and then layer in other peripherals. The reason I suggest this is that mechanical stuff and be detrimentally influenced by other code.

So, start with two servos and get that right. The goal should be to have code that swings the servos through the limits - build in a random number routine called periodically to stop the servos based upon the return of the random operator. Once you have both servos rotating through their range and randomly stopping and restarting, then it is time to add in the sonic ping and incorporate the rules.

Just my opinion: learning how to factor a problem into a layered construction approach is somewhat a trial 'n error project in itself. Start by crating a diagram of code, hardware, and rules. The goal is to have mini-projects that can be combined to form the final project. In doing this, your interaction with the forum community will be focused and your success will be much better.

Take a look at this:


 if(ping1dist > 40)
    int i = i;

What's that?