コード例 #1
0
ファイル: HcSr04Connection.cs プロジェクト: pi-dotnet/Pi
        /// <summary>
        /// Gets the distance.
        /// </summary>
        /// <returns>The distance.</returns>
        public Length GetDistance()
        {
            triggerPin.Write(true);
            Timer.Sleep(triggerTime);
            triggerPin.Write(false);

            var upTime = echoPin.Time(true, echoUpTimeout, Timeout);

            return(Units.Velocity.Sound.ToDistance(upTime) / 2);
        }
コード例 #2
0
        public double getDistance()
        {
            double dist = -1;

            do
            {
                try {
                    triggerPin.Write(true);
                    Timer.Sleep(triggerTime);
                    triggerPin.Write(false);
                    var upTime = echoPin.Time(true, echoUpTimeout, Timeout);
                    dist = ((upTime < TimeSpan.Zero) ? (double.MinValue) : ((upTime.TotalMilliseconds) / 1000.0 * 343.8 * 100) / 2.0);
                }
                catch (TimeoutException) { }
            } while (dist < 0);
            return(dist);
        }
コード例 #3
0
ファイル: HcSr04Connection.cs プロジェクト: tisma/IoT.Home.Pi
        /// <summary>
        /// Gets the distance.
        /// </summary>
        /// <returns>The distance.</returns>
        public Length GetDistance()
        {
            //for (int i = 1; i < 1000; i++)
            //{
            //    triggerPin.Write(true);
            //    Timer.Sleep(triggerTime);
            //    triggerPin.Write(false);
            //    Timer.Sleep(triggerTime);
            //}

            triggerPin.Write(true);
            Timer.Sleep(triggerTime);
            triggerPin.Write(false);

            var upTime = echoPin.Time(true, echoUpTimeout, Timeout);

            return(Units.Velocity.Sound.ToDistance(upTime) / 2);
        }