Cycling through three ultrasonic sensors?

Hey guys :),
I'm trying to learn the ways of programming and electronics and I've come by a particularly difficult situation (for me anyway :P).
http://www.ebay.com.au/itm/161181376374?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649
I have hooked 3 of the above sensors to my arduino and lined them side by side. Now that poses a particularly problem. They each need to fire at different intervals so that they don't interfere with each other.
I figured out a way to easily (but not effeciently xD) do this with the delay command. But using delay is problematic as I'm using other sensors and it'd interfere with them.

The library I am using for the sensors is:
http://playground.arduino.cc/Code/NewPing#.Uyv9KIVYPuU

and my current code is:

#include <NewPing.h>

#define TRIGGER_PIN1  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN1     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN2  14  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN2     13  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN3  16  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN3     15  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). 
int convert;
unsigned int pingRx;
NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar3(TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

double currentTime = millis();
double sonarTime = 0.0;

void Sonic() {
  for (i=0,i<3,i++) {
    if (currentTime>(sonarTime+300)) {
      lcd.clear();
      pingRx = sonar1.ping();  // Send ping, get ping time in microseconds (uS).
      convert = pingRx / US_ROUNDTRIP_CM; // Convert ping time to distance in cm and print result (0 = outside set distance range)/ 
    
      //Serial.print("Distance: ");
      //Serial.print(convert); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      //Serial.println("cm");
    
      lcd.print("Distance: ");
      lcd.setCursor(0, 1);
      lcd.print(convert);
      lcd.print(" cm");
    
      sonarTime = currentTime;
      }
    }
  }
}

void setup() {
}
void loop() {
sonic()
(other stuff)
delay(100)
}

You might have noticed two problems with the above code:

  1. I have used a for loop but have not cycled the sensor for each increment of i. (i.e: every time i increments, sonar1 is pinged, you'll notice this in the line pingRx = sonar1.ping(). It should change to sonar2.ping() at i=1 and sonar3.ping() at i=2. Maybe this is possible to someone more advanced at coding than me?
  2. Even if problem 1 is fixed. The sensors won't fire sequentially thanks to the if statement, for example sonar1 would go off resetting the timer back to 0 (in a sense), i=1 kicks in and sonar2.ping() should occur next but 300ms haven't passed yet, so it skips and goes straight to i=2 instead of looping i=1 until it is successful. Again maybe the solution to this is more obvious to someone more adept than me =/.
  3. Or maybe I'm going about this the wrong way entirely and there's a much easier way altogether :P.

My attempts at finding a solution involved:

  1. Trying to compare the variables:
    if (SONAR == sonar1) {
    SONAR = sonar2;
    }
    else if (SONAR == sonar2) {
    SONAR = sonar3;
    }
    else if (SONAR == sonar3) {
    SONAR = sonar1;
    obviously didn't work since thats not a valid comparison xD...

  2. Try to put sonar1, sonar2, sonar3 into a matrix SONAR[sonar1, sonar2, sonar3] which is ALSO not valid since sonars are not primitive data types but are functions(NewPing) =/.

PLEASE help me guys this is doing my head in >.o.
Thanks :).

There was far too many things wrong with your code and Im not going to spend hours going back and forth telling you what to add and change, so I did it myself. But just this one time to get you on the correct path.

#include <NewPing.h>

#define TRIGGER_PIN1  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN1     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN2  14  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN2     13  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN3  16  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN3     15  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). 

NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar3(TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

NewPing Sensors[3] = {sonar1, sonar2, sonar3}; // create an object array for your 3 sensors, DO NOT CHANGE

unsigned long currentTime;
unsigned long sonarTime = 0;
int convert;
unsigned int pingRx;
byte idx = 0;

void Sonic() 
{
  currentTime = millis();
  if ((currentTime - sonarTime) > 300) // This is the better way to write it.
  {
    //lcd.clear();
    if( idx > 2) // This keeps idx within a range of 0 - 2 for the sensor array.
      idx = 0; // if idx is greater than 2, then reset it back to 0
    else
      idx++; // otherwise increment it by 1.

    sonarTime = currentTime;
  } 

  // I chose to write the code this way so your sensors can get multiple pings within 300 milliseconds. 
  // The more readings it is allowed to get, the better the results.

  // If you look below, this is how you would cycle through the multiple sensors, "Sensors[ idx ]".
  pingRx = Sensors[ idx ].ping();  // Send ping, get ping time in microseconds (uS).
  convert = pingRx / US_ROUNDTRIP_CM; // Convert ping time to distance in cm and print result (0 = outside set distance range)/ 

  //Serial.print("Distance: ");
  //Serial.print(convert); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  //Serial.println("cm");

  // I commented these out for me, just because I didn't want to add in the library to satisfy the compiler.
  //lcd.print("Distance: ");
  //lcd.setCursor(0, 1);
  //lcd.print(convert);
  //lcd.print(" cm");
}

void setup() {}

void loop() 
{
  Sonic(); // C and C++ are case sensitive languages and they also require semicolons ; at the end.
  //(other stuff)
  delay(100);  // Change this later when you add more things to the code, look at the Blink Without Delay example sketch!
}

lol >.<... thank you very much, i know code looked silly, but seeing what you did there has helped me so much in something else too ^.^

Thanks