added distance measurer
This commit is contained in:
parent
472ea170d0
commit
3b6cfed5b2
@ -212,5 +212,10 @@ Here are some fun programs for your @boardname@!
|
||||
"description": "Keep your brick entertained and happy",
|
||||
"url":"/examples/happy-unhappy",
|
||||
"cardType": "example"
|
||||
}, {
|
||||
"name": "Distance Measurer",
|
||||
"description": "Use a motor to measure angle and distance",
|
||||
"url": "/examples/distance-measurer",
|
||||
"cardType": "example"
|
||||
}]
|
||||
```
|
34
docs/examples/distance-measurer.md
Normal file
34
docs/examples/distance-measurer.md
Normal file
@ -0,0 +1,34 @@
|
||||
# Distance Measurer
|
||||
|
||||
```blocks
|
||||
let distance = 0
|
||||
let angle = 0
|
||||
let measuring = false
|
||||
let radius = 0
|
||||
// Start and stop measuring with the enter button
|
||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
||||
if (measuring) {
|
||||
// turn off the measuring
|
||||
measuring = false
|
||||
brick.setStatusLight(StatusLight.Off)
|
||||
} else {
|
||||
// turn on the measuring clear the counters so that
|
||||
// the motor tracks the angle
|
||||
measuring = true
|
||||
motors.largeB.clearCounts()
|
||||
brick.setStatusLight(StatusLight.GreenPulse)
|
||||
}
|
||||
})
|
||||
radius = 2.5
|
||||
brick.showString("Press ENTER to measure", 4)
|
||||
forever(function () {
|
||||
if (measuring) {
|
||||
angle = motors.largeB.angle()
|
||||
distance = angle / 180 * Math.PI * radius
|
||||
brick.clearScreen()
|
||||
brick.showValue("angle", angle, 2)
|
||||
brick.showValue("distance", distance, 3)
|
||||
}
|
||||
pause(100)
|
||||
})
|
||||
```
|
Loading…
Reference in New Issue
Block a user