I made some c sharp drivers for the Ping))) Sensor from parallax (http://www.parallax....92/Default.aspx). These are pretty nice I think. I took a look at this: http://forums.netdui...ch__1#entry4555
I then decided to use the TristatePort class (previously used in my charlieplexing code). I also got started with the math behind it from someone else's code.. if you are the one who wrote some code on the speed of sound conversion let me know and I'll post a link to your thread. Sorry I forgot who it was
What I basicly did was a lot of optimization (other programmer friends of mine hate this, i love it) and cleaning up, also nice commented code makes for happy you and me! Also added a few new features like the Units enum and the math is optimized as well.
I'll make a video soon. Let me know if you have any questions or issues. Little note, the port that connects to the Ping))) does not need to be a PWM!
Enjoy
using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using System.Threading; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace Parallax.Sensors.Ping { public enum DistanceUnits { mm, cm, dm, m, feet, inch, yard } public class Ping { TristatePort _port; DistanceUnits _unit = DistanceUnits.mm; double _soundSpeed = 343, _convertion = (10000 / 343) * 2; // default values public Ping(Cpu.Pin pin) { _port = new TristatePort(pin, false, false, ResistorModes.Disabled); } /// <summary> /// Automaticly adjust the convertion factor depending on air temperature. /// </summary> /// <param name="degC">The temperature in degrees celsius</param> public void AdjustSoundSpeed(double degC) { /* Speed of Sound (at 20 degrees celsius): 343 m/s * or * _soundSpeed = 331.4 + 0.6 * degC * * There are 10,000,000 ticks per second. * 10,000,000 / _soundSpeed * 1000 can be simplyfied into: * 10,000 / _soundSpeed * times it by 2 because of round trip * then you get about 58.309 ticks per mm * * then multiply if other unit is needed * */ _soundSpeed = 331.4 + 0.6 * degC; _convertion = (10000 / _soundSpeed) * 2; } /// <summary> /// Return the Ping))) sensor's reading in millimeters. /// </summary> /// <param name="usedefault">Set true to return value in the unit specified by the "Unit" property. /// Set false to return value in mm.</param> public double GetDistance() { bool low = true, high = true; long t1, t2; // Set it to an putput _port.Active = true; //Send a quick HIGH pulse _port.Write(true); _port.Write(false); // Set it as an input _port.Active = false; while (low) low = !_port.Read(); t1 = System.DateTime.Now.Ticks; while (high) high = _port.Read(); t2 = System.DateTime.Now.Ticks; return Convert(((t2 - t1) / _convertion), _unit); } /// <summary> /// Convert the millimeters into other units. /// </summary> /// <param name="millimeters">The Ping))) sensor's mm reading.</param> /// <param name="outputUnit">The desired output unit.</param> public double Convert(double millimeters, DistanceUnits outputUnit) { double result = millimeters; switch (outputUnit) { case DistanceUnits.cm: result = millimeters * 0.1F; break; case DistanceUnits.dm: result = millimeters * 0.01F; break; case DistanceUnits.m: result = millimeters * 0.001F; break; case DistanceUnits.inch: result = millimeters * 0.0393700787; break; case DistanceUnits.feet: result = millimeters * 0.0032808399; break; case DistanceUnits.yard: result = millimeters * 0.0010936133; break; } return result; } public DistanceUnits Unit { get { return _unit; } set { _unit = value; } } } }