From 3b6cfed5b2f0fe27adf7280791bb177433dee887 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 15 Feb 2018 13:56:50 -0800 Subject: [PATCH] added distance measurer --- docs/examples.md | 5 +++++ docs/examples/distance-measurer.md | 34 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 docs/examples/distance-measurer.md diff --git a/docs/examples.md b/docs/examples.md index a9f6cc3b..7420911b 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -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" }] ``` \ No newline at end of file diff --git a/docs/examples/distance-measurer.md b/docs/examples/distance-measurer.md new file mode 100644 index 00000000..51157902 --- /dev/null +++ b/docs/examples/distance-measurer.md @@ -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) +}) +``` \ No newline at end of file