Finally had a [broken] chance to play with my Netduino. Here's a quick class I wrote for measuring distances.
It's very simple, needs some polishing and commenting. I'll get to it
I've used it, along with someone else's serial class (apologies, I cannot remember who wrote it! ) to show distances on my Cat's Whisker LCD (What a nice piece of kit!)
Anyway....
class Distance { public enum SensorType //mV/Inch { Maxbotix_LV_EZ = 10 } private Cpu.Pin inputPin { get; set; } private AnalogInput analogInput { get; set; } public SensorType sensorType { get; set; } public double vRef { get; set; } private const double _mmPerInch = 25.4; private const double _inchPerFoot = 12.00; public Distance(Cpu.Pin analogInputPin, SensorType sensorType) { this.inputPin = analogInputPin; this.analogInput = new AnalogInput(inputPin); this.sensorType = sensorType; this.vRef = 3.3; } public double getDistanceIn_CM() { return getDistanceIn_mm() / 10.00; } public double getDistanceIn_Inch() { return _baseDistance(); } public double getDistanceIn_Feet() { return _baseDistance() / _inchPerFoot; } public double getDistanceIn_mm() { //double _base = _baseDistance(); //return _base * (double)25.4; return _baseDistance() * _mmPerInch; } private double _baseDistance() { //double ar = Convert.ToDouble(analogInput.Read().ToString()); //double v = (vRef / 1023.00) * ar; //double dis = v / ((double)sensorType / 1000.00); //return dis; return ((vRef / 1023.00) * (Convert.ToDouble(analogInput.Read().ToString()))) / ((double)sensorType / 1000.00); } }
I need to double check the results and add some bits but for now, it's shows I can do it
Usage:
public static void Main() { SerialPort UART = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); Distance dis = new Distance(Pins.GPIO_PIN_A0, Distance.SensorType.Maxbotix_LV_EZ); SerialCom serial = new SerialCom(UART); string sDis = ""; while (true) { sDis = dis.getDistanceIn_CM().ToString(); Thread.Sleep(250); led.Write(!led.Read()); serial.print("Distance: " + sDis + "\n"); Debug.Print(sDis); Thread.Sleep(250); } }
It was a bummer I could not use the pw output. I cannot see a way to measure uSec in .net so resorted to analogIn. The sensors do have serial com on them but I see that as a waste of my precious UARTss.
I'm a database mechanic by trade, write c# (normal and compact) as a hobby / other bits and pieces. Always appreciate comments on how to improve stuff (I'm an efficiency freak and want it as slim as possible!)
Cheers,
Crispin
edit: can't upload the solution for this Will work it out in the morning...