need help programming proximity sensor

ok, so i made a code using to different example codes and put them together. One code was for the proximity sensor it let you tell the distance of objects and relayed the info to the serial monitor. the other code let you display some text on the LCD. i put the code that let you display text on the LCD into the proximity sensor code. so I could hook up the proximity sensor and the LCD to the Arduino and the distance would be displayed on the LCD. NOW my problem is that the distance seems to be off by a lot. I heard that sound travels differently through different temperatures. So i was wondering if i could change or put something in the code so it could adapt to the temperature i'm using it in. I usually use it indoors with is around 75 F.

here is the code

/* Ping))) Sensor

This sketch reads a PING))) ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.

The circuit:

  • +V connection of the PING))) attached to +5V
  • GND connection of the PING))) attached to ground
  • SIG connection of the PING))) attached to digital pin 7

created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

*/

// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin = 7;

const int TxPin = 6;

#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

pinMode(TxPin, OUTPUT);
digitalWrite(TxPin, HIGH);

mySerial.begin(9600);
delay(100);
mySerial.write(12);
mySerial.write(17);
delay(5);
mySerial.print(inches);
mySerial.print("in, ");
mySerial.print(cm);
mySerial.print("cm");
mySerial.println();
mySerial.write(17);

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Use proper code tag here , to avoid confustion

  1. let us know which board ur using???
  2. if using softwareSerial library ,then replace Serial. print by what ever name given like Myserial.print
    3)SoftwareSerial mySerial = SoftwareSerial(255, TxPin); must be data pins of controller which u have being using it

i'm using a micro. the code does work but i want to make the readings of it more accurate and i heard sound travels differently through different temperatures. i was wondering how i could change the code to make it more accurate. BTW i basiclly put together to example codes for my LCD and proximity sensor.

/* Ping))) Sensor
  
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
   * +V connection of the PING))) attached to +5V
   * GND connection of the PING))) attached to ground
   * SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping
   
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe
 
   This example code is in the public domain.

 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

const int TxPin = 6;

   
 #include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
   
 #include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
  
  
 mySerial.begin(9600);
  delay(100);
   mySerial.write(12);                            
  mySerial.write(17);  
  delay(5);
mySerial.print(inches);
mySerial.print("in, ");
mySerial.print(cm);
 mySerial.print("cm");
 mySerial.println();
 mySerial.write(17);
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

you are really very careful, :wink:
i have use this module for a long time , but did not find any detailed description abou the temperature affect/..

anyone have a idea of how i could make the readings more accurate.

Filters, running or moving average, accurate sample times..

All will prove relative accuracy..

Sound at ground level :

Temperature is also a condition that affects the speed of sound. Heat, like sound, is a form of kinetic energy. Molecules at higher temperatures have more energy, thus they can vibrate faster. Since the molecules vibrate faster, sound waves can travel more quickly. The speed of sound in room temperature air is 346 meters per second. This is faster than 331 meters per second, which is the speed of sound in air at freezing temperatures.

The formula to find the speed of sound in air is as follows:

v = 331m/s + 0.6m/s/C * T

v is the speed of sound and T is the temperature of the air. One thing to keep in mind is that this formula finds the average speed of sound for any given temperature. The speed of sound is also affected by other factors such as humidity and air pressure.

The GAS Model..

The ideal gas law is based on a simple picture of a gas as a large number of molecules which move independantly of one another, except for occasional collisions with each other or with the walls of their container. When they do collide, the collision occurs with no net loss of energy -- that is, it is an elastic collision.

The ideal gas model predicts that the speed of sound in a pure gas will be
vs = sqrt(gammaP/rho)
where ? is the adiabatic constant (also referred to as the adiabatic exponent, the specific heat ratio, or the isentropic exponent) for the gas, which at room temperature depends mostly on the shape of the molecule and will have a value just a bit larger than 1, P is the absolute pressure of the gas, and ? is the density of the gas. Using the ideal gas law, PV = nRT (with n constant, that is the number of gas molecules is constant), the equation above can be rewritten as
vs = sqrt(gamma
kB*T/M)
where T is the temperature on an absolute scale (e.g. Kelvin), M is the mass of one gas molecule, and kB is Boltzmann's constant which converts absolute temperature units to energy units. Note that if the ideal gas model is a good model for a real gas, then you can expect, for any specific gas, that there will be no pressure dependence for the speed of sound. This is because as you change the pressure of the gas, you will also change its density by the same factor. The speed of sound will have a very significant dependence on temperature and on the mass of the molecules which make up the gas.

For comparison the "root mean square" (or "rms") velocity of the molecules in an ideal gas, an appropriate average for the speed of molecules in the gas, is given by vrms = sqrt(3kBT/M) and since ? is typically between 1.2 and 1.7, you can see that the average speed of the molecules is closely related to the speed of sound and will be only slightly larger. For typical air at room conditions, the average molecule is moving at about 500 m/s (close to 1000 miles per hour). Note that the speed of sound is largely determined by how fast the molecules move between collisions, and not on how often they make collisions. This is because no energy is lost during the collisions. The collisions do not "slow things down" but simply randomize the motion -- which was already quite random. At higher temperatures the molecules have more energy and are moving faster than at lower temperatures, hence the speed of sound at higher temperatures is faster than at lower temperatures.

But honestly this theoretical physics is fine but in the real world you can generally ignore the effects

So if sound in room temperature travels at 346 M/S how would I make that's into microseconds per centimeter?

Simple bit of Ratio maths...

Grrrrrrr not M/S please.... you mean m/s :0

The number you want is 29.

See if your calculation arrives at the same.