Sampling Three Ultra Sonic Sensors

Hi I wrote some code up and I haven't tested it yet, but I just wanted to get general reactions to the approach I took, the code is fully commented. Except for the sonar_1() method, which is self explanatory in that it pings the sensor and gets the distance value back. Just let me know if anything stands out as being a potential problem, thanks mostly with the logic of how the function works.

void sample_sonar(){
    //Ping and Sample UltraSonic 1
  if(sig_1_enable && (millis() - sigTime_1 > sigWait_1) || startUp){//When Arduio Starts up it will always sample the ping_1
    
    sigDist_1 = ping_1();                                        //Ping the Sonar #1 and Store Distance to Nearest Object
    
    sigTime_1 = millis();                                        //Time Stamp Last known sonar pulse from sonar 1
    
    startUp = false;                                             //Remove start up flg
    sig_1_enable = false;                                        //Disable sonar 1 
    sig_2_enable = true;                                         //Enable sonar 2
  }
  
  //Ping and Sample UltraSonic 2
  else if(sig_2_enable && (millis() - sigTime_2 > sigWait_2)){
    
    sigDist_2 = ping_2();                                        //Ping the Sonar #2 and Store Distance to Nearest Object
    
    sigTime_2 = millis();                                        //Time Stamp Last known sonar pulse
    
    sig_2_enable = false;                                        //Disable sonar 2
    sig_3_enable = true;                                         //Enable sonar 3
  }
  
  //Ping and Sample UltraSonic 3
  else if(sig_3_enable && (millis() - sigTime_3 > sigWait_3)){
    
    sigDist_3 = ping_3();                                        //Ping the Sonar #3 and Store Distance to Nearest Object
    
    sigTime_3 = millis();                                        //Time Stamp Last known sonar pulse
    
    sig_3_enable = false;                                        //Disable sonar 3 
    sig_1_enable = true;                                         //Enable sonar 1
  }

}

int ping_1(){
    long duration, inches, cm;
  
    pinMode(SIG_1, OUTPUT);
    digitalWrite(SIG_1, LOW);
    delayMicroseconds(2);
    digitalWrite(SIG_1, HIGH);
    delayMicroseconds(5);
    digitalWrite(SIG_1, LOW);
  
    pinMode(SIG_1, INPUT);
    duration = pulseIn(SIG_1, HIGH);
  
    return (inches = microsecondsToInches(duration));
}

Then I would just place the sampleSonar() method in the void loop. I think I'm going to have the sampleSonarMethod() return a one 1D array with two elements. Element 1 = Distance, Element 2 = Sonar Sensor ID

Thanks again

So, does that mean you've got "ping_1", "ping_2" and "ping_3", all looking pretty similar, apart from "SIG_1", "SIG_2" etc?

Time for learning about arguments and parameters.
Arrays too.

Yeah, sorry, I left out some of the code. Just assume there is a ping_2 and ping_3. they are the same thing as ping_1.

they are the same thing as ping_1.

Why?

int ping(int sigPin)
{
    pinMode(sigPin, OUTPUT);
    digitalWrite(sigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(sigPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(sigPin, LOW);
  
    pinMode(sigPin, INPUT);
    return pulseIn(sigPin, HIGH); // return the raw value - conversion to units is a different matter.
}

ping_2 is a different method, but the code does the same thing only for ultra sonic sensor 2 and 3.

int ping_2(){
    long duration, inches, cm;
  
    pinMode(SIG_2, OUTPUT);
    digitalWrite(SIG_2, LOW);
    delayMicroseconds(2);
    digitalWrite(SIG_2, HIGH);
    delayMicroseconds(5);
    digitalWrite(SIG_2, LOW);
  
    pinMode(SIG_2, INPUT);
    duration = pulseIn(SIG_2, HIGH);
  
    return (inches = microsecondsToInches(duration));
}

int ping_3(){
     long duration, inches, cm;
  
    pinMode(SIG_3, OUTPUT);
    digitalWrite(SIG_3, LOW);
    delayMicroseconds(2);
    digitalWrite(SIG_3, HIGH);
    delayMicroseconds(5);
    digitalWrite(SIG_3, LOW);
  
    pinMode(SIG_3, INPUT);
    duration = pulseIn(SIG_3, HIGH);
  
    return (inches = microsecondsToInches(duration));
}

If they don't do anything significantly different (like return the distance in parsecs instead of inches), you don't need them.

The three functions correspond to different sonar sensors. If I just have one ping method then I'll need to pass parameters into the ping function to select which sensor I want to sample.

If I just have one ping method then I'll need to pass parameters into the ping function to select which sensor I want to sample.

Nope, still not seeing the downside to that.
Upside is less code to debug.

(not "parameters", just "parameter")

I have delclared all my pin numbers as constants (global)

//I/O Pin Definitions
const byte L_ENCODER_A = 2;                                    //Pin Number attached to Left Encoder Phase A
const byte L_ENCODER_B = 3;                                    //Pin Number attached to Left Encoder Phase B
const byte L_MOTOR_A = 9;                                      //Pin Number attached to Left Motor Positive Pin
const byte L_MOTOR_B = 10;                                     //Pin Number attached to Left Motor Negative Pin
const byte R_MOTOR_A = 11;                                     //Pin Number attached to Right Motor Positive Pin
const byte R_MOTOR_B = 12;                                     //Pin Number attached to Right Motor Negative Pin
const byte OFF = 14;                                           //Pin Number for ON/OFF indicator
const byte SIG_1 = 15;                                         //Pin Number for Sonar #1
const byte SIG_2 = 16;                                         //Pin Number for Sonar #2
const byte SIG_3 = 17;                                         //Pin Number for Sonar #3
const byte R_ENCODER_A = 18;                                   //Pin Number for Right Encoder Phase A
const byte R_ENCODER_B = 19;                                   //Pin Number for Right Encoder Phase B
const byte SDA = 20;                                           //I2C Bus Data Line
const byte SCL = 21;                                           //I2C Bus Clock Line

//Analoge Pin Definitions
const byte LED_1 = A0;                                         //Pin # of LED
const byte LED_2 = A1;                                         //Pin # of LED
const byte LED_3 = A2;                                         //Pin # of LED
const byte LED_4 = A3;                                         //Pin # of LED
const byte LED_5 = A4;                                         //Pin # of LED
const byte LED_6 = A5;                                         //Pin # of LED
const byte LED_7 = A6;                                         //Pin # of LED
const byte LED_8 = A7;

I have delclared all my pin numbers as constants (global)

And?

I see the upside to your method. I'll just pass in my constant pin number. Thanks

const byte LED_1 = A0;                                         //Pin # of LED
const byte LED_2 = A1;                                         //Pin # of LED
const byte LED_3 = A2;                                         //Pin # of LED
const byte LED_4 = A3;                                         //Pin # of LED
const byte LED_5 = A4;                                         //Pin # of LED
const byte LED_6 = A5;                                         //Pin # of LED
const byte LED_7 = A6;                                         //Pin # of LED
const byte LED_8 = A7;

Looks like more opportunities for using arrays here too.

Yeah, thanks. I'll put those guys in an array. Do you know what the value of A0 is in integer?

No - you shouldn't need to know - why?

(It's 14 on a 2009, as it happens but you've got a Mega, I think.
For future reference the value is in "WProgram.h")

Just wanted to know. When I first declared them as a byte I wasn't sure if they were primitive type but it complied so I figured they were numbers.

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
const static uint8_t A0 = 54;

But, like I said, you don't really need to know the actual value - if you put them into a scalar variable or constant, you can put them into an array of the same type.