« November 2008 | Main

December 15, 2008

Ping Distance Calculations w/ Temperature Reading Code

I will be adding more, however I believe this is the only code I referenced for the website. It is the sub routines for ping() and thermistor readings.

There is an optional change that can be made to actually have the temperature readings affect the calculations for ping()

int PingPin = 8;

unsigned int ultrasoundValue;
unsigned long echo;

//TEMP = 172; // used if one wishes to feed temperature information in calculations for distance

unsigned int ping(){
pinMode(PingPin, OUTPUT); // Switch signalpin to output
digitalWrite(PingPin, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(PingPin, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(PingPin, LOW); // Holdoff
pinMode(PingPin, INPUT); // Switch signalpin to input
digitalWrite(PingPin, HIGH); // Turn on pullup resistor
echo = pulseIn(PingPin, HIGH); //Listen for echo
ultrasoundValue = (echo / 58.138)*.39;

//IF one wishes to feed the temperature into this calculation, uncomment the following line, and comment out the previous line
// ultrasoundValue = (echo * TEMP)/25641;

return ultrasoundValue;
}

unsigned int Thermister(int RawADC) { //the value of RawADC should be the returned value of analogRead(THERMISTORPIN);
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celcius

/*IF one wishes to feed the temperature into this calculation, uncomment the following line
TEMP = (Temp*.6 + 331.5)/20000; //for info on how this was found, check the data sheets*/

Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}