I’m using the freeware below on digital pins 10 & 11. I get valid readings with either pin 10 or 11 but when I modify the program to read both sensors, pin 10 is always zero when pin 11 gets valid readings. This is true regardless of which pin I read first. I’ve tried both the Uno on pins 10, 11 and the Mega on pins 30 & 40 with the same results.
//
// Function: Measure the distance to obstacles in front and print the distance
// value to the serial terminal.The measured distance is from
// the range 0 to 400cm(157 inches).
// Hardware: Ultrasonic Range sensor
// Arduino IDE: Arduino-1.0
// Author: LG
// Date: Jan 17,2013
// Version: v1.0 modified by FrankieChu
// by www.seeedstudio.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
/**/
#include “Arduino.h”
class Ultrasonic
{
public:
Ultrasonic(int pin);
void DistanceMeasure(void);
long microsecondsToCentimeters(void);
long microsecondsToInches(void);
private:
int _pin;//pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.
long duration;// the Pulse time received;
};
Ultrasonic::Ultrasonic(int pin)
{
_pin = 11;
}
/Begin the detection and get the pulse back signal/
void Ultrasonic::DistanceMeasure(void)
{
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);
delayMicroseconds(16);
digitalWrite(_pin, HIGH);
delayMicroseconds(40);
digitalWrite(_pin,LOW);
pinMode(_pin,INPUT);
duration = pulseIn(_pin,HIGH);
}
/The measured distance from the range 0 to 400 Centimeters/
long Ultrasonic::microsecondsToCentimeters(void)
{
return duration/29/2;
}
/The measured distance from the range 0 to 157 Inches/
long Ultrasonic::microsecondsToInches(void)
{
return duration/74/2;
}
Ultrasonic ultrasonic(7);
void setup()
{
Serial.begin(9600);
}
void loop()
{
long RangeInInches;
long RangeInCentimeters;
ultrasonic.DistanceMeasure();// get the current signal time;
RangeInInches = ultrasonic.microsecondsToInches();//convert the time to inches;
RangeInCentimeters = ultrasonic.microsecondsToCentimeters();//convert the time to centimeters
Serial.print(“The distance to obstacles in front is: “);
Serial.print(RangeInInches);//0~157 inches
Serial.print(” inch “);
Serial.print(RangeInCentimeters);//0~400cm
Serial.println(” cm”);
delay(1000);
}
Here’s the code I modified to read both pins.
void setup()
{
Serial.begin(9600);
}
void loop()
{
long duration;
int _pin,i;
unsigned long timeout=1000000;
_pin = 30;
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);
delayMicroseconds(2);
digitalWrite(_pin, HIGH);
delayMicroseconds(5);
digitalWrite(_pin,LOW);
pinMode(_pin,INPUT);
duration = pulseIn(_pin,HIGH);
duration = duration/74/2;
Serial.print("Pin ");
Serial.print (_pin);
Serial.print (": The distance to obstacles in front is: ");
Serial.print(duration);
Serial.println(" inch ");
delay(500);
_pin = 40;
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);
delayMicroseconds(2);
digitalWrite(_pin, HIGH);
delayMicroseconds(5);
digitalWrite(_pin,LOW);
pinMode(_pin,INPUT);
duration = pulseIn(_pin,HIGH);
duration = duration/74/2;
Serial.print("Pin ");
Serial.print (_pin);
Serial.print (": The distance to obstacles in front is: ");
Serial.print(duration);
Serial.println(" inch ");
delay(500);
}
Any help you can provide regarding reading the ultrasonic senors is appreciated.