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
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.
}
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.
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;
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.
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.