MakeCode extension for ultrasonic distance measurement with an HC-SR04 sensor.
The MakerBit connects to the BBC micro:bit to provide easy connections to a wide variety of sensors, actuators and other components.
MakerBit | MakerBit+R with motor controller |
Support for an external HC-SR04 ultrasonic distance sensor to steer your robots. The blocks return immediately with the most recent value because measurements are done continuously in the background.
Configures the ultrasonic distance sensor and measures continuously in the background. The HC-SR04 needs to be connected to a 5V source (HC-SR04 VCC) and to ground (HC-SR04 GND).
makerbit.connectUltrasonicDistanceSensor(DigitalPin.P5, DigitalPin.P8)
Do something when an object is detected within a range.
makerbit.onUltrasonicObjectDetected(10, DistanceUnit.CM, () => {})
Returns the distance to an object in a range from 1 to 300 centimeters or up to 118 inch. The maximum value is returned to indicate when no object was detected. -1 is returned when the device is not connected.
makerbit.getUltrasonicDistance(DistanceUnit.CM)
Returns true
if an object is within the specified distance. false
otherwise.
makerbit.isUltrasonicDistanceLessThan(10, DistanceUnit.CM)
let distance = 0
makerbit.connectUltrasonic(DigitalPin.P5, DigitalPin.P8)
basic.forever(() => {
distance = makerbit.getUltrasonicDistance(DistanceUnit.CM)
led.plotBarGraph(distance, 0)
})
Licensed under the MIT License (MIT). See LICENSE file for more details.
- for PXT/microbit
- for PXT/calliope