/// <summary> /// Tests the ultrasonic sensor. /// </summary> public static void TestUltrasonicSensor() { using (var sensor = new UltrasonicHcsr04(Pi.Gpio[BcmPin.Gpio23], Pi.Gpio[BcmPin.Gpio24])) { sensor.OnDataAvailable += (s, e) => { if (e.IsValid) { if (e.HasObstacles) { if (e.Distance < 10) { $"Obstacle detected at {e.Distance:N2}cm / {e.DistanceInch:N2}in".WriteLine(ConsoleColor.Red); } else if (e.Distance < 30) { $"Obstacle detected at {e.Distance:N2}cm / {e.DistanceInch:N2}in".WriteLine(ConsoleColor.DarkYellow); } else if (e.Distance < 100) { $"Obstacle detected at {e.Distance:N2}cm / {e.DistanceInch:N2}in".WriteLine(ConsoleColor.Yellow); } else { $"Obstacle detected at {e.Distance:N2}cm / {e.DistanceInch:N2}in".WriteLine(ConsoleColor.Green); } } else { "No obstacles detected.".Info("HC - SR04"); } } else { "Invalid Reading".Error("HC-SR04"); } }; sensor.Start(); Console.ReadKey(true); } }
static void Main(string[] args) { var ledPin = 27; var triggerPin = 17; var echoPin = 4; var objectDetected = false; Pi.Init <BootstrapWiringPi>(); Pi.Gpio[ledPin].PinMode = GpioPinDriveMode.Output; using (var sensor = new UltrasonicHcsr04(Pi.Gpio[triggerPin], Pi.Gpio[echoPin])) { sensor.OnDataAvailable += async(s, e) => { if (e.IsValid) { if (e.Distance < 50) { Console.WriteLine($"Object detected at {e.Distance:N2}cm!"); if (!objectDetected) { objectDetected = true; Pi.Gpio[ledPin].Value = true; } } else { if (objectDetected) { objectDetected = false; Pi.Gpio[ledPin].Value = false; } } } }; sensor.Start(); Console.ReadLine(); } }
/// <summary> /// For this test, connect the ultrasonic sensor to pins 23 and 24. /// See http://sensorkit.joy-it.net/index.php?title=KY-050_Ultraschallabstandssensor for a cabling diagram using the KY-050 sensor module /// together with a KY-051 voltage translator. But note that this example uses GPIO.23 and GPIO.24 instead of GPIO.17 and GPIO.27 there. /// </summary> public static void TestUltrasonicSensor() { ConsoleColor color; using var sensor = new UltrasonicHcsr04(Pi.Gpio[BcmPin.Gpio23], Pi.Gpio[BcmPin.Gpio24]); sensor.OnDataAvailable += (s, e) => { Terminal.Clear(); if (!e.IsValid) { Terminal.WriteLine("Sensor could not be read (distance to close?)."); } else if (e.HasObstacles) { if (e.Distance <= 10) { color = ConsoleColor.DarkRed; } else if (e.Distance <= 20) { color = ConsoleColor.DarkYellow; } else if (e.Distance <= 30) { color = ConsoleColor.Yellow; } else if (e.Distance <= 40) { color = ConsoleColor.Green; } else if (e.Distance <= 50) { color = ConsoleColor.Blue; } else { color = ConsoleColor.White; } var distance = e.Distance < 57 ? e.Distance : 58; Terminal.WriteLine($"{new string('█', (int)distance)}", color); Terminal.WriteLine("--------------------------------------------------------->"); Terminal.WriteLine(" 10 20 30 40 50 cm"); Terminal.WriteLine($"Obstacle detected at {e.Distance:N2}cm / {e.DistanceInch:N2}in\n"); } else { Terminal.WriteLine("No obstacles detected."); } Terminal.WriteLine(ExitMessage); }; sensor.Start(); while (true) { var input = Terminal.ReadKey(true).Key; if (input != ConsoleKey.Escape) { continue; } break; } }
public HcSrHandler(DeviceClient deviceClient) { this.deviceClient = deviceClient; this.sensorInstance = new UltrasonicHcsr04(Pi.Gpio[BcmPin.Gpio11], Pi.Gpio[BcmPin.Gpio12]); this.sensorInstance.OnDataAvailable += HandleObstacleAsync; }
public static void TestUltrasonicSensor() { ConsoleColor color; using (var sensor = new UltrasonicHcsr04(Pi.Gpio[BcmPin.Gpio23], Pi.Gpio[BcmPin.Gpio24])) { sensor.OnDataAvailable += (s, e) => { Console.Clear(); if (!e.IsValid) { return; } if (e.HasObstacles) { if (e.Distance <= 10) { color = ConsoleColor.DarkRed; } else if (e.Distance <= 20) { color = ConsoleColor.DarkYellow; } else if (e.Distance <= 30) { color = ConsoleColor.Yellow; } else if (e.Distance <= 40) { color = ConsoleColor.Green; } else if (e.Distance <= 50) { color = ConsoleColor.Blue; } else { color = ConsoleColor.White; } var distance = e.Distance < 57 ? e.Distance : 58; $"{new string('█', (int)distance)}".WriteLine(color); "--------------------------------------------------------->".WriteLine(); " 10 20 30 40 50 cm".WriteLine(); $"Obstacle detected at {e.Distance:N2}cm / {e.DistanceInch:N2}in\n".WriteLine(); } else { "No obstacles detected.\n".Info("HC - SR04"); } ExitMessage.WriteLine(); }; sensor.Start(); while (true) { var input = Console.ReadKey(true).Key; if (input != ConsoleKey.Escape) { continue; } break; } } }