added distance measurer

This commit is contained in:
Peli de Halleux 2018-02-15 13:56:50 -08:00
parent 472ea170d0
commit 3b6cfed5b2
2 changed files with 39 additions and 0 deletions

View File

@ -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"
}]
```

View 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)
})
```