Ejemplo n.º 1
0
 public double GetValue()
 {
     try
     {
         var distance = 0.0;
         using (var connection = new HcSr04Connection(
             driver.Out(TRIGGERPIN.ToProcessor()),
             driver.In(ECHOPIN.ToProcessor())))
         {
             distance = connection.GetDistance().Centimeters;
             return distance;
         }
     }
     catch (TimeoutException tex)
     {
         throw new TimeoutException("Timeout", tex);
     }
 }
Ejemplo n.º 2
0
        private static void Main(string[] args)
        {
            Console.CursorVisible = false;

            const ConnectorPin triggerPin = ConnectorPin.P1Pin21;
            const ConnectorPin echoPin = ConnectorPin.P1Pin23;

            Console.WriteLine("HC-SR04 Sample: measure distance");
            Console.WriteLine();
            Console.WriteLine("\tTrigger: {0}", triggerPin);
            Console.WriteLine("\tEcho: {0}", echoPin);
            Console.WriteLine();

            var interval = GetInterval(args);
            var driver = GpioConnectionSettings.DefaultDriver;

            using (var connection = new HcSr04Connection(
                driver.Out(triggerPin.ToProcessor()), 
                driver.In(echoPin.ToProcessor())))
            {
                while (!Console.KeyAvailable)
                {
                    try
                    {
                        var distance = connection.GetDistance();
                        Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:0.0}cm", distance * 100).PadRight(16));
                        Console.CursorTop--;
                    }
                    catch (TimeoutException e)
                    {
                        Console.WriteLine("(Timeout): " + e.Message);
                    }

                    Timer.Sleep(interval);
                }
            }

            Console.CursorVisible = true;
        }