private void OnEchoPortChanged(object sender, DigitalInputPortEventArgs e) { if (e.Value == true) // if(echoPort.State == true) { tickStart = DateTime.Now.Ticks; return; } // Console.WriteLine("false"); // Calculate Difference var elapsed = DateTime.Now.Ticks - tickStart; // Return elapsed ticks // x10 for ticks to micro sec // divide by 58 for cm (assume speed of sound is 340m/s) var curDis = elapsed / 580; CurrentDistance = curDis; //debug - remove Console.WriteLine($"{elapsed}, {curDis}, {CurrentDistance}, {DateTime.Now.Ticks}"); //restore this before publishing to hide false results // if (CurrentDistance < MinimumDistance || CurrentDistance > MaximumDistance) // CurrentDistance = -1; DistanceDetected?.Invoke(this, new DistanceEventArgs(CurrentDistance)); }
private void EchoChanged(object sender, DigitalInputPortEventArgs e) { //Console.WriteLine("value of echo:{0}", echo.State); if (!_IsStarted) { //Console.WriteLine("Start is false"); return; } if (e.Value) { //Console.WriteLine("Echo value is true, reset duration"); //duration = DateTime.Now.Ticks; // Start the clock. return; } // Echo got something back. We can calculate the duration. var endDate = DateTime.Now.Ticks; var delta = endDate - duration; //Console.WriteLine("delta {0}", delta); CurrentDistance = delta / SpeedOfSoundFactor; DistanceDetected?.Invoke(this, new DistanceEventArgs(CurrentDistance)); _IsStarted = false; }
private void OnEchoPortChanged(object sender, DigitalInputPortEventArgs e) { if (e.Value == true) // if(_echoPort.State == true) { /// Console.WriteLine("true"); tickStart = DateTime.Now.Ticks; return; } // Console.WriteLine("false"); // Calculate Difference float elapsed = DateTime.Now.Ticks - tickStart; // Return elapsed ticks // x10 for ticks to micro sec // divide by 58 for cm (assume speed of sound is 340m/s) CurrentDistance = elapsed / 580f; // if (CurrentDistance < MinimumDistance || CurrentDistance > MaximumDistance) // CurrentDistance = -1; DistanceDetected?.Invoke(this, new DistanceEventArgs(CurrentDistance)); }
private void TriggerEchoPort_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e) { if (triggerEchoPort.Read() == GpioPinValue.High) { tickStart = DateTime.Now.Ticks; return; } // Calculate Difference float elapsed = DateTime.Now.Ticks - tickStart; // Return elapsed ticks // x10 for ticks to micro sec // divide by 58 for cm (assume speed of sound is 340m/s) CurrentDistance = elapsed / 580f; // if (CurrentDistance < MinimumDistance || CurrentDistance > MaximumDistance) // CurrentDistance = -1; DistanceDetected?.Invoke(this, new DistanceEventArgs(CurrentDistance)); }
private void AnalogInputPort_Changed(object sender, FloatChangeResult e) { CurrentDistance = 26 / e.New; DistanceDetected?.Invoke(this, new DistanceEventArgs(CurrentDistance)); }
private void AnalogValueChanged(float newValue) { CurrentDistance = 26 / newValue; DistanceDetected?.Invoke(this, new DistanceEventArgs(CurrentDistance)); }