Merge pull request #20 from IsQianGe/patch-1

Added V2 version of ultrasonic sensor module
This commit is contained in:
Baozhu Zuo 2020-12-15 08:18:04 +08:00 committed by GitHub
commit c64162453a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

61
main.ts
View File

@ -453,6 +453,67 @@ namespace grove {
let paj7620 = new PAJ7620(); let paj7620 = new PAJ7620();
// adapted to Calliope mini V2 Core by M.Klein 17.09.2020 // adapted to Calliope mini V2 Core by M.Klein 17.09.2020
/**
* Create a new driver of Grove - Ultrasonic Sensor to measure distances in cm
* @param pin signal pin of ultrasonic ranger module
*/
//% blockId=grove_ultrasonic_centimeters_v2 block="Ultrasonic Sensor (in cm) at for V2|%pin"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
//% pin.fieldOptions.tooltips="false" pin.fieldOptions.width="250"
//% group="Ultrasonic" pin.defl=DigitalPin.C16
export function measureInCentimetersV2(pin: DigitalPin): number
{
let duration = 0;
let RangeInCentimeters = 0;
pins.digitalWritePin(pin, 0);
control.waitMicros(2);
pins.digitalWritePin(pin, 1);
control.waitMicros(20);
pins.digitalWritePin(pin, 0);
duration = pins.pulseIn(pin, PulseValue.High, 50000); // Max duration 50 ms
RangeInCentimeters = duration * 153 / 44 / 2 / 100 ;
if(RangeInCentimeters > 0) distanceBackup = RangeInCentimeters;
else RangeInCentimeters = distanceBackup;
basic.pause(50);
return RangeInCentimeters;
}
/**
* Create a new driver Grove - Ultrasonic Sensor to measure distances in inch
* @param pin signal pin of ultrasonic ranger module
*/
//% blockId=grove_ultrasonic_inches_v2 block="Ultrasonic Sensor (in inch) at for V2|%pin"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
//% pin.fieldOptions.tooltips="false" pin.fieldOptions.width="250"
//% group="Ultrasonic" pin.defl=DigitalPin.C16
export function measureInInchesV2(pin: DigitalPin): number
{
let duration = 0;
let RangeInInches = 0;
pins.digitalWritePin(pin, 0);
control.waitMicros(2);
pins.digitalWritePin(pin, 1);
control.waitMicros(20);
pins.digitalWritePin(pin, 0);
duration = pins.pulseIn(pin, PulseValue.High, 100000); // Max duration 100 ms
RangeInInches = duration * 153 / 113 / 2 / 100;
if(RangeInInches > 0) distanceBackup = RangeInInches;
else RangeInInches = distanceBackup;
basic.pause(50);
return RangeInInches;
}
/** /**
* Create a new driver of Grove - Ultrasonic Sensor to measure distances in cm * Create a new driver of Grove - Ultrasonic Sensor to measure distances in cm
* @param pin signal pin of ultrasonic ranger module * @param pin signal pin of ultrasonic ranger module