forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
49 lines (39 loc) · 897 Bytes
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Threading;
using System.Diagnostics;
using nanoFramework.device.sensor;
//
// Sample program to show using the RMT device to read the distance using a HC-SR04
//
namespace Ultrasonic
{
public class Program
{
public static void Main()
{
// Change to suit you hardware set-up
const int GPIO_TRIGGER_PIN = 27;
const int GPIO_ECHO_PIN = 26;
try
{
Sr04 device = new Sr04(GPIO_TRIGGER_PIN, GPIO_ECHO_PIN);
while (true)
{
float distance = device.GetDistance();
if (distance == -1)
Debug.WriteLine($"Out of range");
else
Debug.WriteLine($"Distance {distance:F3} meters");
Thread.Sleep(1000);
}
}
catch(Exception)
{ }
Thread.Sleep(Timeout.Infinite);
}
}
}