예제 #1
0
        private static void Main(string[] args)
        {
            var interval = GetInterval(args);

            var triggerPin = ConnectorPin.P1Pin03.ToProcessor();
            var echoPin = ConnectorPin.P1Pin07.ToProcessor();

            using (var connection = new HcSr04Connection(triggerPin, echoPin))
            {
                while (!Console.KeyAvailable)
                {
                    try
                    {
                        var distance = connection.GetDistance();
                        Console.WriteLine("{0:0.0}cm", distance * 100);
                    }
                    catch (TimeoutException)
                    {
                        Console.WriteLine("(Timeout)");
                    }

                    Timer.Sleep(interval);
                }
            }
        }
예제 #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);

            using (var connection = new HcSr04Connection(triggerPin.ToProcessor(), 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;
        }