Hello,
Just put together this program which uses 3 ping sensors (mounted on my robot). For the time being(or may leave it in the code), I added a buzzer to emit sound when a certain minimum distance was reached for each sensor.
The program works but was wondering if I can improve it in any way or make it more efficient.
// 3 Ping sensors with buzzer alarm
// Ping sensors mounted on robot: 2 Ping sensors mounted on front and 1 Ping sensor mounted on rear
// Have buzzer emit sound when a minimum threshold distance in inches is reached for each Ping sensor
// 6May12
int durationPingLeft; // Stores duration of pulse on Left Sensor
int durationPingRight; // Stores duration of pulse on Right Sensor
int durationPingRear; // Stores duration of pulse on Rear Sensor
int distancePingLeft; // Stores distance Left Sensor
int distancePingRight; // Stores distance Right Sensor
int distancePingRear; // Stores distance Rear Sensor
const int LeftPingSens = 2; // Left Ping sensor connected to digital pin D2
const int RightPingSens = 4; // Right Ping sensor connected to digital pin D4
const int RearPingSens = 7; // Rear Ping sensor connected to digital pin D7
const int buzzOut = 8; // Buzzer connected to digital pin D8
void setup() {
Serial.begin(9600);
pinMode(buzzOut, OUTPUT);
}
void loop()
{
// Left Ping sensor:
pinMode(LeftPingSens, OUTPUT); // Set pin to OUTPUT Left Sensor
digitalWrite(LeftPingSens, LOW); // Ensure pin is low Left Sensor
delayMicroseconds(2);
digitalWrite(LeftPingSens, HIGH); // Start ranging Left Sensor
delayMicroseconds(10);
digitalWrite(LeftPingSens, LOW); // End ranging Left Sensor
pinMode(LeftPingSens, INPUT); // Set pin to INPUT Left Sensor
int durationPingLeft = pulseIn(LeftPingSens, HIGH); // Read echo pulse Left Sensor
distancePingLeft = durationPingLeft / 74 / 2; // Convert to inches Left Sensor
// Right Ping sensor:
pinMode(RightPingSens, OUTPUT); // Set pin to OUTPUT Right Sensor
digitalWrite(RightPingSens, LOW); // Ensure pin is low Right Sensor
delayMicroseconds(2);
digitalWrite(RightPingSens, HIGH); // Start ranging Right Sensor
delayMicroseconds(10);
digitalWrite(RightPingSens, LOW); // End ranging Right Sensor
pinMode(RightPingSens, INPUT); // Set pin to INPUT Rear Sensor
int durationPingRight = pulseIn(RightPingSens, HIGH); // Read echo pulse Right Sensor
distancePingRight = durationPingRight / 74 / 2; // Convert to inches Right Sensor
// Rear Ping sensor:
pinMode(RearPingSens, OUTPUT); // Set pin to OUTPUT Rear Sensor
digitalWrite(RearPingSens, LOW); // Ensure pin is low Rear Sensor
delayMicroseconds(2);
digitalWrite(RearPingSens, HIGH); // Start ranging Rear Sensor
delayMicroseconds(10);
digitalWrite(RearPingSens, LOW); // End ranging Rear Sensor
pinMode(RearPingSens, INPUT); // Set pin to INPUT Rear Sensor
int durationPingRear = pulseIn(RearPingSens, HIGH); // Read echo pulse Rear Sensor
distancePingRear = durationPingRear / 74 / 2; // Convert to inches Rear Sensor
digitalWrite(buzzOut, LOW);
if(distancePingLeft < 24) digitalWrite(buzzOut, HIGH); // sets a min. threshold value of 24" to sound alarm for Left Ping sensor
if(distancePingRight < 24) digitalWrite(buzzOut, HIGH); // sets a min. threshold value of 24" to sound alarm for Right Ping sensor
if(distancePingRear < 18) digitalWrite(buzzOut, HIGH); // sets a min. threshold value of 18" to sound alarm for Rear Ping sensor
Serial.print(F("Left Sensor "));
Serial.println(distancePingLeft);
Serial.print(F("Right Sensor "));
Serial.println(distancePingRight);
Serial.print(F("Rear Sensor "));
Serial.println(distancePingRear);
Serial.println("");
{
delay(800);
}
}