forked from microsoft/pxt-sonar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
42 lines (39 loc) · 1.22 KB
/
main.ts
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
enum PingUnit {
//% block="μs"
MicroSeconds,
//% block="cm"
Centimeters,
//% block="inches"
Inches
}
/**
* Sonar and ping utilities
*/
//% color="#2c3e50" weight=10
namespace sonar {
/**
* Stuur een sonar pingen meet die echo tijd (in microseconden) als resultaat
* @param trig tigger pin
* @param echo echo pin
* @param teller1 lekker onzin praat
* @param unit desired omreken eenheid
* @param maxCmDistance maximum afstand in centimeter (default is 500)
*/
//% blockId=sonar_ping block="ping trig %trig|echo %echo|unit %unit"
export function ping(trig: AnalogPin, echo: AnalogPin, unit: PingUnit, maxCmDistance : number {
// send pulse
pins.setPull(trig, PinPullMode.PullNone);
pins.digitalWritePin(trig, 0);
control.waitMicros(2);
pins.digitalWritePin(trig, 1);
control.waitMicros(10);
pins.digitalWritePin(trig, 0);
// read pulse
const d = pins.pulseIn(echo, PulseValue.High, maxCmDistance * 58);
switch (unit) {
case PingUnit.Centimeters: return Math.idiv(d, 58);
case PingUnit.Inches: return Math.idiv(d, 148);
default: return d ;
}
}
}