HELP Sonar object detection project, using NEW Ping library

I have a personal project for a 3 zone range detection circuit where each zone has 3 levls of detection to drive a 3 color RGB LED for each zone. so 3 sonar sensors (left, rear, right) and 3 RGB LED's

I was able to find a sonar range finder project online that worked with a single ping sensor but I have no idea how to write code for what I need, I'm a mechanical designer by trade. I am having trouble wrapping my mind around it all.

Seeing this new ping code now is inspiring but I just do not understand how to mold it to my application.

I humbly ask for a sketch that would work for the above. Thank you in advance.
I am fully willing to trade back help with 3D mechanical design / photo rendering or other mechanical design related function. look in my profile to get my website info, in the gallery you can see what my capability is.

David

NEW-PING sample code is below

// ---------------------------------------------------------------------------
// This example code was used to successfully communicate with 15 ultrasonic sensors. You can adjust
// the number of sensors in your project by changing SONAR_NUM and the number of NewPing objects in the
// "sonar" array. You also need to change the pins for each sensor for the NewPing objects. Each sensor
// is pinged at 33ms intervals. So, one cycle of all sensors takes 495ms (33 * 15 = 495ms). The results
// are sent to the "oneSensorCycle" function which currently just displays the distance data. Your project
// would normally process the sensor results in this function (for example, decide if a robot needs to
// turn and call the turn function). Keep in mind this example is event-driven. Your complete sketch needs
// to be written so there's no "delay" commands and the loop() cycles at faster than a 33ms rate. If other
// processes take longer than 33ms, you'll need to increase PING_INTERVAL so it doesn't get behind.
// ---------------------------------------------------------------------------
#include <NewPing.h>

#define SONAR_NUM     15 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
  NewPing(41, 42, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(43, 44, MAX_DISTANCE),
  NewPing(45, 20, MAX_DISTANCE),
  NewPing(21, 22, MAX_DISTANCE),
  NewPing(23, 24, MAX_DISTANCE),
  NewPing(25, 26, MAX_DISTANCE),
  NewPing(27, 28, MAX_DISTANCE),
  NewPing(29, 30, MAX_DISTANCE),
  NewPing(31, 32, MAX_DISTANCE),
  NewPing(34, 33, MAX_DISTANCE),
  NewPing(35, 36, MAX_DISTANCE),
  NewPing(37, 38, MAX_DISTANCE),
  NewPing(39, 40, MAX_DISTANCE),
  NewPing(50, 51, MAX_DISTANCE),
  NewPing(52, 53, MAX_DISTANCE)
};

void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    }
  }
  // Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  // The following code would be replaced with your code that does something with the ping results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");
  }
  Serial.println();
}

You haven't actually said what it is you want.
Some kind of reversing sensor, with a traffic-light display?

superdave42:
Seeing this new ping code now is inspiring but I just do not understand how to mold it to my application.

Start by writing a prototype sketch that just uses one sensor and compares the returned distance against thresholds to decide what colour to display.

Then write another prototype sketch that just displays the alternative colors on one LED.

Then, once the parts are working, go back to this example PING sketch. Make the number of ping sensors three. Take all three measurements as in the example. For each sensor, apply the threshold check and decide what colour the corresponding LED should be. The ping sensors are already configured via an array and processed in a loop. You would use a similar approach to configure the LED pin numbers and make each LED display the colour determined from the corresponding ping sensor.

It's a relatively simple coding problem, but if you literally want somebody to write the code for you (rather than advise you how to do it for yourself) then the Gigs and Collaborations section of the forum would be the place to ask. Most people would expect to be paid for working for you, but your offer of payment in kind seems reasonable and perhaps you'll find somebody interested in it. (As a rough guide, it would take a competent Arduino programmer about half an hour to get the code ready for testing, and probably the same again to test and configure it on your actual hardware.)

AWOL:
You haven't actually said what it is you want.
Some kind of reversing sensor, with a traffic-light display?

it's a proximity detector for controlling a remote platform via video goggles.

PeterH:

superdave42:
Seeing this new ping code now is inspiring but I just do not understand how to mold it to my application.

Start by writing a prototype sketch that just uses one sensor and compares the returned distance against thresholds to decide what colour to display.

Then write another prototype sketch that just displays the alternative colors on one LED.

Then, once the parts are working, go back to this example PING sketch. Make the number of ping sensors three. Take all three measurements as in the example. For each sensor, apply the threshold check and decide what colour the corresponding LED should be. The ping sensors are already configured via an array and processed in a loop. You would use a similar approach to configure the LED pin numbers and make each LED display the colour determined from the corresponding ping sensor.

It's a relatively simple coding problem, but if you literally want somebody to write the code for you (rather than advise you how to do it for yourself) then the Gigs and Collaborations section of the forum would be the place to ask. Most people would expect to be paid for working for you, but your offer of payment in kind seems reasonable and perhaps you'll find somebody interested in it. (As a rough guide, it would take a competent Arduino programmer about half an hour to get the code ready for testing, and probably the same again to test and configure it on your actual hardware.)

Thank you for your advice, I have got a sketch working with 1 sonar and is driving the 3 led colors based on 3 distance zones. I am able to assign the pins, edit distance etc. for some reason I am just having trouble figuring out the syntax for getting 2 more sensors in the loop and processed. I'll study the example sketch some more and see how I can borrow from it. I feel programming wise that I am close, just seem to be missing a couple key things in order to really understand what i'm doing.

Dave

I have got a sketch working with 1 sonar and is driving the 3 led colors based on 3 distance zones.

So post it, and we can point you in the right direction.

AWOL:

I have got a sketch working with 1 sonar and is driving the 3 led colors based on 3 distance zones.

So post it, and we can point you in the right direction.

Right on, I'll post it when I get home.

Thank you

the current code I'm using with the old PING library

/* Ping))) Sensor
 
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
 
   The circuit:
    * +V connection of the PING))) attached to +5V
    * GND connection of the PING))) attached to ground
    * SIG connection of the PING))) attached to digital pin 7
 
http://www.arduino.cc/en/Tutorial/Ping
 
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Jun 2009
   by Tom Igoe
 
   This example code is in the public domain.
   
   MODIFIED BY DAVID COOK
 
 */
 
// this constant won't change.  It's the pin number
// of the sensor's output:

const int pingPin = 7;
 
void setup() {
  // initialize serial communication:
  pinMode(13, OUTPUT); // blue
pinMode(12, OUTPUT); // green
pinMode(11, OUTPUT); // red
  Serial.begin(9600);
}
 
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(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
 
  // 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(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
 
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  // show LED colors
  if(inches > 0 && inches <= 5) {
    // show red, all other leds are off
    digitalWrite(13, LOW);
    digitalWrite(12, LOW);
    digitalWrite(11, HIGH);
  } else if(inches <= 15 && inches > 5) {
    // show blue, all other leds are off
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(13, HIGH);
  } else {
    // show green led, all other leds are off
    digitalWrite(13, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, HIGH);
  }
  //cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
}
 
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;
}